GooglePayController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\dao\Pay\AccountPayInfo;
  4. use App\dao\Pay\PayController;
  5. use App\Http\Controllers\Controller;
  6. use App\Http\helper\CreateOrder;
  7. use App\Http\logic\api\AppleStorePayLogic;
  8. use App\Http\logic\api\OrderLogic;
  9. use App\Jobs\Order;
  10. use App\Models\Treasure\GameScoreInfo;
  11. use App\Services\OrderServices;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. use Illuminate\Support\Facades\Validator;
  16. class GooglePayController extends Controller
  17. {
  18. public $config = [
  19. 'ruby777.500' => 500,
  20. 'ruby777.5000' => 5000,
  21. 'ruby777.20000' => 20000,
  22. 'com.legends777.coins400' => 400,
  23. 'com.legends777.coins4000' => 4000,
  24. 'com.legends777.coins25000' => 25000,
  25. ];
  26. public function pay_order($userId, $pay_amount, $userName, $userEmail, $userPhone, $GiftsID, $buyIp, $AdId, $eventType)
  27. {
  28. $productId = \Illuminate\Support\Facades\Request::input('productID');
  29. if (!isset($this->config[$productId])) {
  30. return apiReturnFail('unknown product');
  31. }
  32. $pay_amount = $this->config[$productId];
  33. $dao = new AccountPayInfo();
  34. [$userPhone, $userName, $userEmail] = $dao->payInfo($userId);
  35. $pay_amount = (int)$pay_amount;
  36. // 礼包类型验证
  37. $PayVerify = new PayController();
  38. $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
  39. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  40. $this->error = $PayVerify->getError();
  41. return false;
  42. }
  43. if ($pay_amount < 0) {
  44. $this->error = 'Payment error_4';
  45. return apiReturnFail($this->error);
  46. }
  47. $order_sn = CreateOrder::order_sn($userId);
  48. // 生成订单信息
  49. $logic = new OrderLogic();
  50. $amount = $pay_amount ;
  51. $logic->orderCreate(
  52. $order_sn,
  53. $amount,
  54. 'GooglePay',
  55. $userId,
  56. $productId,
  57. $GiftsID,
  58. $AdId,
  59. $eventType
  60. );
  61. return apiReturnSuc([
  62. 'code' => 0,
  63. 'order_sn' => $order_sn,
  64. ]);
  65. }
  66. /**校验谷歌支付
  67. * @param string $receipt 支付返回给客户端的一串json
  68. * @param string $signature 支付后返回给客户端的签名字符串
  69. * @param string $google_public_key 谷歌控制台拿到的公钥
  70. * @return bool
  71. */
  72. public function googlePayVerify(string $receipt,string $signature,string $google_public_key):bool
  73. {
  74. $public_key_handle = openssl_pkey_get_public($google_public_key);
  75. if($public_key_handle===false){
  76. $public_key = "-----BEGIN PUBLIC KEY-----" . PHP_EOL .
  77. chunk_split($google_public_key, 64, PHP_EOL) .
  78. "-----END PUBLIC KEY-----";
  79. $public_key_handle = openssl_pkey_get_public($public_key);
  80. if($public_key_handle===false){
  81. return false;
  82. }
  83. }
  84. $result = openssl_verify($receipt, base64_decode($signature), $public_key_handle, OPENSSL_ALGO_SHA1);
  85. //从内存中释放和指定的 public_key相关联的密钥
  86. openssl_free_key($public_key_handle);
  87. return $result;
  88. }
  89. public function confirm(Request $request)
  90. {
  91. Log::channel('googlePay')->info('app store pay validate request', [
  92. 'request' => $request->all(),
  93. ]);
  94. $receipt = $request->input('receipt');
  95. $signature = $request->input('signature');
  96. $google_public_key="";
  97. $result=$this->googlePayVerify($receipt,$signature,$google_public_key);
  98. $result=true;
  99. $orderSN = $request->input('order_sn');
  100. if ($result === false) {
  101. $data = json_decode($response, true);
  102. if (json_last_error() !== JSON_ERROR_NONE) {
  103. return apiReturnFail(__('messages.api.pay.valid_failed'));
  104. }
  105. if (json_last_error() !== JSON_ERROR_NONE || !isset($data['orderId'])) {
  106. Log::channel('googlePay')->error('curl app server error', [
  107. 'endpoint' => $endpoint,
  108. 'data' => $postData,
  109. 'response' => $response,
  110. ]);
  111. return apiReturnFail(__('messages.api.pay.valid_failed'));
  112. }
  113. return apiReturnFail(__('messages.api.pay.valid_failed'));
  114. }
  115. // 验证成功了
  116. // 查询订单信息
  117. $order = DB::connection('write')->table('agent.dbo.order')
  118. ->where('order_sn', $orderSN)->first();
  119. if (!$order) {
  120. Log::channel('googlePay')->info('订单不存在');
  121. return apiReturnSuc();
  122. }
  123. if (!empty($order->pay_at) || !empty($order->finished_at)) {
  124. Log::channel('googlePay')->info('订单已支付');
  125. return apiReturnSuc();
  126. }
  127. $GiftsID = $order->GiftsID ?: '';
  128. $userID = $order->user_id ?: '';
  129. $AdId = $order->AdId ?: '';
  130. $eventType = $order->eventType ?: '';
  131. $payAmt = (int)($order->amount/100);
  132. $body = [
  133. 'payment_sn' => $result['receipt']['in_app'][0]['transaction_id'],
  134. 'updated_at' => date('Y-m-d H:i:s'),
  135. 'pay_status' => 1,
  136. 'pay_at' => date('Y-m-d H:i:s'),
  137. 'finished_at' => date('Y-m-d H:i:s'),
  138. ];
  139. $apply_data = [
  140. 'order_id' => $order->id,
  141. 'payment_sn' => $body['payment_sn'],
  142. 'payment_code' => 'GooglePay',
  143. 'return' => \GuzzleHttp\json_encode($result),
  144. 'is_error' => 0,
  145. 'created_at' => date('Y-m-d H:i:s'),
  146. 'updated_at' => date('Y-m-d H:i:s'),
  147. ];
  148. // 获取金额
  149. $service = new OrderServices();
  150. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = [0,$payAmt,$payAmt,1,45];
  151. // 增加充值记录
  152. [$Score] = $service->addRecord(
  153. $userID,
  154. $payAmt,
  155. $favorable_price,
  156. $orderSN,
  157. $GiftsID,
  158. $Recharge,
  159. $czReason,
  160. $give,
  161. $cjReason,
  162. $AdId,
  163. $eventType
  164. );
  165. // 成功处理回调
  166. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $orderSN]);
  167. $order_up = DB::connection('write')->table('agent.dbo.order')
  168. ->where('order_sn', $orderSN)
  169. ->update($body);
  170. $apply = DB::connection('write')->table('agent.dbo.payment_apply')
  171. ->insert($apply_data);
  172. if (($order_up && $apply) != true) {
  173. Log::channel('googlePay')->info('订单更新失败');
  174. return apiReturnFail(__('messages.api.pay.valid_failed'));
  175. }
  176. Log::channel('googlePay')->info('success', [
  177. 'order_sn' => $orderSN
  178. ]);
  179. $gameScoreInfo = GameScoreInfo::query()->where('UserID', $userID)->first();
  180. return apiReturnSuc([
  181. 'give' => $give,
  182. 'total' => $gameScoreInfo->Score ?? $give
  183. ]);
  184. }
  185. }