Zypay.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Illuminate\Support\Facades\Http;
  9. use Illuminate\Support\Facades\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. $key = "6f13dd44826918a5fc7ab6baa255d99c51b505b3";
  28. $data = [
  29. 'app_id' => "WTQwSt5Dytzs",
  30. 'out_trade_no' => $payment->trade_no,
  31. 'total_amount' => $payment->amount * 100,
  32. 'notify_url' => route('payment.notify', ['method' => 'zpay']),
  33. 'return_url' => route('invoice'),
  34. ];
  35. ksort($data);
  36. $str = http_build_query($data);
  37. $data['sign'] = strtolower(md5($str . $key));
  38. $curl = curl_init();
  39. curl_setopt($curl, CURLOPT_URL, "https://api.hmcheckout.com" . '/api/v1/tron');
  40. curl_setopt($curl, CURLOPT_HEADER, 0);
  41. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  42. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  43. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  44. curl_setopt($curl, CURLOPT_POST, 1);
  45. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  46. curl_setopt($curl, CURLOPT_HTTPHEADER, ['User-Agent: Alpha']);
  47. $res = curl_exec($curl);
  48. curl_close($curl);
  49. $result = json_decode($res, true);
  50. if (!$result) {
  51. return Response::json(['status' => 'success', 'url' => "", 'message' => '网络异常!']);
  52. }
  53. Log::info("[河马支付]请求支付接口". var_export($result, true));
  54. if ($result["code"] === 0 ){
  55. return Response::json(['status' => 'fail', 'message' => '创建订单失败!'.$result['error']]);
  56. }
  57. return Response::json(['status' => 'success', 'url' => $result['url'], 'message' => '创建订单成功!']);
  58. }
  59. public function notify(Request $request): void
  60. {
  61. if ($this->verifySign($request->post())){
  62. exit('SIGN FAIL');
  63. }
  64. if ($request->has(['out_trade_no']) && $this->paymentReceived($request->input(['out_trade_no'])) ) {
  65. $this->addPamentCallback($request->input('out_trade_no'),$request->input('trade_no'),"");
  66. Log::info('【河马支付】paymentReceived:'.var_export($request->all(), true));
  67. exit(json_encode(['status' => 200]));
  68. }
  69. }
  70. function verifySign($params)
  71. {
  72. $key = "6e13274fe90055e38f0751c3e242af1882abc849";
  73. $sign = $params['sign'];
  74. unset($params['sign']);
  75. ksort($params);
  76. $str = strtolower(http_build_query($params) . $key);
  77. if ($sign !== md5($str)) {
  78. return false;
  79. }
  80. return $str;
  81. // 剃离sign,sign_type,空值
  82. // unset($data['sign'], $data['sign_type'] , $data["method"]);
  83. // if ($filter) {
  84. // $data = array_filter($data);
  85. // }
  86. // 排序
  87. // ksort($data, SORT_STRING);
  88. // reset($data);
  89. //
  90. //
  91. // $data["key"] = $key;
  92. // return strtoupper(md5(urldecode(http_build_query($data))));
  93. }
  94. }