OrderController.php 5.3 KB

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