Zypay.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Models\Payment;
  4. use Auth;
  5. use Illuminate\Http\JsonResponse;
  6. use Illuminate\Http\Request;
  7. use Response;
  8. use Http;
  9. use Log;
  10. class Zypay extends AbstractPayment
  11. {
  12. public function purchase(Request $request): JsonResponse
  13. {
  14. $payment = $this->creatNewPayment(Auth::id(), $request->input('id'), $request->input('amount'));
  15. // switch ($request->input('type')) {
  16. // case 2:
  17. // $type = 'qqpay';
  18. // break;
  19. // case 3:
  20. // $type = 'wxpay';
  21. // break;
  22. // case 1:
  23. // default:
  24. // $type = 'alipay';
  25. // break;
  26. // }
  27. // Log::info("[河马支付]请求金额". var_export($payment->amount, true));
  28. // Log::info("[河马支付]格式化金额". var_export(number_format($payment->amount,2,'.',''), true));
  29. $key = "ef32cf2c4b1a352b7340a2465a563cdc9daaf3af";
  30. $data = [
  31. 'version' => '1.0',
  32. 'customerid' => "11635",
  33. 'sdorderno' => $payment->trade_no,
  34. 'total_fee' => number_format($payment->amount,2,'.',''),
  35. 'notifyurl' => route('payment.notify', ['method' => 'zpay']),
  36. 'returnurl' => route('invoice'),
  37. 'paytype' => 'alipay',
  38. 'bankcode' => '',
  39. 'remark' => '测试',
  40. 'get_code' => 1,
  41. ];
  42. #ksort($data);
  43. // 构建签名字符串
  44. $signStr = 'version=' . $data["version"] .
  45. '&customerid=' . $data["customerid"] .
  46. '&total_fee=' . $data["total_fee"] .
  47. '&sdorderno=' . $data["sdorderno"] .
  48. '&notifyurl=' . $data["notifyurl"] .
  49. '&returnurl=' . $data["returnurl"] .
  50. '&' . $key;
  51. Log::info("[河马支付]原始构建签名字符串". var_export($data, true));
  52. Log::info("[河马支付]构建签名字符串". var_export($signStr, true));
  53. // 计算签名
  54. $data['sign'] = md5($signStr);
  55. Log::info("[河马支付]构建参数". var_export($data, true));
  56. $response = Http::get('http://pay.mssdk.com/apisubmit', $data);
  57. if ($response->ok()) {
  58. $result = $response->json();
  59. Log::error('【河马支付】 返回错误信息:'.var_export($result, true));
  60. if ($result['status'] === 1) {
  61. return Response::json(['status' => 'success', 'url' => $result['page_url'], 'message' => '创建订单成功!']);
  62. }
  63. }
  64. return Response::json(['status' => 'fail', 'message' => '创建在线订单失败,请工单通知管理员!']);
  65. }
  66. public function notify(Request $request): void
  67. {
  68. if ($this->verifySign($request->post())){
  69. exit('SIGN FAIL');
  70. }
  71. if ($request->has(['sdorderno']) && $this->paymentReceived($request->input(['sdorderno'])) ) {
  72. $this->addPamentCallback($request->input('sdorderno'),$request->input('sdpayno'),"");
  73. Log::info('【河马支付】paymentReceived:'.var_export($request->all(), true));
  74. exit(json_encode(['status' => 200]));
  75. }
  76. }
  77. function verifySign($params)
  78. {
  79. $key = "ef32cf2c4b1a352b7340a2465a563cdc9daaf3af";
  80. $sign = $params['sign'];
  81. $data = [
  82. 'customerid' => $params['customerid'],
  83. 'status' => $params['status'],
  84. 'sdpayno' => $params['sdpayno'],
  85. 'sdorderno' => $params['sdorderno'],
  86. 'total_fee' => $params['total_fee'],
  87. 'paytype' => $params['paytype'],
  88. ];
  89. // 构建签名字符串
  90. $signStr = 'customerid=' . $params["customerid"] .
  91. '&status=' . $params["status"] .
  92. '&sdpayno=' . $params["sdpayno"] .
  93. '&sdorderno=' . $params["sdorderno"] .
  94. '&total_fee=' . $params["total_fee"] .
  95. '&paytype=' . $params["paytype"] .
  96. '&' . $key;
  97. //$str = http_build_query($data) . '&' . $key;
  98. if ($sign !== md5($signStr)) {
  99. return false;
  100. }
  101. return true;
  102. }
  103. }