OrderController.php 3.1 KB

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