WeChatPay.php 4.8 KB

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