Chinaunix首页 | 论坛 | 博客
  • 博客访问: 53816
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-12 11:48
文章分类
文章存档

2016年(48)

我的朋友

分类: PHP

2016-12-07 17:39:07

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

 

wemall官网地址:

 

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

 * 会将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.         foreach ($arr as $key=>$val)
  122.         {
  123.              if (is_numeric($val))
  124.              {
  125.                  $xml.="<".$key.">".$val.".$key.">";

  126.              }
  127.              else
  128.                  $xml.="<".$key.">.$val."]]>.$key.">";
  129.         }
  130.         $xml.="";
  131.         return $xml;
  132.     }
  133.     
  134.     /**
  135.      *     作用:将xml转为array
  136.      */
  137.     public function xmlToArray($xml)
  138.     {        
  139.         //将XML转为array
  140.         $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);        
  141.         return $array_data;
  142.     }

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

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

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

  288.     /**
  289.      *     作用:获取结果,默认不使用证书
  290.      */
  291.     function getResult()
  292.     {        
  293.         $this->postXml();
  294.         $this->result = $this->xmlToArray($this->response);
  295.         return $this->result;
  296.     }
  297. }


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

  358. /**
  359.  * 订单查询接口
  360.  */
  361. class OrderQuery_pub extends Wxpay_client_pub
  362. {
  363.     function __construct()
  364.     {
  365.         //设置接口链接
  366.         $this->url = "";
  367.         //设置curl超时时间
  368.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;        
  369.     }

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

  393. }

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


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

  484.     /**
  485.      *     作用:获取结果,使用证书通信
  486.      */
  487.     function getResult()
  488.     {        
  489.         $this->postXmlSSL();
  490.         $this->result = $this->xmlToArray($this->response);
  491.         return $this->result;
  492.     }

  493. }

  494. /**
  495.  * 对账单接口
  496.  */
  497. class DownloadBill_pub extends Wxpay_client_pub
  498. {

  499.     function __construct()
  500.     {
  501.         //设置接口链接
  502.         $this->url = "";
  503.         //设置curl超时时间
  504.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;        
  505.     }

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

  539. }

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

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


  646. /**
  647.  * 通用通知接口
  648.  */
  649. class Notify_pub extends Wxpay_server_pub
  650. {

  651. }




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

  681. /**
  682.  * 静态链接二维码
  683.  */
  684. class NativeLink_pub extends Common_util_pub
  685. {
  686.     var $parameters;//静态链接参数
  687.     var $url;//静态链接

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

  734. /**
  735. * JSAPI支付——H5网页端调起支付接口
  736. */
  737. class JsApi_pub extends Common_util_pub
  738. {
  739.     var $code;//code码,用以获取openid
  740.     var $openid;//用户的openid
  741.     var $parameters;//jsapi参数,格式为json
  742.     var $prepay_id;//使用统一支付接口得到的预支付id
  743.     var $curl_timeout;//curl超时时间

  744.     function __construct()
  745.     {
  746.         //设置curl超时时间
  747.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  748.     }
  749.     
  750.     /**
  751.      *     作用:生成可以获得code的url
  752.      */
  753.     function createOauthUrlForCode($redirectUrl)
  754.     {
  755.         $urlObj["appid"] = WxPayConf_pub::APPID;
  756.         $urlObj["redirect_uri"] = "$redirectUrl";
  757.         $urlObj["response_type"] = "code";
  758.         $urlObj["scope"] = "snsapi_base";
  759.         $urlObj["state"] = "STATE"."#wechat_redirect";
  760.         $bizString = $this->formatBizQueryParaMap($urlObj, false);
  761.         return "".$bizString;
  762.     }

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

  800.     /**
  801.      *     作用:设置prepay_id
  802.      */
  803.     function setPrepayId($prepayId)
  804.     {
  805.         $this->prepay_id = $prepayId;
  806.     }

  807.     /**
  808.      *     作用:设置code
  809.      */
  810.     function setCode($code_)
  811.     {
  812.         $this->code = $code_;
  813.     }

  814.     /**
  815.      *     作用:设置jsapi的参数
  816.      */
  817.     public function getParameters()
  818.     {
  819.         $jsApiObj["appId"] = WxPayConf_pub::APPID;
  820.         $timeStamp = time();
  821.      $jsApiObj["timeStamp"] = "$timeStamp";
  822.      $jsApiObj["nonceStr"] = $this->createNoncestr();
  823.         $jsApiObj["package"] = "prepay_id=$this->prepay_id";
  824.      $jsApiObj["signType"] = "MD5";
  825.      $jsApiObj["paySign"] = $this->getSign($jsApiObj);
  826.      $this->parameters = json_encode($jsApiObj);
  827.         
  828.         return $this->parameters;
  829.     }
  830. }
  831. ?>

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

wemall官网地址:

wemall

阅读(1103) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~