微信支付開發發貨通知執行個體_php執行個體

來源:互聯網
上載者:User

一、發貨通知

為了更好地跟蹤訂單的情況,需要第三方在收到最終支付通知之後,調用發貨通知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"}

上述內容參數說明如下表所示:

參數 說明
appid 公眾平台賬戶的AppId;
openid 販買使用者的OpenId,這個已經放在最終支付結果通知的PostData裡了;
transid 交易單號;
out_trade_no 第三方訂單號;
deliver_timestamp 發貨時間戳記,這裡指的是Linux時間戳記;
deliver_status 發貨狀態,1表明成功,0表明失敗,失敗時需要在deliver_msg填上失敗原因;
deliver_msg 發貨狀態資訊,失敗時可以填上UTF8編碼的錯諢提示資訊,比如“該商品已退款”;
app_signature 根據支付簽名(paySign)產生方法中所講的簽名方式產生的,參加簽名欄位為:appid、appkey、openid、transid、out_trade_no、deliver_timestamp、deliver_status、deliver_msg;
sign_method 簽名方法(不計入簽名產生);

微信公眾平台在校正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"}"

以上就是對開發微信支付發貨通知的資料整理,希望能幫到開發微支付的朋友。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.