WeChatPay.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Server\Payment;
  3. use App\Model\BaseConfig;
  4. use EasyWeChat\Factory;
  5. use GuzzleHttp\Exception\GuzzleException;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * Class WeChatPay.
  9. *
  10. * @property \EasyWeChat\Payment\Application $app
  11. */
  12. class WeChatPay extends PaymentProvider
  13. {
  14. private $app;
  15. public function init()
  16. {
  17. $pay_config = BaseConfig::get('wechat_config');
  18. // try {
  19. $config = [
  20. 'app_id' => $pay_config['appId'],
  21. 'mch_id' => $pay_config['mchId'],
  22. 'key' => $pay_config['key'], // API 密钥
  23. 'cert_path' => storage_path('app/public/apiclient_cert.pem'), // XXX: 绝对路径!!!!
  24. 'key_path' => storage_path('app/public/apiclient_key.pem'), // XXX: 绝对路径!!!!
  25. 'notify_url' => $pay_config['notifyUrl'] ?? env('APP_URL') . '/api/wxPayNotify', // 你也可以在下单时单独设置来想覆盖它
  26. // 'notify_url' => $pay_config['notifyUrl'], // 你也可以在下单时单独设置来想覆盖它
  27. ];
  28. /*} catch (\Exception $e) {
  29. throw new \Exception('微信支付初始化失败:' . $e->getMessage());
  30. }*/
  31. $this->app = Factory::payment($config);
  32. if (!$this->app) {
  33. throw new \Exception('微信支付初始化失败,请检查配置');
  34. }
  35. if (isset($pay_config['is_sub']) && 1 == $pay_config['is_sub']) {
  36. $this->setSub();
  37. }
  38. }
  39. public function getApp()
  40. {
  41. return $this->app;
  42. }
  43. private function setSub()
  44. {
  45. $subMchId = BaseConfig::get('wechat_config', 'subMchId', '');
  46. $subAppId = BaseConfig::get('wechat_config', 'subAppId', '');
  47. $this->app->setSubMerchant($subMchId, $subAppId);
  48. }
  49. /**
  50. * 统一下单接口.
  51. *
  52. * @param float $money
  53. *
  54. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  55. */
  56. public function pay(string $out_trade_no, string $openId, int $money, string $title = '余额充值')
  57. {
  58. try {
  59. $result = $this->app->order->unify([
  60. 'body' => $title,
  61. 'out_trade_no' => $out_trade_no,
  62. 'total_fee' => $money,
  63. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  64. 'openid' => $openId,
  65. ]);
  66. if ('FAIL' == $result['return_code']) {
  67. return ['code' => 1, 'message' => "【微信支付】{$result['return_msg']}"];
  68. }
  69. $jssdk = $this->app->jssdk;
  70. $config = $jssdk->sdkConfig($result['prepay_id']);
  71. return ['code' => 0, 'data' => $config];
  72. } catch (\Exception $e) {
  73. return ['code' => 1, 'message' => $e->getMessage()];
  74. } catch (GuzzleException $e) {
  75. return ['code' => 2, 'message' => $e->getMessage()];
  76. }
  77. }
  78. /**
  79. * 用户提现.
  80. *
  81. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  82. */
  83. public function cash(string $partner_trade_no, string $open_id, float $money)
  84. {
  85. try {
  86. $result = $this->app->transfer->toBalance([
  87. 'partner_trade_no' => $partner_trade_no, // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
  88. 'openid' => $open_id,
  89. 'check_name' => 'NO_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
  90. // 're_user_name' => $real_name, // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
  91. 'amount' => intval($money), // 企业付款金额,单位为分
  92. 'desc' => '用户提现', // 企业付款操作说明信息。必填
  93. ]);
  94. if ('SUCCESS' == $result['return_code'] && 'SUCCESS' == $result['result_code']) {
  95. return ['code' => 0, 'message' => 'success'];
  96. }
  97. Log::error('微信零钱提现金额:' . $money . '错误:' . json_encode($result));
  98. return ['code' => 3, 'message' => ''];
  99. } catch (\Exception $e) {
  100. return ['code' => 1, 'message' => $e->getMessage()];
  101. } catch (GuzzleException $e) {
  102. return ['code' => 2, 'message' => $e->getMessage()];
  103. }
  104. }
  105. public function payNotify()
  106. {
  107. }
  108. public function refund()
  109. {
  110. // TODO: Implement refund() method.
  111. }
  112. }