用戶在微信中付款成功後,微信服務器會將訂單付款通知推送到開發者在公眾平台網站中設置的回調URL(在開發模式中設置)。該通知是一個merchant_order事件消息。
微信推送的merchant_order事件消息的內容如下。
<xml> <ToUserName><![CDATA[weixin_media1]]></ToUserName> <FromUserName><![CDATA[oDF3iYyVlek46AyTBbMRVV8VZVlI]]></FromUserName> <CreateTime>1398144192</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[merchant_order]]></Event> <OrderId><![CDATA[7197417460812584720]]></OrderId> <OrderStatus>2</OrderStatus> <ProductId><![CDATA[pDF3iYx7KDQVGzB7kDg6Tge5OKFo]]></ProductId> <SkuInfo><![CDATA[10001:1000012;10002:100021]]></SkuInfo> </xml>
其中,OrderId參數是該次交易的訂單ID號。用戶可以根據訂單ID查詢訂單詳情。
根據訂單ID查詢訂單詳情的接口如下。
https:// api.weixin.qq.com/merchant/order/getbyid?access_token=ACCESS_TOKEN
該接口的POST數據格式如下,參數說明如表10-1所示。
{ "order_id": "7197417460812584720" }
表10-1 訂單查詢接口的參數說明
返回數據的格式如下。
{ "errcode": 0, "errmsg": "success", "order": { "order_id": "7197417460812533543", "order_status": 6, "order_total_price": 69, "order_create_time": 1394635817, "order_express_price": 5, "buyer_openid": "oDF3iY17NsDAW4UP2qzJXPsz1S9Q", "buyer_nick": "方倍", "receiver_name": "方倍工作室", "receiver_province": "廣東省", "receiver_city": "深圳市", "receiver_address": "華景路一號南方通信大廈5樓", "receiver_mobile": "123456789", "receiver_phone": "123456789", "product_id": "pDF3iYx7KDQVGzB7kDg6Tge5OKFo", "product_name": "包郵正版 《微信公眾平台開發最佳實踐》 雙十一特惠", "product_price": 1, "product_sku": "10000983:10000995;10001007:10001010", "product_count": 1, "product_img": "http:// img2.paipaiimg.com/00000000/item-52B87243-63CCF66C0 0000000040100003565C1EA.0.300x300.jpg", "delivery_id": "1900659372473", "delivery_company": "059Yunda", "trans_id": "1900000109201404103172199813" } }
上述數據的參數說明如表10-2所示。
表10-2 訂單查詢接口返回內容的參數說明
付款通知在事件消息中的實現代碼如下。
1 // 接收事件消息 2 private function receiveEvent($object) 3 { 4 $content = ""; 5 switch ($object->Event) 6 { 7 case "subscribe": 8 $content = "歡迎關注 "; 9 break; 10 case "CLICK": 11 switch ($object->EventKey) 12 { 13 default: 14 $content = "點擊菜單:".$object->EventKey; 15 break; 16 } 17 break; 18 case "merchant_order": 19 $orderid = strval($object->OrderId); 20 $openid = strval($object->FromUserName); 21 require_once('weixin.class.php'); 22 $weixin = new class_weixin; 23 $orderArr0 = $weixin->get_detail_by_order_id($orderid); 24 $orderArr = $orderArr0["order"]; 25 26 // 客服接口發送 27 $orderInfo = "【訂單信息】\n單號:".$orderArr["order_id"]."\n時間:". date("Y-m-d H:i:s", ($orderArr["order_create_time"])); 28 $goodsInfo = "【商品信息】\n名稱:".$orderArr["product_name"]. 29 "\n總價:¥".($orderArr["product_price"] / 100)." × ".$orderArr["pro duct_count"]." + ¥".($orderArr["order_express_price"] / 100)." = ¥". ($orderArr["order_total_price"] / 100); 30 $buyerInfo = "【買家信息】\n暱稱:".$orderArr["buyer_nick"]. 31 "\n地址:".$orderArr["receiver_province"].$orderArr["receiver_city"]. $orderArr["receiver_zone"].$orderArr["receiver_address"]. 32 "\n姓名:".$orderArr["receiver_name"]." 電話:".((isset($orderArr["rece iver_phone"]) && !empty($orderArr["receiver_phone"]))?($order Arr["receiver_phone"]):($orderArr["receiver_mobile"])); 33 $data = array("title"=>urlencode("訂單通知"), "description"=>"", "picurl"=>"", "url" =>""); 34 $data = array("title"=>urlencode($orderInfo), "description"=>"", "picurl"=>"", "url" =>""); 35 $data = array("title"=>urlencode($goodsInfo), "description"=>"", "picurl"=>$orderArr["product_img"], "url" =>""); 36 $data = array("title"=>urlencode($buyerInfo), "description"=>"", "picurl"=>"", "url" =>""); 37 $result2 = $weixin->send_custom_message($openid, "news", $data); 38 39 $content = ""; 40 break; 41 default: 42 $content = ""; 43 break; 44 45 } 46 if(is_array($content)){ 47 if (isset($content[0])){ 48 $result = $this->transmitNews($object, $content); 49 }else if (isset($content['MusicUrl'])){ 50 $result = $this->transmitMusic($object, $content); 51 } 52 }else{ 53 $result = $this->transmitText($object, $content); 54 } 55 56 return $result; 57 }
上述代碼中和訂單查詢通知相關部分的簡要說明如下。
第18行:判斷是否收到訂單付款通知。
第19~20行:獲取用戶的OpenID及訂單ID。
第21~24行:引用微信小店SDK,根據訂單ID查詢訂單詳情。
第26~37行:將訂單詳情中的內容填充進微信模板消息中並發送。
一個購買成功通知的圖文消息實現效果如圖10-27所示。
圖10-27 購買成功通知