OrderController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Http\Controllers\Guest;
  3. use App\Services\OrderService;
  4. use App\Services\TelegramService;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. use App\Models\Order;
  8. use Library\Epay;
  9. use Omnipay\Omnipay;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Cache;
  12. use Library\BitpayX;
  13. use Library\MGate;
  14. class OrderController extends Controller
  15. {
  16. public function alipayNotify(Request $request)
  17. {
  18. // Log::info('alipayNotifyData: ' . json_encode($_POST));
  19. $gateway = Omnipay::create('Alipay_AopF2F');
  20. $gateway->setSignType('RSA2'); //RSA/RSA2
  21. $gateway->setAppId(config('v2board.alipay_appid'));
  22. $gateway->setPrivateKey(config('v2board.alipay_privkey')); // 可以是路径,也可以是密钥内容
  23. $gateway->setAlipayPublicKey(config('v2board.alipay_pubkey')); // 可以是路径,也可以是密钥内容
  24. $request = $gateway->completePurchase();
  25. $request->setParams($_POST); //Optional
  26. try {
  27. /** @var \Omnipay\Alipay\Responses\AopCompletePurchaseResponse $response */
  28. $response = $request->send();
  29. if ($response->isPaid()) {
  30. /**
  31. * Payment is successful
  32. */
  33. if (!$this->handle($_POST['out_trade_no'], $_POST['trade_no'])) {
  34. abort(500, 'fail');
  35. }
  36. die('success'); //The response should be 'success' only
  37. } else {
  38. /**
  39. * Payment is not successful
  40. */
  41. die('fail');
  42. }
  43. } catch (Exception $e) {
  44. /**
  45. * Payment is not successful
  46. */
  47. die('fail');
  48. }
  49. }
  50. public function stripeNotify(Request $request)
  51. {
  52. // Log::info('stripeNotifyData: ' . json_encode($request->input()));
  53. \Stripe\Stripe::setApiKey(config('v2board.stripe_sk_live'));
  54. try {
  55. $event = \Stripe\Webhook::constructEvent(
  56. file_get_contents('php://input'),
  57. $_SERVER['HTTP_STRIPE_SIGNATURE'],
  58. config('v2board.stripe_webhook_key')
  59. );
  60. } catch (\Stripe\Error\SignatureVerification $e) {
  61. abort(400);
  62. }
  63. switch ($event->type) {
  64. case 'source.chargeable':
  65. $object = $event->data->object;
  66. \Stripe\Charge::create([
  67. 'amount' => $object->amount,
  68. 'currency' => $object->currency,
  69. 'source' => $object->id,
  70. 'metadata' => json_decode($object->metadata, true)
  71. ]);
  72. die('success');
  73. break;
  74. case 'charge.succeeded':
  75. $object = $event->data->object;
  76. if ($object->status === 'succeeded') {
  77. $metaData = isset($object->metadata->out_trade_no) ? $object->metadata : $object->source->metadata;
  78. $tradeNo = $metaData->out_trade_no;
  79. if (!$tradeNo) {
  80. abort(500, 'trade no is not found in metadata');
  81. }
  82. if (!$this->handle($tradeNo, $object->balance_transaction)) {
  83. abort(500, 'fail');
  84. }
  85. die('success');
  86. }
  87. break;
  88. default:
  89. abort(500, 'event is not support');
  90. }
  91. }
  92. public function bitpayXNotify(Request $request)
  93. {
  94. $inputString = file_get_contents('php://input', 'r');
  95. // Log::info('bitpayXNotifyData: ' . $inputString);
  96. $inputStripped = str_replace(array("\r", "\n", "\t", "\v"), '', $inputString);
  97. $inputJSON = json_decode($inputStripped, true); //convert JSON into array
  98. $bitpayX = new BitpayX(config('v2board.bitpayx_appsecret'));
  99. $params = [
  100. 'status' => $inputJSON['status'],
  101. 'order_id' => $inputJSON['order_id'],
  102. 'merchant_order_id' => $inputJSON['merchant_order_id'],
  103. 'price_amount' => $inputJSON['price_amount'],
  104. 'price_currency' => $inputJSON['price_currency'],
  105. 'pay_amount' => $inputJSON['pay_amount'],
  106. 'pay_currency' => $inputJSON['pay_currency'],
  107. 'created_at_t' => $inputJSON['created_at_t']
  108. ];
  109. $strToSign = $bitpayX->prepareSignId($inputJSON['merchant_order_id']);
  110. if (!$bitpayX->verify($strToSign, $inputJSON['token'])) {
  111. abort(500, 'sign error');
  112. }
  113. if ($params['status'] !== 'PAID') {
  114. abort(500, 'order is not paid');
  115. }
  116. if (!$this->handle($params['merchant_order_id'], $params['order_id'])) {
  117. abort(500, 'order process fail');
  118. }
  119. die(json_encode([
  120. 'status' => 200
  121. ]));
  122. }
  123. public function mgateNotify(Request $request)
  124. {
  125. $mgate = new MGate(config('v2board.mgate_url'), config('v2board.mgate_app_id'), config('v2board.mgate_app_secret'));
  126. if (!$mgate->verify($request->input())) {
  127. abort(500, 'fail');
  128. }
  129. if (!$this->handle($request->input('out_trade_no'), $request->input('trade_no'))) {
  130. abort(500, 'fail');
  131. }
  132. die('success');
  133. }
  134. public function epayNotify(Request $request)
  135. {
  136. $epay = new Epay(config('v2board.epay_url'), config('v2board.epay_pid'), config('v2board.epay_key'));
  137. if (!$epay->verify($request->input())) {
  138. abort(500, 'fail');
  139. }
  140. if (!$this->handle($request->input('out_trade_no'), $request->input('trade_no'))) {
  141. abort(500, 'fail');
  142. }
  143. die('success');
  144. }
  145. private function handle($tradeNo, $callbackNo)
  146. {
  147. $order = Order::where('trade_no', $tradeNo)->first();
  148. if ($order->status === 1) return true;
  149. if (!$order) {
  150. abort(500, 'order is not found');
  151. }
  152. $orderService = new OrderService($order);
  153. if (!$orderService->success($callbackNo)) {
  154. return false;
  155. }
  156. $telegramService = new TelegramService();
  157. $message = sprintf(
  158. "💰成功收款%s元\n———————————————\n订单号:%s",
  159. $order->total_amount / 100,
  160. $order->trade_no
  161. );
  162. $telegramService->sendMessageWithAdmin($message);
  163. return true;
  164. }
  165. }