OrderController.php 5.0 KB

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