用户在微信中付款成功后,微信服务器会将订单付款通知推送到开发者在公众平台网站中设置的回调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 购买成功通知