wemall-mobile是基于WeMall的Android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改。本文分享Native(原生)支付模式一demo,供技术员参考学习。

 

wemall官网地址:http://www.wemallshop.com

 

模式一:商户按固定格式生成链接二维码,用户扫码后调微信

 * 会将productid和用户openid发送到商户设置的链接上,商户收到

 * 请求生成订单,调用统一支付接口下单提交到微信,微信会返回

 * 给商户prepayid。

 * 本例程对应的二维码由native_call_qrcode.php生成;

 * 本例程对应的响应服务为native_call.php;

 

 * 需要两者配合使用。

 

native_call.php

 

点击(此处)折叠或打开

  1. <?php
  2.     include_once("./log_.php");
  3.     include_once("../WxPayPubHelper/WxPayPubHelper.php");
  4.     
  5.     //以log文件形式记录回调信息,用于调试
  6.     $log_ = new Log_();
  7.     $log_name="./native_call.log";

  8.     //使用native通知接口
  9.     $nativeCall = new NativeCall_pub();

  10.     //接收微信请求
  11.     $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
  12.     $log_->log_result($log_name,"【接收到的native通知】:\n".$xml."\n");
  13.     $nativeCall->saveData($xml);
  14.     
  15.     if($nativeCall->checkSign() == FALSE){
  16.         $nativeCall->setReturnParameter("return_code","FAIL");//返回状态码
  17.         $nativeCall->setReturnParameter("return_msg","签名失败");//返回信息
  18.     }else{
  19.      //提取product_id
  20.         $product_id = $nativeCall->getProductId();
  21.         
  22.         //使用统一支付接口
  23.         $unifiedOrder = new UnifiedOrder_pub();
  24.         
  25.         //根据不同的$product_id设定对应的下单参数,此处只举例一种
  26.         switch ($product_id)
  27.         {
  28.             case WxPayConf_pub::APPID."static"://与native_call_qrcode.php中的静态链接二维码对应
  29.                 //设置统一支付接口参数
  30.                 //设置必填参数
  31.                 //appid已填,商户无需重复填写
  32.                 //mch_id已填,商户无需重复填写
  33.                 //noncestr已填,商户无需重复填写
  34.                 //spbill_create_ip已填,商户无需重复填写
  35.                 //sign已填,商户无需重复填写
  36.                 $unifiedOrder->setParameter("body","贡献一分钱");//商品描述
  37.                 //自定义订单号,此处仅作举例
  38.                 $timeStamp = time();
  39.                 $out_trade_no = WxPayConf_pub::APPID."$timeStamp";
  40.                 $unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号             $unifiedOrder->setParameter("product_id","$product_id");//商品ID
  41.                 $unifiedOrder->setParameter("total_fee","1");//总金额
  42.                 $unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址
  43.                 $unifiedOrder->setParameter("trade_type","NATIVE");//交易类型
  44.                 $unifiedOrder->setParameter("product_id","$product_id");//用户标识
  45.                 //非必填参数,商户可根据实际情况选填
  46.                 //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号
  47.                 //$unifiedOrder->setParameter("device_info","XXXX");//设备号
  48.                 //$unifiedOrder->setParameter("attach","XXXX");//附加数据
  49.                 //$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
  50.                 //$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间
  51.                 //$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记
  52.                 //$unifiedOrder->setParameter("openid","XXXX");//用户标识
  53.                 
  54.                 //获取prepay_id
  55.                 $prepay_id = $unifiedOrder->getPrepayId();
  56.                 //设置返回码
  57.                 //设置必填参数
  58.                 //appid已填,商户无需重复填写
  59.                 //mch_id已填,商户无需重复填写
  60.                 //noncestr已填,商户无需重复填写
  61.                 //sign已填,商户无需重复填写
  62.                 $nativeCall->setReturnParameter("return_code","SUCCESS");//返回状态码
  63.                 $nativeCall->setReturnParameter("result_code","SUCCESS");//业务结果
  64.                 $nativeCall->setReturnParameter("prepay_id","$prepay_id");//预支付ID
  65.                 
  66.                 break;
  67.             default:
  68.                 //设置返回码
  69.                 //设置必填参数
  70.                 //appid已填,商户无需重复填写
  71.                 //mch_id已填,商户无需重复填写
  72.                 //noncestr已填,商户无需重复填写
  73.                 //sign已填,商户无需重复填写
  74.                 $nativeCall->setReturnParameter("return_code","SUCCESS");//返回状态码
  75.                 $nativeCall->setReturnParameter("result_code","FAIL");//业务结果
  76.                 $nativeCall->setReturnParameter("err_code_des","此商品无效");//业务结果
  77.                 break;
  78.         }

  79.     }
  80.     
  81.     //将结果返回微信
  82.     $returnXml = $nativeCall->returnXml();
  83.     $log_->log_result($log_name,"【返回微信的native响应】:\n".$returnXml."\n");

  84.     echo $returnXml;
  85.     
  86.     //交易完成

  87. ?>

