php微信移动支付,退款,回调函数

1.支付,回调,退款函数

<?php

// 微信app支付AppId
define("APP_APPID", "");
// 微信app号AppSecret
define("APP_APPSECRET", "");

// <微信支付> app支付商户ID(partnerId)
define("APP_PARTNER", "");
// <微信支付> app商户通加密串(partnerKey)
define("APP_PARTNERKEY", "");

include('./weixinUtils/Util.php');

class wxAppUtils{
    /**
     * 微信生成统一下单
     * @param int $orderId
     */
    public function appWeinXinPay() {
            $utils=new Util();
            $curl=new Curl();
            $notify_url="http://www.weixin.qq.com/wxpay/pay.php";
            $serial_number = date("Ymdhis") . mt_rand(10, 99);
            // 随机字符串
            $nonceStr = $utils->createNoncestr();
            // 时间戳
            $timeStamp = strval(time());
            $totalFee=1;
            $pack = array(
                'out_trade_no' => $serial_number,
                'total_fee' => $totalFee,
                'spbill_create_ip' => $utils->getIps(),
                'appid' => APP_APPID,
                'mch_id' => APP_PARTNER,
                'nonce_str' => $nonceStr,
                'body' => "测试",
                'notify_url' =>$notify_url,
                'trade_type' => 'APP'
            );
            $pack['sign'] =$utils->AppPaySign($pack);
            $xml =$utils->toXML($pack);
            
            $ret = $curl->post('https://api.mch.weixin.qq.com/pay/unifiedorder', $xml);
            
            $postObj = json_decode(json_encode(simplexml_load_string($ret, 'SimpleXMLElement', LIBXML_NOCDATA)));
            
             if (empty($postObj->prepay_id) || $postObj->return_code == "FAIL") {
                  $this->echoFail($postObj->err_code_des);
             } else {
              $packJs = array(
                  'appid'=>APP_APPID,
                  'partnerid'=>APP_PARTNER,
                  'prepayid'=>$postObj->prepay_id,
                  'package'=>"Sign=WXPay",
                  'noncestr'=>$nonceStr,
                  'timestamp'=>$timeStamp,
             );
              $packJs['sign']= $utils->AppPaySign($packJs);
             $this->echoSuccess($packJs);
            }
       
    }
    
    /**
     * 微信移动端退货处理
     * 退货前提是必须支付成功
     * @param type $orderId
     * @return bool
     */
    public function wxRefund($transaction_id,$out_trade_no, $totalFee,$refund_fee) {
        $utils=new Util();
        $curl=new Curl();
        //微信退款
        $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
        $out_refund_no = date("Ymdhis") . mt_rand(10, 99);
        
        $postData = array(
            "appid" => APP_APPID,
            "mch_id" => APP_PARTNER,
            "transaction_id" => $transaction_id,
            "out_trade_no" => $out_trade_no,
            "out_refund_no" =>$out_refund_no,
            "total_fee" => $totalFee,
            "refund_fee" => $refund_fee,
            "op_user_id" => APP_PARTNER,
            "nonce_str" =>  $utils->createNoncestr(),
        );
        $postData["sign"] = $utils->AppPaySign($postData);
        $reqPar =$utils->toXML($postData);
        $r =  $curl->postXmlCurl($url, $reqPar, 50);
        return $r;
        
        
        
    }
    
    // 微信app支付回调页面
    public  function app_payment_notify(){
        $utils=new Util();
        $curl=new Curl();
        //解决$GLOBAL限制导致无法获取xml数据
        $this->sourceStr = file_get_contents('php://input');
        // 读取数据
        $postObj = simplexml_load_string($this->sourceStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        // 数据参考 systemtest/payment_notify.xml
        if (!$postObj) {
            echo "支付回调处理失败,数据包解析失败";
        } else {
            // 对数据包进行签名验证
            $postArr = (array)$postObj;
            $sign    = $utils->AppPaySign($postArr);
            if ($sign == $postObj->sign) {
                $this->app_order_callback($postObj);
            }
    
        }
    }
    
    /**
     * app支付回调
     * @param $postObj
     */
    public function app_order_callback($postObj) {
        $serial = $postObj->out_trade_no;
        // 微信交易单号
        $transaction_id = $postObj->transaction_id;
        if (!empty($transaction_id)) {
            
          //这里写你需要的逻辑
          echo "<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>";
        }
    }
    

 
    /**
     * 输出成功JSON消息
     */
    final public function echoSuccess($msg = 'success') {
        $this->echoMsg(0, $msg);
        exit();
    }
    
    /**
     * 输出JSON消息
     * @param mixed $code
     * @param mixed $msg
     */
    final public function echoMsg($code, $msg = '', $options = JSON_UNESCAPED_UNICODE) {
        return $this->echoJson(array(
            'ret_code' => $code,
            'ret_msg'  => $msg
        ), $options);
    }

   



    /**
     * 输出JSON
     * @param mixed $arr
     */
    final public function echoJson($arr, $options = JSON_UNESCAPED_UNICODE) {
        header('Content-Type: application/json; charset=utf-8');
        if (strpos(PHP_VERSION, '5.3') > -1) {
            // php 5.3-
            echo json_encode($arr);
        } else {
            // php 5.4+
            echo json_encode($arr, $options);
        }
        return true;
    }
    

}



2.测试类

<?php
include('./wxAppUtils.php');

$wxPay =new wxAppUtils();

$out_trade_no="";     //订单号
$wepay_serial="";     //唯一订单号
$totalFee=1;     //订单金额
$refund_fee=1;   //退款金额
$result=$wxPay->wxRefund($wepay_serial, $out_trade_no, $totalFee, $refund_fee);//退款  
if($result['errcode']==0){
    var_dump($result);
}else{
   echo $result['errcode'];
}


百度下载链接:http://pan.baidu.com/s/1jIDttg6 


 密码:2kaj




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值