AppleStorePayController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\logic\api\AppleStorePayLogic;
  5. use App\Jobs\Order;
  6. use App\Models\Treasure\GameScoreInfo;
  7. use App\Services\OrderServices;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Validator;
  12. class AppleStorePayController extends Controller
  13. {
  14. public $config = [
  15. 'ouro777.490' => 500,
  16. 'ouro777.990' => 1000,
  17. 'ouro777.1990' => 2000,
  18. 'vencedor.490' => 500,
  19. 'vencedor.990' => 1000,
  20. 'vencedor.1990' => 2000,
  21. ];
  22. public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIp, $AdId, $eventType)
  23. {
  24. $logic = new AppleStorePayLogic();
  25. $name = \Illuminate\Support\Facades\Request::input('productID');
  26. if (!isset($this->config[$name])) {
  27. return apiReturnFail(__('messages.api.payment.unknown_product'));
  28. }
  29. $payAmt = $this->config[$name];
  30. $res = $logic->pay_order($userId, $payAmt,$name, $userPhone, $userEmail, $userName, $GiftsID, $buyIp, $AdId, $eventType);
  31. if ($res === false) {
  32. return apiReturnFail($logic->getError());
  33. }
  34. return apiReturnSuc($res);
  35. }
  36. public function confirm(Request $request)
  37. {
  38. Log::channel('appleStorePay')->info('app store pay validate request', [
  39. 'request' => $request->all(),
  40. ]);
  41. $validator = Validator::make($request->all(), [
  42. 'is_sandbox' => 'required|in:0,1',
  43. 'receipt' => 'required',
  44. 'order_sn' => 'required'
  45. ]);
  46. if ($validator->fails()) {
  47. return apiReturnFail($validator->errors()->first());
  48. }
  49. $isSandbox = $request->input('is_sandbox');
  50. $receipt = $request->input('receipt');
  51. $orderSN = $request->input('order_sn');
  52. $result = $this->verifyReceipt($receipt, 1);
  53. if ($result === false) {
  54. $result = $this->verifyReceipt($receipt, 0);
  55. if ($result === false) {
  56. return apiReturnFail(['web.payment.valid_failed',__('messages.api.payment.valid_failed')]);
  57. }
  58. }
  59. // 验证成功了
  60. // 查询订单信息
  61. $order = DB::connection('write')->table('agent.dbo.order')
  62. ->where('order_sn', $orderSN)->first();
  63. if (!$order) {
  64. Log::channel('appleStorePay')->info('订单不存在');
  65. return apiReturnSuc();
  66. }
  67. if (!empty($order->pay_at) || !empty($order->finished_at)) {
  68. Log::channel('appleStorePay')->info('订单已支付');
  69. return apiReturnSuc();
  70. }
  71. $GiftsID = $order->GiftsID ?: '';
  72. $userID = $order->user_id ?: '';
  73. $AdId = $order->AdId ?: '';
  74. $eventType = $order->eventType ?: '';
  75. $payAmt = (int)($order->amount/100);
  76. $body = [
  77. 'payment_sn' => $result['receipt']['in_app'][0]['transaction_id'],
  78. 'updated_at' => date('Y-m-d H:i:s'),
  79. 'pay_status' => 1,
  80. 'pay_at' => date('Y-m-d H:i:s'),
  81. 'finished_at' => date('Y-m-d H:i:s'),
  82. ];
  83. $apply_data = [
  84. 'order_id' => $order->id,
  85. 'payment_sn' => $body['payment_sn'],
  86. 'payment_code' => 'AppleStore',
  87. 'return' => \GuzzleHttp\json_encode($result),
  88. 'is_error' => 0,
  89. 'created_at' => date('Y-m-d H:i:s'),
  90. 'updated_at' => date('Y-m-d H:i:s'),
  91. ];
  92. // 获取金额
  93. $service = new OrderServices();
  94. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = [0,$payAmt,$payAmt,1,45];
  95. // 增加充值记录
  96. [$Score] = $service->addRecord(
  97. $userID,
  98. $payAmt,
  99. $favorable_price,
  100. $orderSN,
  101. $GiftsID,
  102. $Recharge,
  103. $czReason,
  104. $give,
  105. $cjReason,
  106. $AdId,
  107. $eventType
  108. );
  109. // 成功处理回调
  110. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $orderSN]);
  111. $order_up = DB::connection('write')->table('agent.dbo.order')
  112. ->where('order_sn', $orderSN)
  113. ->update($body);
  114. $apply = DB::connection('write')->table('agent.dbo.payment_apply')
  115. ->insert($apply_data);
  116. if (($order_up && $apply) != true) {
  117. Log::channel('appleStorePay')->info('订单更新失败');
  118. return apiReturnFail(['web.payment.valid_failed',__('messages.api.payment.valid_failed')]);
  119. }
  120. Log::channel('appleStorePay')->info('success', [
  121. 'order_sn' => $orderSN
  122. ]);
  123. $gameScoreInfo = GameScoreInfo::query()->where('UserID', $userID)->first();
  124. return apiReturnSuc([
  125. 'give' => $give,
  126. 'total' => $gameScoreInfo->Score ?? $give
  127. ]);
  128. }
  129. /**
  130. * 验证苹果收据
  131. * @param $receipt
  132. * @param $isSandbox
  133. * @return \Illuminate\Http\JsonResponse
  134. */
  135. private function verifyReceipt($receipt, $isSandbox)
  136. {
  137. $endpoint = $isSandbox ? 'https://sandbox.itunes.apple.com/verifyReceipt'
  138. : 'https://buy.itunes.apple.com/verifyReceipt';
  139. $postData = json_encode(array('receipt-data' => str_replace(' ', '+', $receipt)));
  140. $ch = curl_init($endpoint);
  141. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  142. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  143. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  144. curl_setopt($ch, CURLOPT_POST, true);
  145. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  146. $response = curl_exec($ch);
  147. $errno = curl_errno($ch);
  148. $errmsg = curl_error($ch);
  149. curl_close($ch);
  150. Log::channel('appleStorePay')->info('curl app server result', [
  151. 'endpoint' => $endpoint,
  152. 'data' => $postData,
  153. 'response' => $response,
  154. 'errmsg' => $errmsg,
  155. ]);
  156. if ($errno != 0) {
  157. Log::channel('appleStorePay')->error('curl app server error', [
  158. 'endpoint' => $endpoint,
  159. 'data' => $postData,
  160. 'response' => $response,
  161. 'errmsg' => $errmsg,
  162. ]);
  163. return false;
  164. }
  165. $data = json_decode($response, true);
  166. if (json_last_error() !== JSON_ERROR_NONE) {
  167. Log::channel('appleStorePay')->error('curl app server error', [
  168. 'endpoint' => $endpoint,
  169. 'data' => $postData,
  170. 'response' => $response,
  171. ]);
  172. return false;
  173. }
  174. if (($data['status'] ?? null) !== 0) {
  175. Log::channel('appleStorePay')->error('app server validated failed', [
  176. 'endpoint' => $endpoint,
  177. 'data' => $postData,
  178. 'response' => $response,
  179. ]);
  180. return false;
  181. }
  182. return $data;
  183. }
  184. }