zoukankan      html  css  js  c++  java
  • 微信支付开发(6) 发货通知

    本文介绍微信支付中发货通知功能的实现。

    一、发货通知

    为了更好地跟踪订单的情况,需要第三方在收到最终支付通知之后,调用发货通知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"
    }

    上述内容参数说明如表6-12所示。

    参数

    说明

    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

    签名方法(不计入签名生成);

    表6-12 发货通知参数说明

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

    ====================================================================

    方倍工作室微信公众平台账号关注方法:
    1. 微信通讯录-添加朋友-查找公众号-搜索“方倍工作室”
    2. 微信通讯录-添加朋友-搜号码-输入“pondbaystudio”
    3. 使用微信扫描下面的二维码

  • 相关阅读:
    【leetcode】1295. Find Numbers with Even Number of Digits
    【leetcode】427. Construct Quad Tree
    【leetcode】1240. Tiling a Rectangle with the Fewest Squares
    【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
    【leetcode】1291. Sequential Digits
    【leetcode】1290. Convert Binary Number in a Linked List to Integer
    【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps
    【leetcode】1289. Minimum Falling Path Sum II
    【leetcode】1288. Remove Covered Intervals
    【leetcode】1287. Element Appearing More Than 25% In Sorted Array
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6467414.html
Copyright © 2011-2022 走看看