PHP微信支付异步通知回调处理

工作是一种乐趣时,生活是一种享受!工作是一种义务时,生活则是一种苦役。对待平凡的工作要用不平凡的态度去面对。

微信支付异步回调通知类

<?php
/**
 *作者:haodaima.com
 *日期:2020-12-21
 */


class WxPayNotify
{
    protected $mchid;
    protected $appid;
    protected $apiKey;

    /**
     * 构造函数,初始化必要参数
     * WxPayNotify constructor.
     * @param $mchid
     * @param $appid
     * @param $key
     */
    public function __construct($mchid, $appid, $key)
    {
        $this->mchid = $mchid;
        $this->appid = $appid;
        $this->apiKey = $key;
    }

    public function notify()
    {
        $config = array(
            'mch_id' => $this->mchid,
            'appid' => $this->appid,
            'key' => $this->apiKey,
        );
        $postStr = file_get_contents('php://input');
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        if ($postObj === false) {
            die('parse xml error');
        }
        if ($postObj->return_code != 'SUCCESS') {
            die($postObj->return_msg);
        }
        if ($postObj->result_code != 'SUCCESS') {
            die($postObj->err_code);
        }
        $arr = (array)$postObj;
        unset($arr['sign']);
        if (self::getSign($arr, $config['key']) == $postObj->sign) {
            echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
            return $arr;
        }
    }

    /**
     * 获取签名
     */
    public static function getSign($params, $key)
    {
        ksort($params, SORT_STRING);
        $unSignParaString = self::formatQueryParaMap($params, false);
        $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
        return $signStr;
    }
    protected static function formatQueryParaMap($paraMap, $urlEncode = false)
    {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if (null != $v && "null" != $v) {
                if ($urlEncode) {
                    $v = urlencode($v);
                }
                $buff .= $k . "=" . $v . "&";
            }
        }
        $reqPar = '';
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        return $reqPar;
    }
}

调用示例

<?php
/**
 *作者:haodaima.com
 *日期:2020-12-21
 */

/**
 * 异步回调通知
 * 说明:需要在支付文件中(如native.php或者jsapi.php)的填写回调地址。例如:http://www.xxx.com/wx/notify.php
 * 付款成功后,微信服务器会将付款结果通知到该页面
 */

header('Content-type:text/html; Charset=utf-8');
$mchid = 'xxxxx';          //微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送
$appid = 'xxxxx';  //公众号APPID 通过微信支付商户资料审核后邮件发送
$apiKey = 'xxxxx';   //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥
$wxPay = new WxpayService($mchid,$appid,$apiKey);
$result = $wxPay->notify();
if($result){
    //完成你的逻辑
    //例如连接数据库,获取付款金额$result['cash_fee'],获取订单号$result['out_trade_no'],修改数据库中的订单状态等;
    //现金支付金额:$result['cash_fee']
    //订单金额:$result['total_fee']
    //商户订单号:$result['out_trade_no']
    //付款银行:$result['bank_type']
    //货币种类:$result['fee_type']
    //是否关注公众账号:$result['is_subscribe']
    //用户标识:$result['openid']
    //业务结果:$result['result_code']  SUCCESS/FAIL
    //支付完成时间:$result['time_end']  格式为yyyyMMddHHmmss
    //具体详细请看微信文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7&index=8
}else{
    echo 'pay error';
}

到此这篇关于PHP微信支付异步通知回调处理就介绍到这了。再见,也许永远不见,我内心固执地追求,只有我自己看得见,但我希望我没错。更多相关PHP微信支付异步通知回调处理内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
PHP支付宝支付异步回调处理示例

PHP微信支付结果通知与回调策略分析

php对微信支付回调处理的方法

layer.open回调获取弹出层参数的实现方法

php app支付宝回调(异步通知)详解