亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
There are too many pitfalls, so I won’t go into details. Let’s go directly to the complete back-end code
Check whether the order payment is completed
Home WeChat Applet Mini Program Development About WeChat Payment Mini Program v3 [With PHP complete backend code]

About WeChat Payment Mini Program v3 [With PHP complete backend code]

Jul 15, 2021 pm 01:44 PM

WeChat Payment Mini Program (v3) - PHP complete back-end code

There are too many pitfalls, so I won’t go into details. Let’s go directly to the complete back-end code

<?php
header(&#39;Content-type:text/html; Charset=utf-8&#39;);
ini_set(&#39;date.timezone&#39;,&#39;Asia/Shanghai&#39;);
$data_s = file_get_contents(&#39;php://input&#39;);
$data_s = json_decode($data_s,true);
//統(tǒng)一下單
function wechartAddOrder($name,$ordernumber,$money,$openid,$timeStamp,$noncestr){
    $url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
    $urlarr = parse_url($url);
    $appid = &#39;小程序APPID&#39;;//appID
    $mchid = &#39;微信支付商戶ID&#39;;//商戶ID
    $xlid = &#39;微信支付公鑰序列號(hào)&#39;;//秘鑰序列號(hào) 可在這個(gè)網(wǎng)址中查詢 https://myssl.com/cert_decode.html
    $data = array();
    $time = $timeStamp;
    $data[&#39;appid&#39;] = $appid;
    $data[&#39;mchid&#39;] = $mchid;
    $data[&#39;description&#39;] = $name;//商品描述
    $data[&#39;out_trade_no&#39;] = $ordernumber;//訂單編號(hào)
    $data[&#39;notify_url&#39;] = "你的域名/你的支付目錄路徑/notify.php";//回調(diào)接口 需根據(jù)自己的情況修改
    $data[&#39;amount&#39;][&#39;total&#39;] = intval($money * 1);//金額 單位 分
    $data[&#39;payer&#39;][&#39;openid&#39;] = $openid;//用戶openID
    $data = json_encode($data); 
    $key = getSign($data,$urlarr[&#39;path&#39;],$noncestr,$time);//簽名
    $token = sprintf(&#39;mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"&#39;,$mchid,$xlid,$noncestr,$time,$key);//頭部信息

    $header  = array(
        &#39;Content-Type:&#39;.&#39;application/json; charset=UTF-8&#39;,
        &#39;Accept:application/json&#39;,
        &#39;User-Agent:*/*&#39;,
        &#39;Authorization: WECHATPAY2-SHA256-RSA2048 &#39;.$token
    );  
    $ret = curl_post_https($url,$data,$header);
    $ret = ltrim($ret,&#39;{"prepay_id":"&#39;);
    $ret = rtrim($ret,&#39;}"&#39;);
    //微信支付(小程序)簽名
    $str = getWechartSign($appid,$timeStamp,$noncestr,&#39;prepay_id=&#39;.$ret);
    $arr = array(&#39;appid&#39;=>$appid,&#39;timestamp&#39;=>$timeStamp,&#39;package&#39;=>&#39;prepay_id=&#39;.$ret,&#39;paySign&#39;=>$str);
    exit(json_encode($arr));
}
$set_body = &#39;支付測(cè)試&#39;;//支付顯示內(nèi)容
$price = &#39;1&#39;;//支付金額
$out_trade_no = $data_s[&#39;out_trade_no&#39;];//訂單號(hào)
$timeStamp = $data_s[&#39;timeStamp&#39;];//時(shí)間戳
$openid = $data_s[&#39;openid&#39;];
$noncestr = $data_s[&#39;nonceStr&#39;];

wechartAddOrder($set_body,$out_trade_no,$price,$openid,$timeStamp,$noncestr);


//微信支付簽名
function getSign($data=array(),$url,$randstr,$time){
    $str = "POST"."\n".$url."\n".$time."\n".$randstr."\n".$data."\n";
    $key = file_get_contents(&#39;apiclient_key.pem&#39;);//在商戶平臺(tái)下載的秘鑰
    $str = getSha256WithRSA($str,$key);
    return $str;
}

//調(diào)起支付的簽名
function getWechartSign($appid,$timeStamp,$noncestr,$prepay_id){
    $str = $appid."\n".$timeStamp."\n".$noncestr."\n".$prepay_id."\n";
    $key = file_get_contents(&#39;apiclient_key.pem&#39;);
    $str = getSha256WithRSA($str,$key);
    return $str;
}

function getSha256WithRSA($content, $privateKey){
    $binary_signature = "";
    $algo = "SHA256";
    openssl_sign($content, $binary_signature, $privateKey, $algo);
    $sign = base64_encode($binary_signature);
    return $sign;
}

/* PHP CURL HTTPS POST */
function curl_post_https($url,$data,$header){ // 模擬提交數(shù)據(jù)函數(shù)
    $curl = curl_init(); // 啟動(dòng)一個(gè)CURL會(huì)話
    curl_setopt($curl, CURLOPT_URL, $url); // 要訪問(wèn)的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 對(duì)認(rèn)證證書來(lái)源的檢查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 從證書中檢查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER[&#39;HTTP_USER_AGENT&#39;]); // 模擬用戶使用的瀏覽器
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動(dòng)跳轉(zhuǎn)
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動(dòng)設(shè)置Referer
    curl_setopt($curl, CURLOPT_POST, 1); // 發(fā)送一個(gè)常規(guī)的Post請(qǐng)求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的數(shù)據(jù)包
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設(shè)置超時(shí)限制防止死循環(huán)
    curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區(qū)域內(nèi)容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回

    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    $tmpInfo = curl_exec($curl); // 執(zhí)行操作
    if (curl_errno($curl)) {
        echo &#39;Errno&#39;.curl_error($curl);//捕抓異常
    }
    curl_close($curl); // 關(guān)閉CURL會(huì)話
    return $tmpInfo; // 返回?cái)?shù)據(jù),json格式
}

Check whether the order payment is completed

header(&#39;Content-type:text/html; Charset=utf-8&#39;);
ini_set(&#39;date.timezone&#39;,&#39;Asia/Shanghai&#39;);
$data_s = file_get_contents(&#39;php://input&#39;);
$data_s = json_decode($data_s,true);
if(empty($data_s[&#39;out_trade_no&#39;])){
    exit;
}

$out_trade_no = $data_s[&#39;out_trade_no&#39;];//訂單號(hào)
$merchant_id = &#39;商戶ID&#39;;//商戶ID
$mch_private_key = file_get_contents(&#39;apiclient_key.pem&#39;);//在商戶平臺(tái)下載的秘鑰
$xlid = &#39;微信支付公鑰序列號(hào)&#39;;//秘鑰序列號(hào) 可在這個(gè)網(wǎng)址中查詢 https://myssl.com/cert_decode.html

$url = &#39;https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/&#39;.$out_trade_no.&#39;?mchid=&#39;.$merchant_id;
$url_parts = parse_url($url);
$http_method = &#39;GET&#39;;
$timestamp = time();
$nonce = md5(time().$out_trade_no);
$body = &#39;&#39;;
$canonical_url = ($url_parts[&#39;path&#39;] . (!empty($url_parts[&#39;query&#39;]) ? "?${url_parts[&#39;query&#39;]}" : ""));
$message =  $http_method."\n".
            $canonical_url."\n".
            $timestamp."\n".
            $nonce."\n".
            $body."\n";
openssl_sign($message, $raw_sign, $mch_private_key, &#39;sha256WithRSAEncryption&#39;);
$sign = base64_encode($raw_sign);

$schema = &#39;WECHATPAY2-SHA256-RSA2048&#39;;
$token = sprintf(&#39;mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"&#39;,$merchant_id, $nonce, $timestamp, $xlid, $sign);
$header  = array(
    &#39;Content-Type:&#39;.&#39;application/json; charset=UTF-8&#39;,
    &#39;Accept:application/json&#39;,
    &#39;User-Agent:*/*&#39;,
    &#39;Authorization: WECHATPAY2-SHA256-RSA2048 &#39;.$token
);  
$ret = curl_get_https($url,$data,$header);

$return_out_trade_no = get_between($ret,&#39;"out_trade_no":"&#39;,&#39;","payer"&#39;);
$return_trade_state = get_between($ret,&#39;trade_state":"&#39;,&#39;","trade_state_desc"&#39;);


$arr = array(&#39;type&#39;=>&#39;success&#39;,&#39;trade_state&#39;=>$return_trade_state,&#39;out_trade_no&#39;=>$return_out_trade_no);
exit(json_encode($arr));

/*
 * php截取指定兩個(gè)字符之間字符串
 * */
function get_between($input, $start, $end) {
 $substr = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1));
 return $substr;
}

/* PHP CURL HTTPS GET */
function curl_get_https($url,$data,$header){ // 模擬提交數(shù)據(jù)函數(shù)
    $curl = curl_init(); // 啟動(dòng)一個(gè)CURL會(huì)話
    curl_setopt($curl, CURLOPT_URL, $url); // 要訪問(wèn)的地址
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER[&#39;HTTP_USER_AGENT&#39;]); // 模擬用戶使用的瀏覽器
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動(dòng)跳轉(zhuǎn)
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動(dòng)設(shè)置Referer
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設(shè)置超時(shí)限制防止死循環(huán)
    curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區(qū)域內(nèi)容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);// 頭部信息
    $tmpInfo = curl_exec($curl); // 執(zhí)行操作
    if (curl_errno($curl)) {
        echo &#39;Errno&#39;.curl_error($curl);//捕抓異常
    }
    curl_close($curl); // 關(guān)閉CURL會(huì)話
    return $tmpInfo; // 返回?cái)?shù)據(jù),json格式
}

The above is the detailed content of About WeChat Payment Mini Program v3 [With PHP complete backend code]. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)