Laravel5.2微信APP支付接口

来源:互联网 发布:dota2比赛数据 编辑:程序博客网 时间:2024/05/18 02:00

微信APP支付至今也没一个完整的DEMO,在这里我写一下具体怎么实现。。

1.下载官方PHP DEMO

2.下载好以后,我们可以把里面的东西放到app\Wechat里(文件夹需要新建)

3.打开lib/WxPay.Config.php,把里面四个重要参数改成自己的

4.在example里创建APP支付类文件WxPay.AppPay.php

[php] view plain copy
  1. <?php  
  2.   
  3. /** 
  4.  
  5. * APP支付实现类 
  6. * @author widyhu 
  7. * 
  8. */  
  9. class AppPay  
  10. {      
  11.     /** 
  12.      *  
  13.      * 参数数组转换为url参数 
  14.      * @param array $urlObj 
  15.      */  
  16.     private function ToUrlParams($urlObj)  
  17.     {  
  18.         $buff = "";  
  19.         foreach ($urlObj as $k => $v)  
  20.         {  
  21.             $buff .= $k . "=" . $v . "&";  
  22.         }  
  23.            
  24.         $buff = trim($buff"&");  
  25.         return $buff;  
  26.     }  
  27.        
  28.     /** 
  29.      *  
  30.      * 生成直接支付url,支付url有效期为2小时,模式二 
  31.      * @param UnifiedOrderInput $input 
  32.      */  
  33.     public function GetPayPrepayId($input)  
  34.     {  
  35.         if($input->GetTrade_type()=="APP")  
  36.         {  
  37.             $result = WxPayApi::unifiedOrder($input);  
  38.             return $result;  
  39.         }  
  40.     }  
  41.     /*生成APP提交数据*/  
  42.     public function GetAppApiParameters($UnifiedOrderResult)  
  43.     {  
  44.         if(!array_key_exists("appid"$UnifiedOrderResult)  
  45.         || !array_key_exists("prepay_id"$UnifiedOrderResult)  
  46.         || $UnifiedOrderResult['prepay_id'] == "")  
  47.         {  
  48.             throw new WxPayException("参数错误");  
  49.         }  
  50.         $appapi = new WxPayAppApiPay();  
  51.         $appapi->SetAppid($UnifiedOrderResult["appid"]);  
  52.         $appapi->SetPartnerId($UnifiedOrderResult["mch_id"]);  
  53.         $appapi->SetPrepayId($UnifiedOrderResult["prepay_id"]);  
  54.         $timeStamp = time();  
  55.         $appapi->SetTimeStamp($timeStamp);  
  56.         $appapi->SetOldStr($UnifiedOrderResult["nonce_str"]);  
  57.         $appapi->SetPackage("Sign=WXPay");  
  58.         $appapi->SetSign();  
  59.         $parameters = json_encode($appapi->GetValues());  
  60.         return $parameters;  
  61.     }  
  62. }  

5.创建模板bussinessapppay.blade.php

[php] view plain copy
  1. @extends('layouts.new')  
  2.   
  3. <?php   
  4. ini_set('date.timezone','Asia/Shanghai');  
  5. require_once "../app/Wechat/lib/WxPay.Api.php";  
  6. require_once "../app/Wechat/example/WxPay.AppPay.php";  
  7.   
  8. $notify = new AppPay();  
  9. /*首先生成prepayid*/  
  10. $input = new WxPayUnifiedOrder();  
  11. $input->SetBody("充值");//商品或支付单简要描述(必须填写)  
  12. //$input->SetAttach("test2");//附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据(不必填)  
  13. //$input->SetDetail("Ipad mini 16G 白色,黑色");//商品名称明细列表(不必填)  
  14. $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));//订单号(必须填写)  
  15. $input->SetTotal_fee($money);//订单金额(必须填写)  
  16. //$input->SetTime_start(date("YmdHis"));//交易起始时间(不必填)  
  17. //$input->SetTime_expire(date("YmdHis",time()+600));//交易结束时间10分钟之内不必填)  
  18. // $input->SetGoods_tag("test");//商品标记(不必填)  
  19. $input->SetNotify_url("http://www.pinxuejianyou.cn/wechat/notify");//回调URL(必须填写)  
  20. $input->SetTrade_type("APP");//交易类型(必须填写)  
  21. // $input->SetProduct_id("1001");//rade_type=NATIVE,此参数必传。此id为二维码中包含的商品ID,商户自行定义。  
  22. $order = WxPayApi::unifiedOrder($input);//获得订单的基本信息,包括prepayid  
  23. // die(json_encode($order));  
  24. $appApiParameters = $notify->GetAppApiParameters($order);//生成提交给app的一些参数  
  25. die($appApiParameters);  
  26. ?>  

