OrderController.php 5.6 KB

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