OrderController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. 'description' => config('v2board.app_name', 'V2Board') . $source['metadata']['invoice_id'],
  68. ]);
  69. if ($charge['status'] == 'succeeded') {
  70. $trade_no = Cache::get($source['id']);
  71. if (!$trade_no) {
  72. abort(500, 'redis is not found trade no by stripe source id');
  73. }
  74. if (!$this->handle($trade_no, $source['id'])) {
  75. abort(500, 'fail');
  76. }
  77. Cache::forget($source['id']);
  78. die('success');
  79. }
  80. break;
  81. default:
  82. abort(500, 'event is not support');
  83. }
  84. }
  85. public function bitpayXNotify(Request $request)
  86. {
  87. $inputString = file_get_contents('php://input', 'r');
  88. // Log::info('bitpayXNotifyData: ' . $inputString);
  89. $inputStripped = str_replace(array("\r", "\n", "\t", "\v"), '', $inputString);
  90. $inputJSON = json_decode($inputStripped, true); //convert JSON into array
  91. $bitpayX = new BitpayX(config('v2board.bitpayx_appsecret'));
  92. $params = [
  93. 'status' => $inputJSON['status'],
  94. 'order_id' => $inputJSON['order_id'],
  95. 'merchant_order_id' => $inputJSON['merchant_order_id'],
  96. 'price_amount' => $inputJSON['price_amount'],
  97. 'price_currency' => $inputJSON['price_currency'],
  98. 'pay_amount' => $inputJSON['pay_amount'],
  99. 'pay_currency' => $inputJSON['pay_currency'],
  100. 'created_at_t' => $inputJSON['created_at_t']
  101. ];
  102. $strToSign = $bitpayX->prepareSignId($inputJSON['merchant_order_id']);
  103. if (!$bitpayX->verify($strToSign, $inputJSON['token'])) {
  104. abort(500, 'sign error');
  105. }
  106. if ($params['status'] !== 'PAID') {
  107. abort(500, 'order is not paid');
  108. }
  109. if (!$this->handle($params['merchant_order_id'], $params['order_id'])) {
  110. abort(500, 'order process fail');
  111. }
  112. die(json_encode([
  113. 'status' => 200
  114. ]));
  115. }
  116. public function payTaroNotify(Request $request)
  117. {
  118. // Log::info('payTaroNotify: ' . json_encode($request->input()));
  119. $payTaro = new PayTaro(config('v2board.paytaro_app_id'), config('v2board.paytaro_app_secret'));
  120. if (!$payTaro->verify($request->input())) {
  121. abort(500, 'fail');
  122. }
  123. if (!$this->handle($request->input('out_trade_no'), $request->input('trade_no'))) {
  124. abort(500, 'fail');
  125. }
  126. die('success');
  127. }
  128. private function handle($tradeNo, $callbackNo)
  129. {
  130. $order = Order::where('trade_no', $tradeNo)->first();
  131. if (!$order) {
  132. abort(500, 'order is not found');
  133. }
  134. if ($order->status !== 0) {
  135. return true;
  136. }
  137. $order->status = 1;
  138. $order->callback_no = $callbackNo;
  139. return $order->save();
  140. }
  141. }