PaymentService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2024/10/17 14:43
  7. */
  8. namespace App\Http\Services\Frontend\Client\Wechat;
  9. use App\Http\Services\Service;
  10. use App\Models\Member\User;
  11. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  12. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  13. use EasyWeChat\Kernel\Exceptions\RuntimeException;
  14. use EasyWeChat\Pay\Message;
  15. use Exception;
  16. use Faker\Provider\Payment;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\Log;
  19. use Overtrue\LaravelWeChat\EasyWeChat;
  20. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  21. use Throwable;
  22. use EasyWeChat\Pay\Application;
  23. class PaymentService extends Service
  24. {
  25. /**
  26. * @throws TransportExceptionInterface
  27. */
  28. public function payment(array $params)
  29. {
  30. try {
  31. $openid = User::query()->where('id', Auth::id())->value('openid');
  32. $pay = EasyWeChat::pay();
  33. $merchant = $pay->getMerchant();
  34. // $app = EasyWeChat::officialAccount();
  35. // $client = $app->getClient();
  36. $config = EasyWeChat::pay()->getConfig();
  37. $app = new Application($config);
  38. $response = $app->getClient()->postJson("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi", [
  39. "mchid" => (string)$merchant->getMerchantId(), // <---- 请修改为您的商户号
  40. "out_trade_no" => uniqid('native') . rand(1, 1000), // "native12177525012012070352333'.rand(1,1000).'",
  41. "appid" => $app->getConfig()->get('app_id'), // <---- 请修改为服务号的 appid
  42. "description" => "Image形象店-深圳腾大-QQ公仔",
  43. "notify_url" => env('BACKEND_URL') . env('WECHAT_PAYMENT_NOTIFY_URL'),
  44. "amount" => [
  45. "total" => 1,
  46. "currency" => "CNY"
  47. ],
  48. "payer" => [
  49. "openid" => $openid // <---- 请修改为服务号下单用户的 openid
  50. ]
  51. ]);
  52. $app->getValidator()->validate($response->toPsrResponse());
  53. // 验证通过
  54. // dd($response->toArray(false));
  55. $prepayId = $response['prepay_id'];
  56. $sign = [
  57. 'appId' => $config['app_id'], // 公众号 appid
  58. 'timeStamp' => (string)time(), // 时间戳
  59. 'nonceStr' => uniqid(), // 随机字符串
  60. 'package' => 'prepay_id=' . $prepayId, // prepay_id
  61. 'signType' => 'RSA', // 签名类型,默认为 MD5
  62. ];
  63. // 对参数进行排序并拼接成字符串
  64. ksort($sign);
  65. $stringA = urldecode(http_build_query($sign));
  66. $privateKey =file_get_contents( $config['private_key']); // 拼接 key
  67. // 使用 OpenSSL 生成签名
  68. openssl_sign($stringA, $signature, $privateKey, OPENSSL_ALGO_SHA256);
  69. $sign['paySign'] = base64_encode($signature); // 生成签名
  70. return $sign;
  71. } catch (\Exception $e) {
  72. return response()->json(['error' => $e->getMessage()], 500);
  73. }
  74. // $app = EasyWeChat::pay();
  75. //
  76. // $server = $app->getServer();
  77. //// $server->prepend()
  78. // $server->handlePaid(function (Message $message, \Closure $next) use ($app) {
  79. // try {
  80. // $app->getValidator()->validate($app->getRequest());
  81. // // 验证通过,业务处理
  82. // } catch (Exception $e) {
  83. // // 验证失败
  84. // }
  85. // return $next($message);
  86. // });
  87. // return $server->serve();
  88. }
  89. /**
  90. * @throws Throwable
  91. */
  92. public function notify(): false|\Psr\Http\Message\ResponseInterface
  93. {
  94. try {
  95. $pay = EasyWeChat::pay();
  96. $server = $pay->getServer();
  97. $server->handlePaid(function (Message $message, \Closure $next) {
  98. // $message->out_trade_no 获取商户订单号
  99. // $message->payer['openid'] 获取支付者 openid
  100. // 🚨🚨🚨 注意:推送信息不一定靠谱哈,请务必验证
  101. // 建议是拿订单号调用微信支付查询接口,以查询到的订单状态为准
  102. Log::log('success', 'payNotify', (array)$message);
  103. return $next($message);
  104. });
  105. // 默认返回 ['code' => 'SUCCESS', 'message' => '成功']
  106. return $server->serve();
  107. } catch (Exception $e) {
  108. Log::log('error', $e->getMessage());
  109. return false;
  110. }
  111. }
  112. }