CodePay.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Models\Payment;
  4. use Auth;
  5. use Illuminate\Http\JsonResponse;
  6. use Response;
  7. class CodePay extends AbstractPayment
  8. {
  9. public function purchase($request): JsonResponse
  10. {
  11. $payment = $this->creatNewPayment(Auth::id(), $request->input('id'), $request->input('amount'));
  12. $data = [
  13. 'id' => sysConfig('codepay_id'),
  14. 'pay_id' => $payment->trade_no,
  15. 'type' => $request->input('type'), //1支付宝支付 2QQ钱包 3微信支付
  16. 'price' => $payment->amount,
  17. 'page' => 1,
  18. 'outTime' => 900,
  19. 'notify_url' => route('payment.notify', ['method' => 'codepay']),
  20. 'return_url' => route('invoice'),
  21. ];
  22. $data['sign'] = $this->aliStyleSign($data, sysConfig('codepay_key'));
  23. $url = sysConfig('codepay_url').http_build_query($data);
  24. $payment->update(['url' => $url]);
  25. return Response::json(['status' => 'success', 'url' => $url, 'message' => '创建订单成功!']);
  26. }
  27. public function notify($request): void
  28. {
  29. $trade_no = $request->input('pay_id');
  30. if ($trade_no && $request->input('pay_no')
  31. && $this->verify($request->except('method'), sysConfig('codepay_key'), $request->input('sign'), false)) {
  32. $payment = Payment::whereTradeNo($trade_no)->first();
  33. if ($payment) {
  34. $ret = $payment->order->update(['status' => 2]);
  35. if ($ret) {
  36. exit('success');
  37. }
  38. }
  39. }
  40. exit('fail');
  41. }
  42. }