关于java:微信服务商分账功能开发PHP

2次阅读

共计 2960 个字符,预计需要花费 8 分钟才能阅读完成。

微信服务商分账性能开发(PHP)

<!– more –>

博客阐明

文章所波及的材料来自互联网整顿和集体总结,意在于集体学习和教训汇总,如有什么中央侵权,请分割自己删除,谢谢!

设置分账参数

在调用领取的时候设置参数

增加分账参数

在 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);
    }

增加分账接管方

外围代码

局部参数写在 SDK 的 config 文件中

<?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 文件

在 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;
}
测试

分账

外围代码

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;
    }

测试

感激

微信开放平台

万能的网络

以及勤奋的本人

正文完
 0