MachantPay.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace crmeb\payment;
  3. use \Yurun\PaySDK\Weixin\Params\PublicParams;
  4. use \Yurun\PaySDK\Weixin\CompanyPay\Weixin\Pay\Request;
  5. use \Yurun\PaySDK\Weixin\SDK; // TODO: update to V3
  6. use crmeb\services\SystemConfigService;
  7. use EasyWeChat\Core\Exception;
  8. use \think\facade\Log;
  9. use \think\facade\Config;
  10. /**
  11. * 企业付款
  12. *
  13. * 包含微信/支付宝等付款到零钱,付款到银行卡
  14. */
  15. class MachantPay {
  16. /**
  17. * 获得微信支付-付款到零钱/银行卡 API 用到的参数
  18. *
  19. * DEPENDENCIES: 数据库读取
  20. */
  21. protected static function getWeixinParams()
  22. {
  23. // 读取配置
  24. $payment = SystemConfigService::more([
  25. 'pay_routine_appid',
  26. 'pay_routine_mchid',
  27. 'pay_routine_key',
  28. 'pay_routine_client_cert',
  29. 'pay_routine_client_key',
  30. 'pay_weixin_open',
  31. ]);
  32. if (!isset($payment['pay_weixin_open']) || !$payment['pay_weixin_open']) {
  33. throw new Exception('weixin pay is not enabled.', -1);
  34. }
  35. $params = new PublicParams();
  36. $params->appID = $payment['pay_routine_appid'] ?? '';
  37. $params->mch_id = $payment['pay_routine_mchid'] ?? '';
  38. $params->key = $payment['pay_routine_key'] ?? '';
  39. $params->keyPath = realpath('.' . $payment['pay_routine_client_key']);
  40. $params->certPath = realpath('.' . $payment['pay_routine_client_cert']);
  41. return $params;
  42. }
  43. /**
  44. * 付款到微信零钱
  45. *
  46. * 文档见 (https://doc.yurunsoft.com/PaySDK/112)
  47. *
  48. * NOTICE: 本函数只是调用微信 API,并未进行业务逻辑处理。
  49. *
  50. * @param int $openid: wechat user openid
  51. * @param string $trade_no: 付款订单号,平台自定义
  52. * @param int $amount: 金额,单位为分
  53. * @param string $desc: 订单描述
  54. * @param string $realname: 收款放真实姓名, 参数 check_name 为 FORCE_CHECK 时使用。
  55. *
  56. * @return (bool, int, string) (是否成功,错误代码,错误信息)
  57. *
  58. * TODO: 微信付款升级为 V3(https://wechatpay-api.gitbook.io/wechatpay-api-v3/wei-xin-zhi-fu-api-v3-jie-kou-gui-fan)
  59. * 本功能使用 Yurunsoft/PaySDK 也已支持 V3 (2021/11/28),但申请微信支付时未申请微信 V3 相关 Key。等待升级使用 V3 协议,或再做一个函数
  60. */
  61. public static function toWeixin($openid, $trade_no, $amount, $desc='', $realname='')
  62. {
  63. try {
  64. $caller_ip = Config::get('app.server_ip', '127.0.0.1');
  65. $params = self::getWeixinParams();
  66. $sdk = new SDK($params);
  67. $req = new Request();
  68. $req->partner_trade_no = $trade_no;
  69. $req->openid = $openid;
  70. $req->check_name = 'NO_CHECK';
  71. $req->re_user_name = $realname;
  72. $req->amount = intval(bcmul($amount, 100, 0));
  73. $req->desc = $desc;
  74. $req->spbill_create_ip = $caller_ip; // 调用接口的机器IP, 这个可能微信用于验证
  75. $res = $sdk->execute($req);
  76. return [
  77. $sdk->checkResult($res),
  78. $sdk->getErrorCode($res),
  79. $sdk->getError($res),
  80. ];
  81. } catch (\Exception $e) {
  82. Log::warning('exception:' . $e->getMessage());
  83. return [false, $e->getCode(), $e->getMessage()];
  84. }
  85. }
  86. /**
  87. * 通过微信支付付款到银行卡
  88. */
  89. public static function toBankByWeixin()
  90. {
  91. try {
  92. } catch (\Exception $e) {
  93. }
  94. }
  95. /**
  96. * 付款到支付宝
  97. */
  98. public static function toAlipay()
  99. {
  100. }
  101. /**
  102. * 通过支付宝付款到银行卡
  103. */
  104. public static function toBankByAlipay()
  105. {
  106. }
  107. }