5.在lib\WxPay.Data.php最后加上WxPayAppApiPay这个类

[php] view plain copy
  1. //APP支付参数  
  2. class WxPayAppApiPay extends WxPayDataBase{  
  3.     public function SetAppid($value)  
  4.     {  
  5.         $this->values['appid'] = $value;  
  6.     }  
  7.   
  8.     public function SetTimeStamp($value)  
  9.     {  
  10.         $this->values['timestamp'] = $value;  
  11.     }  
  12.   
  13.     public function SetNonceStr($value)  
  14.     {  
  15.         $this->values['noncestr'] = $value;  
  16.     }  
  17.   
  18.     public function SetPackage($value)  
  19.     {  
  20.         $this->values['package'] = $value;  
  21.     }  
  22.   
  23.     public function SetSign()  
  24.     {  
  25.         $sign = $this->MakeNewSign();  
  26.         $this->values['sign'] = $sign;  
  27.         return $sign;  
  28.     }  
  29.   
  30.     public function GetValues()  
  31.     {  
  32.         return $this->values;  
  33.     }  
  34.   
  35.     public function SetPartnerId($value){  
  36.         $this->values['partnerid'] = $value;  
  37.     }  
  38.   
  39.     public function SetPrepayId($value){  
  40.         $this->values['prepayid'] = $value;  
  41.     }  
  42.   
  43.     public function MakeNewSign()  
  44.     {  
  45.         //签名步骤一:按字典序排序参数  
  46.         ksort($this->values);  
  47.         $string = $this->ToUrlParams();  
  48.         //签名步骤二:在string后加入KEY  
  49.         $string = $string . "&key=".WxPayConfig::KEY;  
  50.         // dd($string);  
  51.         //签名步骤三:MD5加密  
  52.         $string = md5($string);  
  53.         //签名步骤四:所有字符转为大写  
  54.         $result = strtoupper($string);  
  55.         return $result;  
  56.     }  
  57.   
  58.     public function SetOldStr($value)  
  59.     {  
  60.         $this->values['noncestr'] = $value;  
  61.     }  
  62.   
  63.   
  64. }  


6.新建控制器WechatController.php,在里面创建方法

[php] view plain copy
  1. public function businessPay(){  
  2.         $money = Input::get('money');  
  3.         if($money == ''){  
  4.             return response()->json(['status'=>0,'msg'=>'请传入充值金额']);  
  5.         }  
  6.         return view('home.wechat.businessapppay')  
  7.             ->withMoney($money);  
  8.     }  

7.注册路由,这里不多说了。。

[php] view plain copy
  1. Route::any('api/home/wechat/businesspay','Api\WechatController@businessPay');  

8.注册好路由以后,我的路径是http://localhost:8000/api/home/wechat/businesspay?money=100,金额必须传哦,单位是分~

9.测试接口查看返回数据,如果你填写的参数都正确,应该是这样的

[plain] view plain copy
  1. {  
  2. "appid":"xxxxxxxxxxx",  
  3. "partnerid":"xxxxxxxxxx",  
  4. "noncestr":"xxxxxxxxxx",  
  5. "package":"Sign=WXPay",  
  6. "prepayid":"xxxxxx",  
  7. "timestamp":1472715881,  
  8. "sign":"xxxxxxx"  
  9. }  
10.大功告成,很简单啊有木有~
11.微信APP支付验证签名错误就是因为你的参数名不对,必须这样的小写的才行,例如timestamp不能写成timeStamp
原创粉丝点击