OrderController.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. $obj = $event->data->object;
  66. if ($obj['status'] == 'chargeable') {
  67. $trade_no = Redis::get($obj['id']);
  68. if (!$trade_no) {
  69. abort(500, 'redis is not found trade no by stripe source id.');
  70. }
  71. $order = Order::where('trade_no', $trade_no)->first();
  72. if (!$order) {
  73. abort(500, 'order is not found');
  74. }
  75. if ($order->status !== 0) {
  76. die('order is paid');
  77. }
  78. $order->status = 1;
  79. if (!$order->save()) {
  80. abort(500, 'fail');
  81. }
  82. Redis::del($obj['id']);
  83. die('success');
  84. }
  85. }
  86. public function stripeReturn (Request $request) {
  87. Log::info('stripeReturnData: ' . json_encode($request->input()));
  88. header('Location:' . '/#/dashboard');
  89. }
  90. }