log_.php

点击(此处)折叠或打开

  1. <?php

  2. class Log_
  3. {
  4.     // 打印log
  5.     function log_result($file,$word)
  6.     {
  7.      $fp = fopen($file,"a");
  8.      flock($fp, LOCK_EX) ;
  9.      fwrite($fp,"执行日期:".strftime("%Y-%m-%d-%H:%M:%S",time())."\n".$word."\n\n");
  10.      flock($fp, LOCK_UN);
  11.      fclose($fp);
  12.     }
  13. }

  14. ?>

 

WxPayPubHelper.php

点击(此处)折叠或打开

  1. <?php
  2. /**
  3.  * 微信支付帮助库
  4.  * ====================================================
  5.  * 接口分三种类型:
  6.  * 【请求型接口】--Wxpay_client_
  7.  *         统一支付接口类--UnifiedOrder
  8.  *         订单查询接口--OrderQuery
  9.  *         退款申请接口--Refund
  10.  *         退款查询接口--RefundQuery
  11.  *         对账单接口--DownloadBill
  12.  *         短链接转换接口--ShortUrl
  13.  * 【响应型接口】--Wxpay_server_
  14.  *         通用通知接口--Notify
  15.  *         Native支付——请求商家获取商品信息接口--NativeCall
  16.  * 【其他】
  17.  *         静态链接二维码--NativeLink
  18.  *         JSAPI支付--JsApi
  19.  * =====================================================
  20.  * 【CommonUtil】常用工具:
  21.  *         trimString(),设置参数时需要用到的字符处理函数
  22.  *         createNoncestr(),产生随机字符串,不长于32位
  23.  *         formatBizQueryParaMap(),格式化参数,签名过程需要用到
  24.  *         getSign(),生成签名
  25.  *         arrayToXml(),array转xml
  26.  *         xmlToArray(),xml转 array
  27.  *         postXmlCurl(),以post方式提交xml到对应的接口url
  28.  *         postXmlSSLCurl(),使用证书,以post方式提交xml到对应的接口url
  29. */
  30.     include_once("SDKRuntimeException.php");
  31.     include_once("WxPay.pub.config.php");

  32. /**
  33.  * 所有接口的基类
  34.  */
  35. class Common_util_pub
  36. {
  37.     function __construct() {
  38.     }

  39.     function trimString($value)
  40.     {
  41.         $ret = null;
  42.         if (null != $value)
  43.         {
  44.             $ret = $value;
  45.             if (strlen($ret) == 0)
  46.             {
  47.                 $ret = null;
  48.             }
  49.         }
  50.         return $ret;
  51.     }
  52.     
  53.     /**
  54.      *     作用:产生随机字符串,不长于32位
  55.      */
  56.     public function createNoncestr( $length = 32 )
  57.     {
  58.         $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  59.         $str ="";
  60.         for ( $i = 0; $i < $length; $i++ ) {
  61.             $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  62.         }
  63.         return $str;
  64.     }
  65.     
  66.     /**
  67.      *     作用:格式化参数,签名过程需要使用
  68.      */
  69.     function formatBizQueryParaMap($paraMap, $urlencode)
  70.     {
  71.         $buff = "";
  72.         ksort($paraMap);
  73.         foreach ($paraMap as $k => $v)
  74.         {
  75.          if($urlencode)
  76.          {
  77.              $v = urlencode($v);
  78.             }
  79.             //$buff .= strtolower($k) . "=" . $v . "&";
  80.             $buff .= $k . "=" . $v . "&";
  81.         }
  82.         $reqPar;
  83.         if (strlen($buff) > 0)
  84.         {
  85.             $reqPar = substr($buff, 0, strlen($buff)-1);
  86.         }
  87.         return $reqPar;
  88.     }
  89.     
  90.     /**
  91.      *     作用:生成签名
  92.      */
  93.     public function getSign($Obj)
  94.     {
  95.         foreach ($Obj as $k => $v)
  96.         {
  97.             $Parameters[$k] = $v;
  98.         }
  99.         //签名步骤一:按字典序排序参数
  100.         ksort($Parameters);
  101.         $String = $this->formatBizQueryParaMap($Parameters, false);
  102.         //echo '【string1】'.$String.'
    ';

  103.         //签名步骤二:在string后加入KEY
  104.         $String = $String."&key=".WxPayConf_pub::KEY;
  105.         //echo "【string2】".$String."
    ";

  106.         //签名步骤三:MD5加密
  107.         $String = md5($String);
  108.         //echo "【string3】 ".$String."
    ";

  109.         //签名步骤四:所有字符转为大写
  110.         $result_ = strtoupper($String);
  111.         //echo "【result】 ".$result_."
    ";

  112.         return $result_;
  113.     }
  114.     
  115.     /**
  116.      *     作用:array转xml
  117.      */
  118.     function arrayToXml($arr)
  119.     {
  120.         $xml = "";
  121.         return $xml;
  122.     }
  123.     
  124.     /**
  125.      *     作用:将xml转为array
  126.      */
  127.     public function xmlToArray($xml)
  128.     {        
  129.         //将XML转为array
  130.         $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);        
  131.         return $array_data;
  132.     }

  133.     /**
  134.      *     作用:以post方式提交xml到对应的接口url
  135.      */
  136.     public function postXmlCurl($xml,$url,$second=30)
  137.     {        
  138.         //初始化curl
  139.            $ch = curl_init();
  140.         //设置超时
  141.         curl_setopt($ch, CURLOP_TIMEOUT, $second);
  142.         //这里设置代理,如果有的话
  143.         //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
  144.         //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
  145.         curl_setopt($ch,CURLOPT_URL, $url);
  146.         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  147.         curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  148.         //设置header
  149.         curl_setopt($ch, CURLOPT_HEADER, FALSE);
  150.         //要求结果为字符串且输出到屏幕上
  151.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  152.         //post提交方式
  153.         curl_setopt($ch, CURLOPT_POST, TRUE);
  154.         curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  155.         //运行curl
  156.         $data = curl_exec($ch);
  157.         curl_close($ch);
  158.         //返回结果
  159.         if($data)
  160.         {
  161.             curl_close($ch);
  162.             return $data;
  163.         }
  164.         else
  165.         {
  166.             $error = curl_errno($ch);
  167.             echo "curl出错,错误码:$error"."
    "
    ;
  168.             echo "
    "
    ;
  169.             curl_close($ch);
  170.             return false;
  171.         }
  172.     }

  173.     /**
  174.      *     作用:使用证书,以post方式提交xml到对应的接口url
  175.      */
  176.     function postXmlSSLCurl($xml,$url,$second=30)
  177.     {
  178.         $ch = curl_init();
  179.         //超时时间
  180.         curl_setopt($ch,CURLOPT_TIMEOUT,$second);
  181.         //这里设置代理,如果有的话
  182.         //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
  183.         //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
  184.         curl_setopt($ch,CURLOPT_URL, $url);
  185.         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  186.         curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  187.         //设置header
  188.         curl_setopt($ch,CURLOPT_HEADER,FALSE);
  189.         //要求结果为字符串且输出到屏幕上
  190.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
  191.         //设置证书
  192.         //使用证书:cert 与 key 分别属于两个.pem文件
  193.         //默认格式为PEM,可以注释
  194.         curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  195.         curl_setopt($ch,CURLOPT_SSLCERT, WxPayConf_pub::SSLCERT_PATH);
  196.         //默认格式为PEM,可以注释
  197.         curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
  198.         curl_setopt($ch,CURLOPT_SSLKEY, WxPayConf_pub::SSLKEY_PATH);
  199.         //post提交方式
  200.         curl_setopt($ch,CURLOPT_POST, true);
  201.         curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
  202.         $data = curl_exec($ch);
  203.         //返回结果
  204.         if($data){
  205.             curl_close($ch);
  206.             return $data;
  207.         }
  208.         else {
  209.             $error = curl_errno($ch);
  210.             echo "curl出错,错误码:$error"."
    "
    ;
  211.             echo "
    "
    ;
  212.             curl_close($ch);
  213.             return false;
  214.         }
  215.     }
  216.     
  217.     /**
  218.      *     作用:打印数组
  219.      */
  220.     function printErr($wording='',$err='')
  221.     {
  222.         print_r('
    ');
  223.         echo $wording."
    "
    ;
  224.         var_dump($err);
  225.         print_r('');
  226.     }
  227. }

  228. /**
  229.  * 请求型接口的基类
  230.  */
  231. class Wxpay_client_pub extends Common_util_pub
  232. {
  233.     var $parameters;//请求参数,类型为关联数组
  234.     public $response;//微信返回的响应
  235.     public $result;//返回参数,类型为关联数组
  236.     var $url;//接口链接
  237.     var $curl_timeout;//curl超时时间
  238.     
  239.     /**
  240.      *     作用:设置请求参数
  241.      */
  242.     function setParameter($parameter, $parameterValue)
  243.     {
  244.         $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  245.     }
  246.     
  247.     /**
  248.      *     作用:设置标配的请求参数,生成签名,生成接口参数xml
  249.      */
  250.     function createXml()
  251.     {
  252.          $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  253.          $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  254.      $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  255.      $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  256.      return $this->arrayToXml($this->parameters);
  257.     }
  258.     
  259.     /**
  260.      *     作用:post请求xml
  261.      */
  262.     function postXml()
  263.     {
  264.      $xml = $this->createXml();
  265.         $this->response = $this->postXmlCurl($xml,$this->url,$this->curl_timeout);
  266.         return $this->response;
  267.     }
  268.     
  269.     /**
  270.      *     作用:使用证书post请求xml
  271.      */
  272.     function postXmlSSL()
  273.     {    
  274.      $xml = $this->createXml();
  275.         $this->response = $this->postXmlSSLCurl($xml,$this->url,$this->curl_timeout);
  276.         return $this->response;
  277.     }

  278.     /**
  279.      *     作用:获取结果,默认不使用证书
  280.      */
  281.     function getResult()
  282.     {        
  283.         $this->postXml();
  284.         $this->result = $this->xmlToArray($this->response);
  285.         return $this->result;
  286.     }
  287. }


  288. /**
  289.  * 统一支付接口类
  290.  */
  291. class UnifiedOrder_pub extends Wxpay_client_pub
  292. {    
  293.     function __construct()
  294.     {
  295.         //设置接口链接
  296.         $this->url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  297.         //设置curl超时时间
  298.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  299.     }
  300.     
  301.     /**
  302.      * 生成接口参数xml
  303.      */
  304.     function createXml()
  305.     {
  306.         try
  307.         {
  308.             //检测必填参数
  309.             if($this->parameters["out_trade_no"] == null)
  310.             {
  311.                 throw new SDKRuntimeException("缺少统一支付接口必填参数out_trade_no!"."
    "
    );
  312.             }elseif($this->parameters["body"] == null){
  313.                 throw new SDKRuntimeException("缺少统一支付接口必填参数body!"."
    "
    );
  314.             }elseif ($this->parameters["total_fee"] == null ) {
  315.                 throw new SDKRuntimeException("缺少统一支付接口必填参数total_fee!"."
    "
    );
  316.             }elseif ($this->parameters["notify_url"] == null) {
  317.                 throw new SDKRuntimeException("缺少统一支付接口必填参数notify_url!"."
    "
    );
  318.             }elseif ($this->parameters["trade_type"] == null) {
  319.                 throw new SDKRuntimeException("缺少统一支付接口必填参数trade_type!"."
    "
    );
  320.             }elseif ($this->parameters["trade_type"] == "JSAPI" &&
  321.                 $this->parameters["openid"] == NULL){
  322.                 throw new SDKRuntimeException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"."
    "
    );
  323.             }
  324.              $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  325.              $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  326.              $this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip    
  327.          $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  328.          $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  329.          return $this->arrayToXml($this->parameters);
  330.         }catch (SDKRuntimeException $e)
  331.         {
  332.             die($e->errorMessage());
  333.         }
  334.     }
  335.     
  336.     /**
  337.      * 获取prepay_id
  338.      */
  339.     function getPrepayId()
  340.     {
  341.         $this->postXml();
  342.         $this->result = $this->xmlToArray($this->response);
  343.         $prepay_id = $this->result["prepay_id"];
  344.         return $prepay_id;
  345.     }
  346.     
  347. }

  348. /**
  349.  * 订单查询接口
  350.  */
  351. class OrderQuery_pub extends Wxpay_client_pub
  352. {
  353.     function __construct()
  354.     {
  355.         //设置接口链接
  356.         $this->url = "https://api.mch.weixin.qq.com/pay/orderquery";
  357.         //设置curl超时时间
  358.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;        
  359.     }

  360.     /**
  361.      * 生成接口参数xml
  362.      */
  363.     function createXml()
  364.     {
  365.         try
  366.         {
  367.             //检测必填参数
  368.             if($this->parameters["out_trade_no"] == null &&
  369.                 $this->parameters["transaction_id"] == null)
  370.             {
  371.                 throw new SDKRuntimeException("订单查询接口中,out_trade_no、transaction_id至少填一个!"."
    "
    );
  372.             }
  373.              $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  374.              $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  375.          $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  376.          $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  377.          return $this->arrayToXml($this->parameters);
  378.         }catch (SDKRuntimeException $e)
  379.         {
  380.             die($e->errorMessage());
  381.         }
  382.     }

  383. }

  384. /**
  385.  * 退款申请接口
  386.  */
  387. class Refund_pub extends Wxpay_client_pub
  388. {
  389.     
  390.     function __construct() {
  391.         //设置接口链接
  392.         $this->url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
  393.         //设置curl超时时间
  394.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;        
  395.     }
  396.     
  397.     /**
  398.      * 生成接口参数xml
  399.      */
  400.     function createXml()
  401.     {
  402.         try
  403.         {
  404.             //检测必填参数
  405.             if($this->parameters["out_trade_no"] == null && $this->parameters["transaction_id"] == null) {
  406.                 throw new SDKRuntimeException("退款申请接口中,out_trade_no、transaction_id至少填一个!"."
    "
    );
  407.             }elseif($this->parameters["out_refund_no"] == null){
  408.                 throw new SDKRuntimeException("退款申请接口中,缺少必填参数out_refund_no!"."
    "
    );
  409.             }elseif($this->parameters["total_fee"] == null){
  410.                 throw new SDKRuntimeException("退款申请接口中,缺少必填参数total_fee!"."
    "
    );
  411.             }elseif($this->parameters["refund_fee"] == null){
  412.                 throw new SDKRuntimeException("退款申请接口中,缺少必填参数refund_fee!"."
    "
    );
  413.             }elseif($this->parameters["op_user_id"] == null){
  414.                 throw new SDKRuntimeException("退款申请接口中,缺少必填参数op_user_id!"."
    "
    );
  415.             }
  416.              $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  417.              $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  418.          $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  419.          $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  420.          return $this->arrayToXml($this->parameters);
  421.         }catch (SDKRuntimeException $e)
  422.         {
  423.             die($e->errorMessage());
  424.         }
  425.     }
  426.     /**
  427.      *     作用:获取结果,使用证书通信
  428.      */
  429.     function getResult()
  430.     {        
  431.         $this->postXmlSSL();
  432.         $this->result = $this->xmlToArray($this->response);
  433.         return $this->result;
  434.     }
  435.     
  436. }


  437. /**
  438.  * 退款查询接口
  439.  */
  440. class RefundQuery_pub extends Wxpay_client_pub
  441. {
  442.     
  443.     function __construct() {
  444.         //设置接口链接
  445.         $this->url = "https://api.mch.weixin.qq.com/pay/refundquery";
  446.         //设置curl超时时间
  447.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;        
  448.     }
  449.     
  450.     /**
  451.      * 生成接口参数xml
  452.      */
  453.     function createXml()
  454.     {        
  455.         try
  456.         {
  457.             if($this->parameters["out_refund_no"] == null &&
  458.                 $this->parameters["out_trade_no"] == null &&
  459.                 $this->parameters["transaction_id"] == null &&
  460.                 $this->parameters["refund_id "] == null)
  461.             {
  462.                 throw new SDKRuntimeException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!"."
    "
    );
  463.             }
  464.              $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  465.              $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  466.          $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  467.          $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  468.          return $this->arrayToXml($this->parameters);
  469.         }catch (SDKRuntimeException $e)
  470.         {
  471.             die($e->errorMessage());
  472.         }
  473.     }

  474.     /**
  475.      *     作用:获取结果,使用证书通信
  476.      */
  477.     function getResult()
  478.     {        
  479.         $this->postXmlSSL();
  480.         $this->result = $this->xmlToArray($this->response);
  481.         return $this->result;
  482.     }

  483. }

  484. /**
  485.  * 对账单接口
  486.  */
  487. class DownloadBill_pub extends Wxpay_client_pub
  488. {

  489.     function __construct()
  490.     {
  491.         //设置接口链接
  492.         $this->url = "https://api.mch.weixin.qq.com/pay/downloadbill";
  493.         //设置curl超时时间
  494.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;        
  495.     }

  496.     /**
  497.      * 生成接口参数xml
  498.      */
  499.     function createXml()
  500.     {        
  501.         try
  502.         {
  503.             if($this->parameters["bill_date"] == null )
  504.             {
  505.                 throw new SDKRuntimeException("对账单接口中,缺少必填参数bill_date!"."
    "
    );
  506.             }
  507.              $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  508.              $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  509.          $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  510.          $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  511.          return $this->arrayToXml($this->parameters);
  512.         }catch (SDKRuntimeException $e)
  513.         {
  514.             die($e->errorMessage());
  515.         }
  516.     }
  517.     
  518.     /**
  519.      *     作用:获取结果,默认不使用证书
  520.      */
  521.     function getResult()
  522.     {        
  523.         $this->postXml();
  524.         $this->result = $this->xmlToArray($this->result_xml);
  525.         return $this->result;
  526.     }
  527.     
  528.     

  529. }

  530. /**
  531.  * 短链接转换接口
  532.  */
  533. class ShortUrl_pub extends Wxpay_client_pub
  534. {
  535.     function __construct()
  536.     {
  537.         //设置接口链接
  538.         $this->url = "https://api.mch.weixin.qq.com/tools/shorturl";
  539.         //设置curl超时时间
  540.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;        
  541.     }
  542.     
  543.     /**
  544.      * 生成接口参数xml
  545.      */
  546.     function createXml()
  547.     {        
  548.         try
  549.         {
  550.             if($this->parameters["long_url"] == null )
  551.             {
  552.                 throw new SDKRuntimeException("短链接转换接口中,缺少必填参数long_url!"."
    "
    );
  553.             }
  554.              $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  555.              $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  556.          $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  557.          $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  558.          return $this->arrayToXml($this->parameters);
  559.         }catch (SDKRuntimeException $e)
  560.         {
  561.             die($e->errorMessage());
  562.         }
  563.     }
  564.     
  565.     /**
  566.      * 获取prepay_id
  567.      */
  568.     function getShortUrl()
  569.     {
  570.         $this->postXml();
  571.         $prepay_id = $this->result["short_url"];
  572.         return $prepay_id;
  573.     }
  574.     
  575. }

  576. /**
  577.  * 响应型接口基类
  578.  */
  579. class Wxpay_server_pub extends Common_util_pub
  580. {
  581.     public $data;//接收到的数据,类型为关联数组
  582.     var $returnParameters;//返回参数,类型为关联数组
  583.     
  584.     /**
  585.      * 将微信的请求xml转换成关联数组,以方便数据处理
  586.      */
  587.     function saveData($xml)
  588.     {
  589.         $this->data = $this->xmlToArray($xml);
  590.     }
  591.     
  592.     function checkSign()
  593.     {
  594.         $tmpData = $this->data;
  595.         unset($tmpData['sign']);
  596.         $sign = $this->getSign($tmpData);//本地签名
  597.         if ($this->data['sign'] == $sign) {
  598.             return TRUE;
  599.         }
  600.         return FALSE;
  601.     }
  602.     
  603.     /**
  604.      * 获取微信的请求数据
  605.      */
  606.     function getData()
  607.     {        
  608.         return $this->data;
  609.     }
  610.     
  611.     /**
  612.      * 设置返回微信的xml数据
  613.      */
  614.     function setReturnParameter($parameter, $parameterValue)
  615.     {
  616.         $this->returnParameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  617.     }
  618.     
  619.     /**
  620.      * 生成接口参数xml
  621.      */
  622.     function createXml()
  623.     {
  624.         return $this->arrayToXml($this->returnParameters);
  625.     }
  626.     
  627.     /**
  628.      * 将xml数据返回微信
  629.      */
  630.     function returnXml()
  631.     {
  632.         $returnXml = $this->createXml();
  633.         return $returnXml;
  634.     }
  635. }


  636. /**
  637.  * 通用通知接口
  638.  */
  639. class Notify_pub extends Wxpay_server_pub
  640. {

  641. }




  642. /**
  643.  * 请求商家获取商品信息接口
  644.  */
  645. class NativeCall_pub extends Wxpay_server_pub
  646. {
  647.     /**
  648.      * 生成接口参数xml
  649.      */
  650.     function createXml()
  651.     {
  652.         if($this->returnParameters["return_code"] == "SUCCESS"){
  653.              $this->returnParameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  654.              $this->returnParameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  655.          $this->returnParameters["nonce_str"] = $this->createNoncestr();//随机字符串
  656.          $this->returnParameters["sign"] = $this->getSign($this->returnParameters);//签名
  657.         }
  658.         return $this->arrayToXml($this->returnParameters);
  659.     }
  660.     
  661.     /**
  662.      * 获取product_id
  663.      */
  664.     function getProductId()
  665.     {
  666.         $product_id = $this->data["product_id"];
  667.         return $product_id;
  668.     }
  669.     
  670. }

  671. /**
  672.  * 静态链接二维码
  673.  */
  674. class NativeLink_pub extends Common_util_pub
  675. {
  676.     var $parameters;//静态链接参数
  677.     var $url;//静态链接

  678.     function __construct()
  679.     {
  680.     }
  681.     
  682.     /**
  683.      * 设置参数
  684.      */
  685.     function setParameter($parameter, $parameterValue)
  686.     {
  687.         $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  688.     }
  689.     
  690.     /**
  691.      * 生成Native支付链接二维码
  692.      */
  693.     function createLink()
  694.     {
  695.         try
  696.         {        
  697.             if($this->parameters["product_id"] == null)
  698.             {
  699.                 throw new SDKRuntimeException("缺少Native支付二维码链接必填参数product_id!"."
    "
    );
  700.             }            
  701.              $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  702.              $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  703.              $time_stamp = time();
  704.              $this->parameters["time_stamp"] = "$time_stamp";//时间戳
  705.          $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  706.          $this->parameters["sign"] = $this->getSign($this->parameters);//签名         
  707.             $bizString = $this->formatBizQueryParaMap($this->parameters, false);
  708.          $this->url = "weixin://wxpay/bizpayurl?".$bizString;
  709.         }catch (SDKRuntimeException $e)
  710.         {
  711.             die($e->errorMessage());
  712.         }
  713.     }
  714.     
  715.     /**
  716.      * 返回链接
  717.      */
  718.     function getUrl()
  719.     {        
  720.         $this->createLink();
  721.         return $this->url;
  722.     }
  723. }

  724. /**
  725. * JSAPI支付——H5网页端调起支付接口
  726. */
  727. class JsApi_pub extends Common_util_pub
  728. {
  729.     var $code;//code码,用以获取openid
  730.     var $openid;//用户的openid
  731.     var $parameters;//jsapi参数,格式为json
  732.     var $prepay_id;//使用统一支付接口得到的预支付id
  733.     var $curl_timeout;//curl超时时间

  734.     function __construct()
  735.     {
  736.         //设置curl超时时间
  737.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  738.     }
  739.     
  740.     /**
  741.      *     作用:生成可以获得code的url
  742.      */
  743.     function createOauthUrlForCode($redirectUrl)
  744.     {
  745.         $urlObj["appid"] = WxPayConf_pub::APPID;
  746.         $urlObj["redirect_uri"] = "$redirectUrl";
  747.         $urlObj["response_type"] = "code";
  748.         $urlObj["scope"] = "snsapi_base";
  749.         $urlObj["state"] = "STATE"."#wechat_redirect";
  750.         $bizString = $this->formatBizQueryParaMap($urlObj, false);
  751.         return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
  752.     }

  753.     /**
  754.      *     作用:生成可以获得openid的url
  755.      */
  756.     function createOauthUrlForOpenid()
  757.     {
  758.         $urlObj["appid"] = WxPayConf_pub::APPID;
  759.         $urlObj["secret"] = WxPayConf_pub::APPSECRET;
  760.         $urlObj["code"] = $this->code;
  761.         $urlObj["grant_type"] = "authorization_code";
  762.         $bizString = $this->formatBizQueryParaMap($urlObj, false);
  763.         return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
  764.     }
  765.     
  766.     
  767.     /**
  768.      *     作用:通过curl向微信提交code,以获取openid
  769.      */
  770.     function getOpenid()
  771.     {
  772.         $url = $this->createOauthUrlForOpenid();
  773.         //初始化curl
  774.            $ch = curl_init();
  775.         //设置超时
  776.         curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
  777.         curl_setopt($ch, CURLOPT_URL, $url);
  778.         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  779.         curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  780.         curl_setopt($ch, CURLOPT_HEADER, FALSE);
  781.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  782.         //运行curl,结果以jason形式返回
  783.         $res = curl_exec($ch);
  784.         curl_close($ch);
  785.         //取出openid
  786.         $data = json_decode($res,true);
  787.         $this->openid = $data['openid'];
  788.         return $this->openid;
  789.     }

  790.     /**
  791.      *     作用:设置prepay_id
  792.      */
  793.     function setPrepayId($prepayId)
  794.     {
  795.         $this->prepay_id = $prepayId;
  796.     }

  797.     /**
  798.      *     作用:设置code
  799.      */
  800.     function setCode($code_)
  801.     {
  802.         $this->code = $code_;
  803.     }

  804.     /**
  805.      *     作用:设置jsapi的参数
  806.      */
  807.     public function getParameters()
  808.     {
  809.         $jsApiObj["appId"] = WxPayConf_pub::APPID;
  810.         $timeStamp = time();
  811.      $jsApiObj["timeStamp"] = "$timeStamp";
  812.      $jsApiObj["nonceStr"] = $this->createNoncestr();
  813.         $jsApiObj["package"] = "prepay_id=$this->prepay_id";
  814.      $jsApiObj["signType"] = "MD5";
  815.      $jsApiObj["paySign"] = $this->getSign($jsApiObj);
  816.      $this->parameters = json_encode($jsApiObj);
  817.         
  818.         return $this->parameters;
  819.     }
  820. }
  821. ?>

 WeMall - 开源微商城 微信商城 商城源码 分销商城 b2b2c商城系统

wemall官网地址:http://www.wemallshop.com

wemall app商城源码Android之Native(原生)支付模式一demo-LMLPHP

12-21 16:49