您的当前位置:首页>全部文章>文章详情

微信支付中商家转账到零钱的解决方案全代码

发表于:2023-05-29 22:28:36浏览:585次TAG: #付款到零钱 #商家转账到零钱

服务端的使用方式

我的这个文件是放到了 extend\utils\core下面 这是封装的一个对象
<?php

namespace utils\core;
use WeChatPay\Builder;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Util\PemUtil;
class Wxtransfer
{
    protected $merchantId;
    protected $KeyFilePath;
    protected $CertificateSerial;
    protected $CertificateFilePath;

    public function __construct($merchantId="",$KeyFilePath="",$CertificateSerial="",$CertificateFilePath=""){
        $this->merchantId = $merchantId;//商户号
        $this->KeyFilePath = $KeyFilePath;//api证书的key.pem
        $this->CertificateSerial = $CertificateSerial;//证书序列号
        $this->CertificateFilePath = $CertificateFilePath;//从本地文件中加载「微信支付平台证书」
    }

    public  function  make()
    {
        // 商户号
        $merchantId = $this->merchantId;
        // 从本地文件中加载「商户API私钥」,「商户API私钥」会用来生成请求的签名
        $merchantPrivateKeyFilePath = $this->KeyFilePath;
        $merchantPrivateKeyInstance = Rsa::from($merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);
        // 「商户API证书」的「证书序列号」
        $merchantCertificateSerial = $this->CertificateSerial;
        // 从本地文件中加载「微信支付平台证书」,用来验证微信支付应答的签名
        $platformCertificateFilePath = $this->CertificateFilePath;
        $platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);
        // 从「微信支付平台证书」中获取「证书序列号」
        $platformCertificateSerial = PemUtil::parseCertificateSerialNo($platformCertificateFilePath);
        // 构造一个 APIv3 客户端实例
        $app = Builder::factory([
            'mchid'      => $merchantId,
            'serial'     => $merchantCertificateSerial,
            'privateKey' => $merchantPrivateKeyInstance,
            'certs'      => [
                $platformCertificateSerial => $platformPublicKeyInstance,
            ],
        ]);
        return $app;
    }

   public function encryptor($msg) {
        $platformCertificateFilePath =$this->CertificateFilePath;;
        $platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);
        $encryptor = static function(string $msg) use ($platformPublicKeyInstance): string {
            return Rsa::encrypt($msg, $platformPublicKeyInstance);
        };

    } 



}

前端的使用方式

use utils\core\Wxtransfer;

 try {
             $info = new Wxtransfer("商户号",'file://cert/apiclient_key.pem','你的证书序列号67D49E79ADFFAF4209F55D4D*****','file://cert/wechatpay_40C98208FECF901C1511A0A362394847D9507410.pem');
            $resp = $info->make()->chain('v3/transfer/batches')
            ->post(['json' => [
                'appid'=>'小程序的appid',
                'out_batch_no'=>'自己生成的单号',
                'batch_name'=>'分销返佣金',
                'batch_remark'=>'分销返佣金备注',
                'total_amount'=>40,//转账的金额必须是所有金额的综合
                'total_num'=>1,
                'transfer_detail_list'=>[
                        ['out_detail_no'=>'自己生成的单号',
                        'transfer_amount'=>40,
                        'transfer_remark'=>'用户的报销',
                        'openid'=>'用户的openid',
                        'user_name'=>$info->encryptor('用户名')
                        ]
                    ]
            ],
            "headers"=>[
                "Wechatpay-Serial"=>$platformCertificateSerial,
                'Accept'           => 'application/json'

                ]

            ]);

            halt($info);

            return return_json("操作成功",2000);
        } catch (\Exception $e) {
            // 进行错误处理
            // echo $e->getMessage(), PHP_EOL;
            if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
                $r = $e->getResponse();
                if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
                    $err_info = json_decode($r->getBody(),true);
                    return return_json($err_info['message']);
                 }

            }
            // echo $e->getTraceAsString(), PHP_EOL;
        }