| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace crmeb\payment;
- use \Yurun\PaySDK\Weixin\Params\PublicParams;
- use \Yurun\PaySDK\Weixin\CompanyPay\Weixin\Pay\Request;
- use \Yurun\PaySDK\Weixin\SDK; // TODO: update to V3
- use crmeb\services\SystemConfigService;
- use EasyWeChat\Core\Exception;
- use \think\facade\Log;
- use \think\facade\Config;
- /**
- * 企业付款
- *
- * 包含微信/支付宝等付款到零钱,付款到银行卡
- */
- class MachantPay {
- /**
- * 获得微信支付-付款到零钱/银行卡 API 用到的参数
- *
- * DEPENDENCIES: 数据库读取
- */
- protected static function getWeixinParams()
- {
- // 读取配置
- $payment = SystemConfigService::more([
- 'pay_routine_appid',
- 'pay_routine_mchid',
- 'pay_routine_key',
- 'pay_routine_client_cert',
- 'pay_routine_client_key',
- 'pay_weixin_open',
- ]);
- if (!isset($payment['pay_weixin_open']) || !$payment['pay_weixin_open']) {
- throw new Exception('weixin pay is not enabled.', -1);
- }
- $params = new PublicParams();
- $params->appID = $payment['pay_routine_appid'] ?? '';
- $params->mch_id = $payment['pay_routine_mchid'] ?? '';
- $params->key = $payment['pay_routine_key'] ?? '';
- $params->keyPath = realpath('.' . $payment['pay_routine_client_key']);
- $params->certPath = realpath('.' . $payment['pay_routine_client_cert']);
- return $params;
- }
- /**
- * 付款到微信零钱
- *
- * 文档见 (https://doc.yurunsoft.com/PaySDK/112)
- *
- * NOTICE: 本函数只是调用微信 API,并未进行业务逻辑处理。
- *
- * @param int $openid: wechat user openid
- * @param string $trade_no: 付款订单号,平台自定义
- * @param int $amount: 金额,单位为分
- * @param string $desc: 订单描述
- * @param string $realname: 收款放真实姓名, 参数 check_name 为 FORCE_CHECK 时使用。
- *
- * @return (bool, int, string) (是否成功,错误代码,错误信息)
- *
- * TODO: 微信付款升级为 V3(https://wechatpay-api.gitbook.io/wechatpay-api-v3/wei-xin-zhi-fu-api-v3-jie-kou-gui-fan)
- * 本功能使用 Yurunsoft/PaySDK 也已支持 V3 (2021/11/28),但申请微信支付时未申请微信 V3 相关 Key。等待升级使用 V3 协议,或再做一个函数
- */
- public static function toWeixin($openid, $trade_no, $amount, $desc='', $realname='')
- {
- try {
- $caller_ip = Config::get('app.server_ip', '127.0.0.1');
- $params = self::getWeixinParams();
- $sdk = new SDK($params);
- $req = new Request();
- $req->partner_trade_no = $trade_no;
- $req->openid = $openid;
- $req->check_name = 'NO_CHECK';
- $req->re_user_name = $realname;
- $req->amount = intval(bcmul($amount, 100, 0));
- $req->desc = $desc;
- $req->spbill_create_ip = $caller_ip; // 调用接口的机器IP, 这个可能微信用于验证
- $res = $sdk->execute($req);
-
- return [
- $sdk->checkResult($res),
- $sdk->getErrorCode($res),
- $sdk->getError($res),
- ];
- } catch (\Exception $e) {
- Log::warning('exception:' . $e->getMessage());
- return [false, $e->getCode(), $e->getMessage()];
- }
- }
- /**
- * 通过微信支付付款到银行卡
- */
- public static function toBankByWeixin()
- {
- try {
- } catch (\Exception $e) {
- }
- }
- /**
- * 付款到支付宝
- */
- public static function toAlipay()
- {
- }
- /**
- * 通过支付宝付款到银行卡
- */
- public static function toBankByAlipay()
- {
- }
- }
|