一、發貨通知
為了更好地跟蹤訂單的情況,需要第三方在收到最終支付通知之後,調用發貨通知API告知微信後台該訂單的發貨狀態。
發貨時間限制:虛擬、服務類24小時內,實物類72小時內。
請在收到支付通知後,按時發貨,並使用發貨通知接口將相關信息同步到微信後台。若平台在規定時間內沒有收到,將視作發貨超時處理。
發貨通知API的URL為:
https://api.weixin.qq.com/pay/delivernotify?access_token=xxxxxx
URL中的參數只包含目前微信公眾平台憑證access_token,而發貨通知的真正的數據是放在PostData中的,
格式如下:
{ "appid" : "wwwwb4f85f3a797777", "openid" : "oX99MDgNcgwnz3zFN3DNmo8uwa-w", "transid" : "111112222233333", "out_trade_no" : "555666uuu", "deliver_timestamp" : "1369745073", "deliver_status" : "1", "deliver_msg" : "ok", "app_signature" : "53cca9d47b883bd4a5c85a9300df3da0cb48565c", "sign_method" : "sha1" }
上述內容參數說明如下表所示:
微信公眾平台在校驗ok之後,會返回數據表明是否通知成功,例如:{"errcode":0,"errmsg":"ok"}如果有異常,會在errcode和errmsg描述出來,如果成功errcode就為0。
二、程序實現
程序中的一些參數來自本博客前面的微信支付開發數據。讀者請參照運行
<?php include_once("WxPayHelper.php"); //1. 獲取access token $appid = "wx0000000000000000"; $appsecret = "e76050733ce76050733ce76050733cdd"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $result = https_request($url); $jsoninfo = json_decode($result, true); $access_token = $jsoninfo["access_token"]; //2.准備參數 $deliver_timestamp = time(); //2.1構造最麻煩的app_signature $obj['appid'] = $appid; $obj['appkey'] = "8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6k"; $obj['openid'] = "o0pk9uIVnlY-fJkzFKEbQ6LJ4cFc"; $obj['transid'] = "1218614901201405273313473135"; $obj['out_trade_no'] = "JfuKdiBig4zZnE4n"; $obj['deliver_timestamp'] = $deliver_timestamp; $obj['deliver_status'] = "1"; $obj['deliver_msg'] = "ok"; $WxPayHelper = new WxPayHelper(); //get_biz_sign函數受保護,需要先取消一下,否則會報錯 $app_signature = $WxPayHelper->get_biz_sign($obj); //3. 將構造的json提交給微信服務器,查詢 $jsonmenu = ' { "appid" : "'.$obj['appid'].'", "openid" : "'.$obj['openid'].'", "transid" : "'.$obj['transid'].'", "out_trade_no" : "'.$obj['out_trade_no'].'", "deliver_timestamp" : "'.$deliver_timestamp.'", "deliver_status" : "'.$obj['deliver_status'].'", "deliver_msg" : "'.$obj['deliver_msg'].'", "app_signature" : "'.$app_signature.'", "sign_method" : "sha1" }'; $url = "https://api.weixin.qq.com/pay/delivernotify?access_token=".$access_token; $result = https_request($url, $jsonmenu); var_dump($result); function https_request($url, $data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; }
運行後返回結果
string(27) "{"errcode":0,"errmsg":"ok"}"
以上就是對開發微信支付發貨通知的資料整理,希望能幫到開發微支付的朋友。