THeadPay.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use Auth;
  4. use Http;
  5. use Illuminate\Http\JsonResponse;
  6. use Log;
  7. use Response;
  8. class THeadPay extends AbstractPayment
  9. {
  10. public function purchase($request): JsonResponse
  11. {
  12. $payment = $this->creatNewPayment(Auth::id(), $request->input('id'), $request->input('amount'));
  13. $data = [
  14. 'mchid' => sysConfig('theadpay_mchid'),
  15. 'out_trade_no' => $payment->trade_no,
  16. 'total_fee' => (string) ($payment->amount * 100),
  17. 'notify_url' => route('payment.notify', ['method' => 'theadpay']),
  18. ];
  19. $data['sign'] = $this->sign($data);
  20. Log::error('【平头哥支付】 返 '.sysConfig('theadpay_url'));
  21. $response = Http::post(sysConfig('theadpay_url').'/orders', $data);
  22. if ($response->ok()) {
  23. $result = $response->json();
  24. if ($result['status'] === 'success') {
  25. $payment->update(['qr_code' => 1, 'url' => $result['code_url']]);
  26. return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => '创建订单成功!']);
  27. }
  28. Log::error('【平头哥支付】 返回错误信息:'.$result['message']);
  29. }
  30. Log::alert('【平头哥支付】 支付渠道建立订单出现问题!'.$response->body());
  31. return Response::json(['status' => 'fail', 'message' => '创建在线订单失败,请工单通知管理员!']);
  32. }
  33. private function sign($params)
  34. {
  35. unset($params['sign']);
  36. ksort($params, SORT_STRING);
  37. reset($params);
  38. $params['key'] = sysConfig('theadpay_key');
  39. return strtoupper(md5(http_build_query($params)));
  40. }
  41. public function notify($request): void
  42. {
  43. if ($this->verify_notify($request->post())) {
  44. $tradeNo = $request->input('out_trade_no');
  45. if ($tradeNo) {
  46. if ($this->paymentReceived($tradeNo)) {
  47. exit(200);
  48. }
  49. } else {
  50. Log::error('【平头哥支付】交易失败:订单信息-'.var_export($request->all(), true));
  51. }
  52. }
  53. exit('fail');
  54. }
  55. private function verify_notify($params)
  56. {
  57. return $params['sign'] === $this->sign($params);
  58. }
  59. }