OrderController.php 5.1 KB

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