微信支付分支付免押订单租赁订单thinkphp5

本文详细介绍了微信支付V3版接口的使用方法,包括智慧零售和先享后付等场景下的接口规则,以及如何使用V3版接口进行支付分支付、订单查询等操作。文章还提供了具体的代码示例,帮助开发者快速上手。

智慧零售和先享后付使用微信支付V3版接口规则参见:https://wechatpay-api.gitbook.io/wechatpay-api-v3/

免押租借和免押速住使用微信支付V2版接口规则参见:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=4_3

https://pay.weixin.qq.com/wiki/doc/apiv3/payscore.php?chapter=18_1&index=2

微信支付提供V2和V3这两种接口方式

微信支付分支付免押订单租赁订单thinkphp5  

本地项目路径:application\api\controller\Orderzfbwx.php

详情见QQ网盘:mg/tianjieb.../

    /**
     * https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_1.shtml
     * 微信支付分支付
     * @param string $orderzfb_id
     * @return bool
     */
    public function h5_wx($orderzfb_id='')
    {
        require_once "../extend/wxpay_lib/WxPay.Api.php";
        require_once "../extend/wxpay_lib/WxPay.JsApiPay.php";
        require_once "../extend/wxpay_lib/WxPay.Config.php";

//        $order      = \WxPayApi::getcertificates();
        $input  = [
            'out_order_no' =>'1594286271',//商户订单号
            'service_id'   => '500001',
            'service_introduction' =>'设备租赁',
            'time_range'    =>['start_time'=>date("YmdHis"), 'end_time'=>date("YmdHis",strtotime("+30 day")), ],
            'risk_fund'     =>["name"=> "DEPOSIT", "amount"=> 100,"description"=>"设备租赁的费用"],
            'notify_url'      =>"https://app.tianjiebao.com/api/wx/notify",
            'openid'         => 'oBWgc8x1wEBZPsiYdQXuMJ4',
            'need_user_confirm'=>false,
        ];
        $config     = new \WxPayConfig();
        $order      = \WxPayApi::createrentbill($config, $input);
        echo($order);
        exit;
    }
<?php
require_once "WxPay.Exception.php";
require_once "WxPay.Config.Interface.php";
require_once "WxPay.Data.php";

/**
 * 
 * 接口访问类,包含所有微信支付API列表的封装,类中方法为static方法,
 * 每个接口有默认超时时间(除提交被扫支付为10s,上报超时时间为1s外,其他均为6s)
 * @author widyhu
 *
 */
class WxPayApi
{
    public static $error;
    public static $values;
	/**
	 * 
	 * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
	 * appid、mchid、spbill_create_ip、nonce_str不需要填入
	 * @param WxPayConfigInterface $config  配置对象
	 * @param WxPayUnifiedOrder $inputObj
	 * @param int $timeOut
	 * @throws WxPayException
	 * @return 成功时返回,其他抛异常
	 */
	public static function unifiedOrder($config, $inputObj, $timeOut = 6)
	{
		$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
		//检测必填参数
		if(!$inputObj->IsOut_trade_noSet()) {
			throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
		}else if(!$inputObj->IsBodySet()){
			throw new WxPayException("缺少统一支付接口必填参数body!");
		}else if(!$inputObj->IsTotal_feeSet()) {
			throw new WxPayException("缺少统一支付接口必填参数total_fee!");
		}else if(!$inputObj->IsTrade_typeSet()) {
			throw new WxPayException("缺少统一支付接口必填参数trade_type!");
		}
		
		//关联参数
		if($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()){
			throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
		}
		if($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()){
			throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
		}
		
		//异步通知url未设置,则使用配置文件中的url
		if(!$inputObj->IsNotify_urlSet() && $config->GetNotifyUrl() != ""){
			$inputObj->SetNotify_url($config->GetNotifyUrl());//异步通知url
		}
		
		$inputObj->SetAppid($config->GetAppId());//公众账号ID
		$inputObj->SetMch_id($config->GetMerchantId());//商户号
		$inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);//终端ip	   	    
		$inputObj->SetNonce_str(self::getNonceStr());//随机字符串
		
		//签名
		$inputObj->SetSign($config);
		$xml = $inputObj->ToXml();
		
		$startTimeStamp = self::getMillisecond();//请求开始时间
		$response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
		$result = WxPayResults::Init($config, $response);
		self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
		
		return $result;
	}
	
	/**
	 * 
	 * 查询订单,WxPayOrderQuery中out_trade_no、transaction_id至少填一个
	 * appid、mchid、spbill_create_ip、nonce_str不需要填入
	 * @param WxPayConfigInterface $config  配置对象
	 * @param WxPayOrderQuery $inputObj
	 * @param int $timeOut
	 * @throws WxPayException
	 * @return 成功时返回,其他抛异常
	 */
	public static function orderQuery($config, $inputObj, $timeOut = 6)
	{
		$url = "https://api.mch.weixin.qq.com/pay/orderquery";
		//检测必填参数
		if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
			throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!");
		}
		$inputObj->SetAppid($config->GetAppId());//公众账号ID
		$inputObj->SetMch_id($config->GetMerchantId());//商户号
		$inputObj->SetNonce_str(self::getNonceStr());//随机字符串
		
		$inputObj->SetSign($config);//签名
		$xml = $inputObj->ToXml();
		
		$startTimeStamp = self::getMillisecond();//请求开始时间
		$response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
		$result = WxPayResults::Init($config, $response);
		self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
		
		return $result;
	}

    public static function set_error($error)
    {
        self::$error = $error;
    }

    public static function get_error()
    {
        return self::$error;
    }

    /**
     * 新请求方式。 直接调用当前面的签名 V2
     * https://api.mch.weixin.qq.com/pay/orderquery
     * @param $config
     * @param $values  请求前设置的一些参数,如:$values['out_order_no']  //商户订单号
     * @param int $timeOut
     * @return array
     * @throws WxPayException
     */
    public static function orderQueryNew($config, $values, $timeOut = 6)
    {
        $url = "https://api.mch.weixin.qq.com/pay/orderquery";
        //检测必填参数
        if(!$values['out_order_no']) {
            self::set_error('"订单查询接口中,out_order_no必填!"');
            return false;
        }
        $values['appid']    = $config->GetAppId();
        $values['sign']     = self::MakeSign($config, $values);//生成签名
        self::$values       = $values;
        $xml                = self::ToXml($values);

        $startTimeStamp     = self::getMillisecond();//请求开始时间
        $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
        $result             = WxPayResults::Init($config, $response);//将xml转为array
        self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间

        return $result;
    }

    /**
     * 创建支付分订单API
     * https://wechatpay-api.gitbook.io/wechatpay-api-v3/
     * https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_1.shtml
     * @param $config
     * @param $values
     * @param int $timeOut
     * @return array|bool
     */
    public static function createrentbill($config, $values, $timeOut = 6)
    {
        $url = "https://api.mch.weixin.qq.com/v3/payscore/serviceorder";
        //检测必填参数
        if(!$values['out_order_no']) {
            self::set_error('"订单查询接口中,out_order_no必填!"');
            return false;
        }
        $values['appid']    = $config->GetAppId();


        self::$values       = $values;
        $serial_no      = config("weixin.WXPT_SERIAL_NO");//平台证书序列号
        //生成V3请求 header认证信息
        $header         = self::createAuthorization( $url ,$values, 'POST' );
        $post_data      = json_encode($values , JSON_UNESCAPED_UNICODE);
        //增加平台证书序列号 , 平台证书序列号方法 getcertificates()
        $header[]   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值