Skip to main content

微信支付 之 通知微信发货接口

<?php
//通知微信发货接口

//通知微信发货接口 — 传入订单号
function wx_fahuo_true($order_sn)
{
//取第三方订单号等信息
$sql = 'SELECT * FROM '.$GLOBALS['ecs']->table('wxpay_log')." where order_sn = '$order_sn'";
$data = $GLOBALS['db']->getRow($sql);
if(!empty($data['openid']) && !empty($data['transid']) && !empty($data['out_trade_no']))
{
//发货通知
wx_delivernotify($data['openid'],$data['transid'],$data['out_trade_no']);
}
}

//通知微信发货接口
//openid 用户id
//transid 交易单号
//out_trade_no 第三方订单号
//deliver_status 发货状态 1表示成功 0表示失败,失败deliver_msg 填上原因
//deliver_msg 发货状态信息

function wx_delivernotify($openid,$transid,$out_trade_no,$deliver_status='1′,$deliver_msg = 'ok')
{
include_once("WxPayHelper.php");

@define(APPID , ""); //appid
@define(APPKEY ,"); //paysign key
@define(SIGNTYPE, ""); //method
@define(PARTNERID,");//通加密串
@define(PARTNERKEY,");//通加密串
@define(APPSERCERT, "");

//1. 获取access token
$appid = APPID;
$appsecret = APPSERCERT;
$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'] = APPKEY;
$obj['openid'] = $openid;
$obj['transid'] = $transid;
$obj['out_trade_no'] = $out_trade_no;
$obj['deliver_timestamp'] = $deliver_timestamp;
$obj['deliver_status'] = $deliver_status;
$obj['deliver_msg'] = $deliver_msg;

$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;
}

?>