APP 微信支付,服务端处理

#微信支付 
#例:
#   [H5 APP支付的服务端处理 ]
#   [client ios]
#   SDK 下载及处理 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

# 用提交订单点击支付
#https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
#根据ios client 的需求,服务端需要生成签名url 和 callback 返回
require_once PUBLIC_PATH."/WxpayAPI/lib/WxPay.Config.php";
require_once PUBLIC_PATH."/WxpayAPI/lib/WxPay.Api.php";
require_once PUBLIC_PATH."/WxpayAPI/lib/WxPay.Data.php";

public function weChatPay()
{    
    $goods_name = empty($this->goods_name) ? 'NULL' : $this->goods_name;
    $order_sn  = empty($this->order_sn) ? 'NULL' : $this->order_sn;
    $order_amount = empty($this->order_amount) ? 'NULL' : $this->order_amount;
    $versionCode = empty($this->versionCode) ? 'NULL' : $this->versionCode;

        $result = '';

        $input = new WxPayUnifiedOrder();
        $input->SetAppid(WxPayConfig::APPID);            //应用ID
        $input->SetMch_id(WxPayConfig::MCHID);           //商户号
        $input->SetNonce_str(WxPayApi::getNonceStr());   //随机字符串
        $input->SetBody($goods_name);                    //商品描述
        $input->SetOut_trade_no($order_sn);              //订单号
        $input->SetTotal_fee($order_amount * 100);       //订单总价
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetNotify_url("https://www.xxx.com/wxpay_notify.php");#https://www.xxx.com/wxpay_notify.php异步地址
        $input->SetSpbill_create_ip(get_client_ip());    //客户端IP
        $input->SetTrade_type("APP");                    //交易类型

        // 第一次验证签名 获取prepay_id
        $results = WxPayApi::unifiedOrder($input);

        // 需要通过 prepay_id  去调取支付接口
        if (!empty($results)) {

                $wxParam = '';
                $signRes = $this->sign($weChat['prepay_id']);
                //注意   ios 客户端不需要我返回,所以unset() 这得看具体情况
                unset($signRes['package']);
                // 返回给客户端的参数 ,  让客户端唤起微信支付
                $sign_arr = [
                       'partnerid' =>$signRes['partnerid'],
                       'appid' =>$signRes['appid'],
                       'prepayid' =>$signRes['prepayid'],
                       'sign' =>$signRes['sign'],
                       'timestamp' =>$signRes['timestamp'],
                       'noncestr' =>$signRes['noncestr'],
                ];
              return json_encode(array('error'=>0,'result'=>$sign_arr));

        }


    return $result;
}

public function sign($prepay_id = '')
{
     $results = [];
    if (!empty($prepay_id)) {
      $input = new WxPayApp();
      $input->SetAppid(WxPayConfig::APPID);          //应用ID
      $input->SetPartnerid(WxPayConfig::MCHID);      //商户号
      $input->SetNonce_str(WxPayApi::getNonceStr()); //随机字符串
      $input->SetPrepayid($prepayid);                //微信预支付ID
      $input->SetTimeStamp(time());
      $input->SetPackage('Sign=WXPay');
      $results = WxPayApi::appSign($input);   //
      if (empty($results)) {
        return false;
      }

    }
    return $results;

}

#https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_7&index=3
#微信服务返回通知处理[异步处理]
//验证签名,并回应微信。
//对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,
//微信会通过一定的策略(如30分钟共8次)定期重新发起通知,
//尽可能提高通知的成功率,但微信不保证通知最终能成功。


require_once "./WxpayAPI/lib/WxPay.Config.php";
require_once "./WxpayAPI/lib/WxPay.Api.php";
require_once "./WxpayAPI/lib/WxPay.Notify.php";



//接收微信xml参数, 并解析
$xml_data = file_get_contents('php://input', 'r');
//将xml 数据变数组
$data = json_decode(json_encode(simplexml_load_string($xml_data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

//验证签名
$sign = $data['sign'];
unset($data['sign']);
$key = WxPayConfig::KEY;

function MakeSign($data,$key)
{
  //签名步骤一:按字典序排序参数
  ksort($data);
  $string = ToUrlParams($data);
  //签名步骤二:在string后加入KEY
  $string = $string . "&key=".$key;
  //签名步骤三:MD5加密
  $string = md5($string);
  //签名步骤四:所有字符转为大写
  $result = strtoupper($string);
  return $result;
}

function ToUrlParams($params)
{
  $buff = "";
  foreach ($params as $k => $v)
  {
    if($k != "sign" && $v != "" && !is_array($v)){
      $buff .= $k . "=" . $v . "&";
    }
  }

  $buff = trim($buff, "&");
  return $buff;
}


 if(MakeSign($data,$key) == $sign){

     NotifyCheck($data);

 }



//更新业务
function NotifyCheck($data){
  if (!empty($data)) {

      //根据微信返回的订单号获取订单信息
      $orderInfo = 'order_info表下的order_sn = $data["out_trade_no"] ';   //
      if(empty($orderInfo)){
          exit('订单不存在');
      }

      //
      if ($orderInfo['pay_status'] == 0) {

          if (isset($data['result_code']) && $data['result_code'] == 'SUCCESS' && $data['total_fee'] == $orderInfo['order_amount']) {
              /* 修改订单状态为已付款 */
              // 'order_info表下的pay_status = 1'; 

              /*更新支付日志*/
              // pay_log 
              // is_pay = 1
              // 
              // 
              // ----------------------------------------------------------------------
          }
      }

      $notify = new PayNotifyCallBack();
      $notify->Handle(false);
  }

}

#callback 处理  [同步通知跳转到给用户看的页面]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值