OrderController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Http\Controllers\Guest;
  3. use App\Services\OrderService;
  4. use App\Services\TelegramService;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. use App\Models\Order;
  8. use Library\Epay;
  9. use Omnipay\Omnipay;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Cache;
  12. use Library\BitpayX;
  13. use Library\MGate;
  14. class OrderController extends Controller
  15. {
  16. public function alipayNotify(Request $request)
  17. {
  18. if (!(int)config('v2board.alipay_enable')) {
  19. die('fail');
  20. }
  21. // Log::info('alipayNotifyData: ' . json_encode($_POST));
  22. $gateway = Omnipay::create('Alipay_AopF2F');
  23. $gateway->setSignType('RSA2'); //RSA/RSA2
  24. $gateway->setAppId(config('v2board.alipay_appid'));
  25. $gateway->setPrivateKey(config('v2board.alipay_privkey')); // 可以是路径,也可以是密钥内容
  26. $gateway->setAlipayPublicKey(config('v2board.alipay_pubkey')); // 可以是路径,也可以是密钥内容
  27. $request = $gateway->completePurchase();
  28. $request->setParams($_POST); //Optional
  29. try {
  30. /** @var \Omnipay\Alipay\Responses\AopCompletePurchaseResponse $response */
  31. $response = $request->send();
  32. if ($response->isPaid()) {
  33. /**
  34. * Payment is successful
  35. */
  36. if (!$this->handle($_POST['out_trade_no'], $_POST['trade_no'])) {
  37. abort(500, 'fail');
  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. {
  55. // Log::info('stripeNotifyData: ' . json_encode($request->input()));
  56. if (!(int)config('v2board.stripe_alipay_enable') && !(int)config('v2board.stripe_wepay_enable')) {
  57. die('fail');
  58. }
  59. \Stripe\Stripe::setApiKey(config('v2board.stripe_sk_live'));
  60. try {
  61. $event = \Stripe\Webhook::constructEvent(
  62. file_get_contents('php://input'),
  63. $_SERVER['HTTP_STRIPE_SIGNATURE'],
  64. config('v2board.stripe_webhook_key')
  65. );
  66. } catch (\Stripe\Error\SignatureVerification $e) {
  67. abort(400);
  68. }
  69. switch ($event->type) {
  70. case 'source.chargeable':
  71. $object = $event->data->object;
  72. \Stripe\Charge::create([
  73. 'amount' => $object->amount,
  74. 'currency' => $object->currency,
  75. 'source' => $object->id,
  76. 'metadata' => json_decode($object->metadata, true)
  77. ]);
  78. die('success');
  79. break;
  80. case 'charge.succeeded':
  81. $object = $event->data->object;
  82. if ($object->status === 'succeeded') {
  83. $metaData = isset($object->metadata->out_trade_no) ? $object->metadata : $object->source->metadata;
  84. $tradeNo = $metaData->out_trade_no;
  85. if (!$tradeNo) {
  86. abort(500, 'trade no is not found in metadata');
  87. }
  88. if (!$this->handle($tradeNo, $object->balance_transaction)) {
  89. abort(500, 'fail');
  90. }
  91. die('success');
  92. }
  93. break;
  94. default:
  95. abort(500, 'event is not support');
  96. }
  97. }
  98. public function bitpayXNotify(Request $request)
  99. {
  100. if (!(int)config('v2board.bitpayx_enable')) {
  101. die('fail');
  102. }
  103. $inputString = file_get_contents('php://input', 'r');
  104. // Log::info('bitpayXNotifyData: ' . $inputString);
  105. $inputStripped = str_replace(array("\r", "\n", "\t", "\v"), '', $inputString);
  106. $inputJSON = json_decode($inputStripped, true); //convert JSON into array
  107. $bitpayX = new BitpayX(config('v2board.bitpayx_appsecret'));
  108. $params = [
  109. 'status' => $inputJSON['status'],
  110. 'order_id' => $inputJSON['order_id'],
  111. 'merchant_order_id' => $inputJSON['merchant_order_id'],
  112. 'price_amount' => $inputJSON['price_amount'],
  113. 'price_currency' => $inputJSON['price_currency'],
  114. 'pay_amount' => $inputJSON['pay_amount'],
  115. 'pay_currency' => $inputJSON['pay_currency'],
  116. 'created_at_t' => $inputJSON['created_at_t']
  117. ];
  118. $strToSign = $bitpayX->prepareSignId($inputJSON['merchant_order_id']);
  119. if (!$bitpayX->verify($strToSign, $inputJSON['token'])) {
  120. abort(500, 'sign error');
  121. }
  122. if ($params['status'] !== 'PAID') {
  123. abort(500, 'order is not paid');
  124. }
  125. if (!$this->handle($params['merchant_order_id'], $params['order_id'])) {
  126. abort(500, 'order process fail');
  127. }
  128. die(json_encode([
  129. 'status' => 200
  130. ]));
  131. }
  132. public function mgateNotify(Request $request)
  133. {
  134. if (!(int)config('v2board.mgate_enable')) {
  135. die('fail');
  136. }
  137. $mgate = new MGate(config('v2board.mgate_url'), config('v2board.mgate_app_id'), config('v2board.mgate_app_secret'));
  138. if (!$mgate->verify($request->input())) {
  139. abort(500, 'fail');
  140. }
  141. if (!$this->handle($request->input('out_trade_no'), $request->input('trade_no'))) {
  142. abort(500, 'fail');
  143. }
  144. die('success');
  145. }
  146. public function epayNotify(Request $request)
  147. {
  148. if (!(int)config('v2board.epay_enable')) {
  149. die('fail');
  150. }
  151. $epay = new Epay(config('v2board.epay_url'), config('v2board.epay_pid'), config('v2board.epay_key'));
  152. if (!$epay->verify($request->input())) {
  153. abort(500, 'fail');
  154. }
  155. if (!$this->handle($request->input('out_trade_no'), $request->input('trade_no'))) {
  156. abort(500, 'fail');
  157. }
  158. die('success');
  159. }
  160. private function handle($tradeNo, $callbackNo)
  161. {
  162. $order = Order::where('trade_no', $tradeNo)->first();
  163. if ($order->status === 1) return true;
  164. if (!$order) {
  165. abort(500, 'order is not found');
  166. }
  167. $orderService = new OrderService($order);
  168. if (!$orderService->success($callbackNo)) {
  169. return false;
  170. }
  171. $telegramService = new TelegramService();
  172. $message = sprintf(
  173. "💰成功收款%s元\n———————————————\n订单号:%s",
  174. $order->total_amount / 100,
  175. $order->trade_no
  176. );
  177. $telegramService->sendMessageWithAdmin($message);
  178. return true;
  179. }
  180. }