快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

app微信支付类封装如何理解-创新互联

app微信支付类封装如何理解,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联公司-专业网站定制、快速模板网站建设、高性价比乌达网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式乌达网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖乌达地区。费用合理售后完善,十载实体公司更值得信赖。

namespace MyClass;

class WechatPay

{

  public function createJsBizPackage($totalFee, $outTradeNo, $orderName, $timestamp){

    $unified = array(

      'appid' => WX_APPID,

      'attach' => '支付',

      'body' => $orderName,

      'mch_id' => WX_MACID,

      'nonce_str' => self::createNonceStr(),

      'notify_url' => WX_URL,

      'out_trade_no' => $outTradeNo,

      'spbill_create_ip' => '127.0.0.1',

      'total_fee' => intval($totalFee * 100),

      'trade_type' => 'APP',

    );

    $unified['sign'] = self::getSign($unified);

    $responseXml = self::curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder', self::arrayToXml($unified));

    $unifiedOrder = (array)simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);

    if ($unifiedOrder === false) {

      die('parse xml error');

    }

    if ($unifiedOrder['return_code'] != 'SUCCESS') {

      die($unifiedOrder['return_msg']);

    }

    if ($unifiedOrder['result_code'] != 'SUCCESS') {

      die($unifiedOrder['err_code']);

    }

    $arr = array(

      "appid" => WX_APPID,

      'partnerid'=>WX_MACID,

      "timestamp" => $timestamp,

      "noncestr" => $unifiedOrder['nonce_str'],

      "package" => 'Sign=WXPay',

      "prepayid"=>$unifiedOrder['prepay_id'],

    );

    $data['paySign'] = self::getSign($arr);

    $data['appId']=WX_APPID;

    $data['timeStamp']=$timestamp;

    $data['nonceStr']=$unifiedOrder['nonce_str'];

    $data['prepay_id']=$unifiedOrder['prepay_id'];

    $data['trade_type']=$unifiedOrder['trade_type'];

    $data['mchid']=WX_MACID;

    return $data;

  }

  public static function curlGet($url = '', $options = array()){

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    if (!empty($options)) {

      curl_setopt_array($ch, $options);

    }

    //https请求 不验证证书和host

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;

  }

  public static function curlPost($url = '', $postData = '', $options = array()){

    if (is_array($postData)) {

      $postData = http_build_query($postData);

    }

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数

    if (!empty($options)) {

      curl_setopt_array($ch, $options);

    }

    //https请求 不验证证书和host

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;

  }

  public static function createNonceStr($length = 16){

    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

    $str = '';

    for ($i = 0; $i<$length; $i++){

      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);

    }

    return $str;

  }

  public static function arrayToXml($arr){

    $xml = "";

    foreach ($arr as $key => $val){

      if (is_numeric($val)) {

        $xml .= "<" . $key . ">" . $val . "";

      } else {

        $xml .= "<" . $key . ">";

      }

    }

    $xml .= "";

    return $xml;

  }

  public static function getSign($params){

    ksort($params, SORT_STRING);

    $unSignParaString = self::formatQueryParaMap($params, false);

    $signStr = strtoupper(md5($unSignParaString . "&key=" . WX_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;

  }

}

看完上述内容,你们掌握app微信支付类封装如何理解的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享文章:app微信支付类封装如何理解-创新互联
URL地址:http://6mz.cn/article/coegsh.html

其他资讯