10
10
use Phonex \Jobs \IssueProductLicense ;
11
11
use Phonex \Model \OrderInapp ;
12
12
use Phonex \Model \OrderInappState ;
13
+ use Phonex \Model \OrderPlayInapp ;
13
14
use Phonex \Model \Product ;
14
15
use Phonex \Purchase \PlayPurchase ;
15
16
use Phonex \User ;
@@ -27,9 +28,10 @@ class PurchaseController extends Controller {
27
28
const RESULT_ERR_INVALID_PRODUCT = 4 ;
28
29
const RESULT_ERR_TEST = 5 ;
29
30
const RESULT_ERR_INVALID_SIGNATURE = 6 ;
31
+ const RESULT_ERR_EXISTING_ORDER_ID = 7 ;
32
+ const RESULT_MULTIPLE_PURCHASES = 8 ; // when saving multiple purchases
30
33
31
-
32
- /*These codes are coming from apple itunes store*/
34
+ /* These codes are coming from apple itunes store */
33
35
// The App Store could not read the JSON object you provided.
34
36
const RESULT_APPSTORE_CANNOT_READ = 21000 ;
35
37
// The data in the receipt-data property was malformed or missing.
@@ -54,6 +56,11 @@ class PurchaseController extends Controller {
54
56
public function __construct (){
55
57
}
56
58
59
+ /**
60
+ * Main processing method for Google Play store transactions
61
+ * @param Request $request
62
+ * @return string
63
+ */
57
64
public function postAndroidPaymentVerification (Request $ request )
58
65
{
59
66
$ user = $ request ->attributes ->get (MiddlewareAttributes::CLIENT_CERT_AUTH_USER );
@@ -63,31 +70,79 @@ public function postAndroidPaymentVerification(Request $request)
63
70
64
71
// Parse json
65
72
$ jsonReq = $ request ->get ('request ' );
66
- $ playPurchase = PlayPurchase::fromJson ($ jsonReq );
73
+ $ jsonObject = json_decode ($ jsonReq , true );
74
+ $ playPurchase = PlayPurchase::fromJsonObject ($ jsonObject );
75
+
76
+ // Process Ticket
77
+ $ returnCode = $ this ->processPlayPurchase ($ playPurchase , $ user );
78
+ if ($ returnCode == self ::RESULT_OK ){
79
+ Log::info ("received google payment verification " , [$ returnCode , $ jsonReq ]);
80
+ } else {
81
+ Log::error ("received google payment verification " , [$ returnCode , $ jsonReq ]);
82
+ }
67
83
68
- if ($ playPurchase === null ){
69
- Log::error ("json_decode failed " , [$ jsonReq ]);
84
+ return $ this ->getResponse ($ returnCode );
85
+ }
86
+
87
+ /**
88
+ * Processes array of payments
89
+ * @param Request $request
90
+ * @return string
91
+ */
92
+ public function postAndroidPaymentsVerification (Request $ request )
93
+ {
94
+ $ user = $ request ->attributes ->get (MiddlewareAttributes::CLIENT_CERT_AUTH_USER );
95
+ if (!$ user ){
96
+ return $ this ->getResponse (self ::RESULT_ERR_INVALID_USER );
97
+ }
98
+
99
+
100
+ $ jsonReq = $ request ->get ('request ' );
101
+ $ jsonObjects = json_decode ($ jsonReq , true );
102
+ if ($ jsonObjects == null || !is_array ($ jsonObjects )){
70
103
return $ this ->getResponse (self ::RESULT_ERR_JSON_PARSING );
71
104
}
105
+
106
+ $ responseArray = [];
107
+ // Process purchases one by one
108
+ foreach ($ jsonObjects as $ jsonObject ){
109
+ $ playPurchase = PlayPurchase::fromJsonObject ($ jsonObject );
110
+ $ errCode = $ this ->processPlayPurchase ($ playPurchase , $ user );
111
+ $ responseArray [] = $ errCode ;
112
+ }
113
+ Log::info ("received google payment verifications " , [$ responseArray , $ jsonReq ]);
114
+ return $ this ->getResponseForMultiplePurchases ($ responseArray );
115
+ }
116
+
117
+
118
+ private function processPlayPurchase (PlayPurchase $ playPurchase , User $ user )
119
+ {
120
+ if ($ playPurchase === null ){
121
+ return self ::RESULT_ERR_JSON_PARSING ;
122
+ }
72
123
if (!$ playPurchase ->verifySignature ()){
73
- return $ this ->getResponse (self ::RESULT_ERR_INVALID_SIGNATURE );
124
+ return self ::RESULT_ERR_INVALID_SIGNATURE ;
125
+ }
126
+ // order id should be unique, otherwise the transaction is replayed
127
+ if (OrderPlayInapp::orderIdExists ($ playPurchase ->orderId , $ user )){
128
+ return self ::RESULT_ERR_EXISTING_ORDER_ID ;
74
129
}
75
130
76
131
// First save the order
77
132
$ orderPlayInapp = $ playPurchase ->createDatabaseModel ($ user );
78
133
$ orderPlayInapp ->save ();
79
134
80
- Log::info ("received google payment verification " , [ $ jsonReq , $ playPurchase , $ orderPlayInapp ] );
135
+ Log::info ("received google payment verification " );
81
136
82
137
// Find the product
83
138
// TODO temporary for testing retrieve product name from developer payload, later switch to productId
84
139
$ dp = json_decode ($ playPurchase ->developerPayload );
85
140
$ productName = $ dp ->sku ;
86
-
87
141
$ product = Product::where (["name " => $ productName ])->first ();
142
+
88
143
// $product = Product::where(["name" => $playPurchase->productId])->first();
89
144
if (!$ product ){
90
- return $ this -> getResponse ( self ::RESULT_ERR_INVALID_PRODUCT ) ;
145
+ return self ::RESULT_ERR_INVALID_PRODUCT ;
91
146
}
92
147
93
148
// Issue product
@@ -97,7 +152,7 @@ public function postAndroidPaymentVerification(Request $request)
97
152
// Update order
98
153
$ orderPlayInapp ->update (['license_id ' => $ license ->id ]);
99
154
100
- return $ this -> getResponse ( self ::RESULT_OK ) ;
155
+ return self ::RESULT_OK ;
101
156
}
102
157
103
158
@@ -108,6 +163,23 @@ private function getResponse($code)
108
163
return json_encode ($ response );
109
164
}
110
165
166
+ private function getResponseForMultiplePurchases (array $ allResponseCodes )
167
+ {
168
+ $ response = new \stdClass ();
169
+ $ response ->responseCode = self ::RESULT_MULTIPLE_PURCHASES ;
170
+ $ response ->purchasesResponseCodes = $ allResponseCodes ;
171
+ return json_encode ($ response );
172
+ }
173
+
174
+ /***************************************/
175
+ /************* APPLE *****************/
176
+ /***************************************/
177
+
178
+ /**
179
+ * Main processing method of Apple AppStore transactions
180
+ * @param Request $request
181
+ * @return string
182
+ */
111
183
public function postApplePaymentVerification (Request $ request )
112
184
{
113
185
$ response = new \stdClass ();
0 commit comments