123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php namespace App\Servers\WechatPay;
- use DateTime;
- use DateTimeZone;
- use App\Facades\Servers\Logs\Log;
- use WeChatPay\Builder;
- use WeChatPay\Crypto\Rsa;
- use WeChatPay\Util\PemUtil;
- use WeChatPay\Formatter;
- class Transfer
- {
- private $appid;
- private $mchid;
- private $instance;
- private $merchantPrivateKeyInstance;
- private $merchantCertificateSerial;
- function __construct()
- {
- // 从本地文件中加载「商户API私钥」,「商户API私钥」会用来生成请求的签名
- $merchantPrivateKeyFilePath = Config('wechatpay.private_key');
- //$merchantPrivateKeyFilePath = 'file://D:\phpstudy_pro\WWW\mall\kailin\resources\1612111355_20241118_cert\apiclient_key.pem';
- $merchantPrivateKeyInstance = Rsa::from($merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);
- // 「商户API证书」的「证书序列号」
- $merchantCertificateSerial = '4A5F7CFCC3280CF8C09735D5FC2DEFBC9CCD6F46';
- // 从本地文件中加载「微信支付平台证书」或者「微信支付平台公钥」,用来验证微信支付应答的签名
- $platformCertificateOrPublicKeyFilePath = Config('wechatpay.platformCertificate');
- $platformPublicKeyInstance = Rsa::from($platformCertificateOrPublicKeyFilePath, Rsa::KEY_TYPE_PUBLIC);
- // 「微信支付平台证书」的「证书序列号」或者是「微信支付平台公钥ID」
- // 「平台证书序列号」及/或「平台公钥ID」可以从 商户平台 -> 账户中心 -> API安全 直接查询到
- $platformCertificateSerialOrPublicKeyId = 'PUB_KEY_ID_0116121113552024111800218500000334';
- // 构造一个 APIv3 客户端实例
- $instance = Builder::factory([
- 'mchid' => Config('wechatpay.mchid'),
- 'serial' => $merchantCertificateSerial,
- 'privateKey' => $merchantPrivateKeyInstance,
- 'certs' => [
- $platformCertificateSerialOrPublicKeyId => $platformPublicKeyInstance,
- ],
- ]);
- $this->instance = $instance;
- $this->appid = Config('wechatpay.appid');
- $this->mchid = Config('wechatpay.mchid');
- $this->merchantPrivateKeyInstance = $merchantPrivateKeyInstance;
- $this->merchantCertificateSerial = Config('wechat.certificate');
- return;
- }
- /**
- * 拉起小程序支付
- */
- public function pay($params)
- {
- //调用微信JSAPI下单获取预支付交易会话标识
- try {
- $resp = $this->instance
- ->chain('/v3/fund-app/mch-transfer/transfer-bills')
- ->post(['json' => [
- 'appid' => $this->appid,
- 'out_bill_no' => (string)$params['out_bill_no'],
- 'transfer_scene_id' => '1000',
- 'openid' => $params['openid'],
- 'transfer_amount' => (int)($params['amount'] * 100),
- 'transfer_remark' => '余额提现',
- 'user_recv_perception' => '现金奖励',
- 'transfer_scene_report_infos' => [
- [
- 'info_type' => '活动名称',
- 'info_content' => '下单红包',
- ],
- [
- 'info_type' => '奖励说明',
- 'info_content' => '预约下单得现金红包',
- ],
- ],
- 'notify_url' => Config('wechatpay.transfer_notify_url')
- /*'time_expire' => $this->timestampToRfc3339(time() + env('ORDER_OUT_TIME')*60),*/
- ]]);
- $result = json_decode($resp->getBody(),true);
- } catch (\Exception $e) {
- if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
- $r = $e->getResponse();
- // 记录错误信息
- Log::error('wechat/transfer','微信商户转账失败,出现错误'.$r->getBody().'参数'.json_encode($params));
- }
- return false;
- }
- return $result;
- }
- //查询订单
- public function query($params)
- {
- try {
- $promise = $this->instance
- ->chain('v3/pay/transactions/id/'.$params[''])
- ->get(['json' => [
- 'mchid' => $this->mchid,
- ]]);
- } catch (\Exception $e) {
- if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
- $r = $e->getResponse();
- // 记录错误信息
- Log::error('wechat/Payment','查询订单,出现错误'.$r->getBody().'参数'.json_encode($params));
- }
- return response()->json(['error' => '查询订单支付失败'], 500);
- }
- return response()->json($promise);
- }
- /**
- * 订单微信退款
- */
- public function refund($params)
- {
- $notify_url = Config('wechat.course_refund_notify_url');
- try {
- $resp = $this->instance
- ->chain('v3/refund/domestic/refunds')
- ->post(['json' => [
- 'transaction_id' => $params['transaction_id'],
- 'out_refund_no' => $params['out_refund_no'],
- 'reason' => $params['reason'] ?? '',
- 'notify_url' => $notify_url,
- 'amount' => [
- 'refund' => $params['refund'],
- 'total' => $params['total'],
- 'currency' => 'CNY'
- ],
- ]]);
- } catch (\Exception $e) {
- if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
- $r = $e->getResponse();
- Log::error('wechat/Payment','订单微信退款,出现错误'.$r->getBody());
- }
- return response()->json(['error' => '订单微信退款失败'], 500);
- }
- return response()->json($resp);
- }
- //Rfc3339时间日期格式
- public function timestampToRfc3339($timestamp)
- {
- // 转换为DateTime对象
- $dateTime = new DateTime();
- $dateTime->setTimestamp($timestamp);
- // 设置时区,默认为UTC
- $dateTime->setTimezone(new DateTimeZone('UTC'));
- // 格式化为RFC3339
- return $dateTime->format(DateTime::RFC3339);
- }
- }
|