OrderController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class OrderController extends Controller
  10. {
  11. public function alipayNotify (Request $request) {
  12. Log::info('alipayNotifyData: ' . json_encode($_POST));
  13. $gateway = Omnipay::create('Alipay_AopF2F');
  14. $gateway->setSignType('RSA2'); //RSA/RSA2
  15. $gateway->setAppId(config('v2board.alipay_appid'));
  16. $gateway->setPrivateKey(config('v2board.alipay_privkey')); // 可以是路径,也可以是密钥内容
  17. $gateway->setAlipayPublicKey(config('v2board.alipay_pubkey')); // 可以是路径,也可以是密钥内容
  18. $request = $gateway->completePurchase();
  19. $request->setParams($_POST); //Optional
  20. try {
  21. /** @var \Omnipay\Alipay\Responses\AopCompletePurchaseResponse $response */
  22. $response = $request->send();
  23. if($response->isPaid()){
  24. $order = Order::where('trade_no', $_POST['out_trade_no'])->first();
  25. if (!$order) {
  26. abort(500, 'fail');
  27. }
  28. if ($order->status == 1) {
  29. die('success');
  30. }
  31. $order->status = 1;
  32. $order->callback_no = $_POST['trade_no'];
  33. if (!$order->save()) {
  34. abort(500, 'fail');
  35. }
  36. /**
  37. * Payment is successful
  38. */
  39. die('success'); //The response should be 'success' only
  40. }else{
  41. /**
  42. * Payment is not successful
  43. */
  44. die('fail');
  45. }
  46. } catch (Exception $e) {
  47. /**
  48. * Payment is not successful
  49. */
  50. die('fail');
  51. }
  52. }
  53. public function stripeNotify (Request $request) {
  54. Log::info('stripeNotifyData: ' . json_encode($request->input()));
  55. \Stripe\Stripe::setApiKey(config('v2board.stripe_sk_live'));
  56. try {
  57. $event = \Stripe\Webhook::constructEvent(
  58. file_get_contents('php://input'),
  59. $_SERVER['HTTP_STRIPE_SIGNATURE'],
  60. config('v2board.stripe_webhook_key')
  61. );
  62. } catch (\Stripe\Error\SignatureVerification $e) {
  63. abort(400);
  64. }
  65. switch ($event->type) {
  66. case 'source.chargeable':
  67. $source = $event->data->object;
  68. $charge = \Stripe\Charge::create([
  69. 'amount' => $source['amount'],
  70. 'currency' => $source['currency'],
  71. 'source' => $source['id'],
  72. ]);
  73. if ($charge['status'] == 'succeeded') {
  74. $trade_no = Redis::get($source['id']);
  75. if (!$trade_no) {
  76. abort(500, 'redis is not found trade no by stripe source id.');
  77. }
  78. $order = Order::where('trade_no', $trade_no)->first();
  79. if (!$order) {
  80. abort(500, 'order is not found');
  81. }
  82. if ($order->status !== 0) {
  83. die('order is paid');
  84. }
  85. $order->status = 1;
  86. $order->callback_no = $source['id'];
  87. if (!$order->save()) {
  88. abort(500, 'fail');
  89. }
  90. Redis::del($source['id']);
  91. die('success');
  92. }
  93. break;
  94. default:
  95. abort(500, 'event is not support');
  96. }
  97. }
  98. public function stripeReturn (Request $request) {
  99. Log::info('stripeReturnData: ' . json_encode($request->input()));
  100. header('Location:' . '/#/dashboard');
  101. }
  102. }