微信服务商分帐功能开发(PHP)

微信服务商分帐功能开发(PHP)

博客说明php

文章所涉及的资料来自互联网整理和我的总结,意在于我的学习和经验汇总,若有什么地方侵权,请联系本人删除,谢谢!json

设置分帐参数

在调用支付的时候设置参数api

image-20200809140655674

添加分帐参数微信

image-20200809140739741

在JDK中添加get,set方法网络

//分帐
    public function SetProfit_sharing($value)
    {
        $this->values['profit_sharing'] = $value;
    }

    public function GetProfit_sharing()
    {
        return $this->values['profit_sharing'];
    }

    public function IsProfit_sharingSet()
    {
        return array_key_exists('profit_sharing', $this->values);
    }

添加分帐接收方

核心代码

image-20200809155717580

部分参数写在SDK的config文件中app

<?php
/**
 * @name ProfitSharing.php
 * @author tanglei
 * @date 2020/8/9
 */

namespace app\api\service;

use think\facade\Env;
require_once Env::get('root_path'). "extend/WxPay/WxPay.Api.php";

class ProfitSharing
{
    public static function addPro()
    {
        $receiver = [
            "type" => "MERCHANT_ID",
            "account" => "11111",
            "name" => "有限公司",
            "relation_type" => "SERVICE_PROVIDER"
        ];
        $WxOrderData = new \WxPayProfitSharing();
        //子商户号
        $WxOrderData->SetSub_Mch_id('11111');
        $WxOrderData->SetSubAppid(config('base.app_id'));
        $WxOrderData->SetReceiver(json_encode($receiver));
        $wxOrder = \WxPayApi::profitsharingAddReceiver($WxOrderData);
        return $wxOrder;
    }
}

要改一下SDK文件post

在Data的签名里面必须使用HMAC-SHA256学习

public function MakeSignH()
    {
        //签名步骤一:按字典序排序参数
        ksort($this->values);
        $string = $this->ToUrlParams();
        //签名步骤二:在string后加入KEY
		$string = $string . "&key=".WxPayConfig::KEY;
        //签名
        $string = hash_hmac("sha256",$string,WxPayConfig::KEY);
        //签名步骤四:全部字符转为大写
        $result = strtoupper($string);
        return $result;
    }

在Api文件中添加一个方法测试

public static function profitsharingAddReceiver($inputObj, $timeOut = 6)
{
    $url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";

    $inputObj->SetAppid(WxPayConfig::APPID);//公众帐号ID
    $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
    $inputObj->SetNonce_str(self::getNonceStr());//随机字符串

    //签名
    $inputObj->SetSignHash();
    $xml = $inputObj->ToXml();

    $startTimeStamp = self::getMillisecond();//请求开始时间
    $response = self::postXmlCurl($xml, $url, false, $timeOut);
    return $response;
    $result = WxPayResults::Init($response);
    self::reportCostTime($url, $startTimeStamp, $result);//上报请求花费时间
    return $result;
}
测试

image-20200809161118770

分帐

image-20200809173030172

核心代码ui

public static function profitSharing()
    {
        $receivers = [
            "type" => "MERCHANT_ID",
            "account" => "111111",
            "amount" => 2,
            "description" => "分到服务商"
        ];
        $orderNo = self::makeOrderNo();
        $WxOrderData = new \WxPayProfitSharing();
        $WxOrderData->SetSub_Mch_id('11111');
        $WxOrderData->SetTransaction_id('42789709008202008098970721631');
        $WxOrderData->SetOut_order_no($orderNo);
        $WxOrderData->SetSubAppid(config('base.app_id'));
        $WxOrderData->SetReceivers(json_encode($receivers));
        $wxOrder = \WxPayApi::profitsharing($WxOrderData);
        return json($wxOrder);
    }

在Api文件中添加一个方法

public static function profitsharing($inputObj, $timeOut = 6)
    {
        $url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing";

        $inputObj->SetAppid(WxPayConfig::APPID);//公众帐号ID
        $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
        $inputObj->SetNonce_str(self::getNonceStr());//随机字符串

        //签名
        $inputObj->SetSignHash();
        $xml = $inputObj->ToXml();

        $startTimeStamp = self::getMillisecond();//请求开始时间
        $response = self::postXmlCurl($xml, $url, true, $timeOut);
        //return $response;
        $result = WxPayResults::Init($response);
        self::reportCostTime($url, $startTimeStamp, $result);//上报请求花费时间
        return $result;
    }

测试

image-20200809172857613

感谢

微信开放平台

万能的网络

以及勤劳的本身 关注公众号: 归子莫,获取更多的资料,还有更长的学习计划