微信服務商分帳功能開發(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

感謝

微信開放平臺

萬能的網絡

以及勤勞的本身 關注公衆號: 歸子莫,獲取更多的資料,還有更長的學習計劃

相關文章
相關標籤/搜索