CodePay.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Utils\Payments;
  3. use App\Utils\Library\PaymentHelper;
  4. use App\Utils\Library\Templates\Gateway;
  5. use Illuminate\Http\JsonResponse;
  6. use Illuminate\Http\Request;
  7. use Log;
  8. class CodePay implements Gateway
  9. {
  10. public static function metadata(): array
  11. {
  12. return [
  13. 'key' => 'codepay',
  14. 'method' => ['ali', 'qq', 'wechat'],
  15. 'settings' => [
  16. 'codepay_url' => [
  17. 'type' => 'url',
  18. 'placeholder' => 'admin.system.placeholder.codepay_url',
  19. ],
  20. 'codepay_id' => null,
  21. 'codepay_key' => null,
  22. ],
  23. ];
  24. }
  25. public function purchase(Request $request): JsonResponse
  26. {
  27. $payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
  28. $data = [
  29. 'id' => sysConfig('codepay_id'),
  30. 'pay_id' => $payment->trade_no,
  31. 'type' => $request->input('type'), // 1支付宝支付 2QQ钱包 3微信支付
  32. 'price' => $payment->amount,
  33. 'page' => 1,
  34. 'outTime' => 900,
  35. 'notify_url' => route('payment.notify', ['method' => 'codepay']),
  36. 'return_url' => route('invoice.index'),
  37. ];
  38. $data['sign'] = PaymentHelper::aliStyleSign($data, sysConfig('codepay_key'));
  39. $url = sysConfig('codepay_url').http_build_query($data);
  40. $payment->update(['url' => $url]);
  41. return response()->json(['status' => 'success', 'url' => $url, 'message' => trans('user.payment.order_creation.success')]);
  42. }
  43. public function notify(Request $request): void
  44. {
  45. $tradeNo = $request->input('pay_id');
  46. if ($tradeNo && $request->input('pay_no') && PaymentHelper::verify($request->except('method'), sysConfig('codepay_key'), $request->input('sign'), false)) {
  47. if (PaymentHelper::paymentReceived($tradeNo)) {
  48. exit('success');
  49. }
  50. Log::error('【码支付】验签失败:'.var_export($request->all(), true));
  51. } else {
  52. Log::error('【码支付】交易失败:'.var_export($request->all(), true));
  53. }
  54. exit('fail');
  55. }
  56. }