OrderController.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, 'ERROR');
  26. }
  27. if ($order->status !== 0) {
  28. die('SUCCESS');
  29. }
  30. $order->status = 1;
  31. if (!$order->save()) {
  32. abort(500, 'ERROR');
  33. }
  34. /**
  35. * Payment is successful
  36. */
  37. die('success'); //The response should be 'success' only
  38. }else{
  39. /**
  40. * Payment is not successful
  41. */
  42. die('fail');
  43. }
  44. } catch (Exception $e) {
  45. /**
  46. * Payment is not successful
  47. */
  48. die('fail');
  49. }
  50. }
  51. public function stripeNotify (Request $request) {
  52. Log::info('stripeNotifyData: ' . json_encode($request->input()));
  53. \Stripe\Stripe::setApiKey(config('v2board.stripe_sk_live'));
  54. try {
  55. $event = \Stripe\Webhook::constructEvent(
  56. file_get_contents('php://input'),
  57. $_SERVER['HTTP_STRIPE_SIGNATURE'],
  58. config('v2board.stripe_webhook_key')
  59. );
  60. } catch (\Stripe\Error\SignatureVerification $e) {
  61. abort(400);
  62. }
  63. $obj = $event->data->object;
  64. if ($obj['status'] == 'succeeded') {
  65. $order = Order::where('callback_no', $obj['source']['id'])->first();
  66. if (!$order) {
  67. abort(500, 'ERROR');
  68. }
  69. if ($order->status !== 0) {
  70. die('SUCCESS');
  71. }
  72. $order->status = 1;
  73. if (!$order->save()) {
  74. abort(500, 'ERROR');
  75. }
  76. die('SUCCESS');
  77. }
  78. }
  79. public function stripeReturn (Request $request) {
  80. Log::info('stripeReturnData: ' . json_encode($request->input()));
  81. header('Location:' . '/#/dashboard');
  82. }
  83. }