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
-
-
-
-
-
-
-
- class AppPay
- {
-
-
-
-
-
- private function ToUrlParams($urlObj)
- {
- $buff = "";
- foreach ($urlObj as $k => $v)
- {
- $buff .= $k . "=" . $v . "&";
- }
-
- $buff = trim($buff, "&");
- return $buff;
- }
-
-
-
-
-
-
- public function GetPayPrepayId($input)
- {
- if($input->GetTrade_type()=="APP")
- {
- $result = WxPayApi::unifiedOrder($input);
- return $result;
- }
- }
-
- public function GetAppApiParameters($UnifiedOrderResult)
- {
- if(!array_key_exists("appid", $UnifiedOrderResult)
- || !array_key_exists("prepay_id", $UnifiedOrderResult)
- || $UnifiedOrderResult['prepay_id'] == "")
- {
- throw new WxPayException("参数错误");
- }
- $appapi = new WxPayAppApiPay();
- $appapi->SetAppid($UnifiedOrderResult["appid"]);
- $appapi->SetPartnerId($UnifiedOrderResult["mch_id"]);
- $appapi->SetPrepayId($UnifiedOrderResult["prepay_id"]);
- $timeStamp = time();
- $appapi->SetTimeStamp($timeStamp);
- $appapi->SetOldStr($UnifiedOrderResult["nonce_str"]);
- $appapi->SetPackage("Sign=WXPay");
- $appapi->SetSign();
- $parameters = json_encode($appapi->GetValues());
- return $parameters;
- }
- }
5.创建模板bussinessapppay.blade.php- @extends('layouts.new')
-
- <?php
- ini_set('date.timezone','Asia/Shanghai');
- require_once "../app/Wechat/lib/WxPay.Api.php";
- require_once "../app/Wechat/example/WxPay.AppPay.php";
-
- $notify = new AppPay();
-
- $input = new WxPayUnifiedOrder();
- $input->SetBody("充值");
-
-
- $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
- $input->SetTotal_fee($money);
-
-
-
- $input->SetNotify_url("http://www.pinxuejianyou.cn/wechat/notify");//回调URL(必须填写)
- $input->SetTrade_type("APP");
-
- $order = WxPayApi::unifiedOrder($input);
-
- $appApiParameters = $notify->GetAppApiParameters($order);
- die($appApiParameters);
- ?>
5.在lib\WxPay.Data.php最后加上WxPayAppApiPay这个类
-
- class WxPayAppApiPay extends WxPayDataBase{
- public function SetAppid($value)
- {
- $this->values['appid'] = $value;
- }
-
- public function SetTimeStamp($value)
- {
- $this->values['timestamp'] = $value;
- }
-
- public function SetNonceStr($value)
- {
- $this->values['noncestr'] = $value;
- }
-
- public function SetPackage($value)
- {
- $this->values['package'] = $value;
- }
-
- public function SetSign()
- {
- $sign = $this->MakeNewSign();
- $this->values['sign'] = $sign;
- return $sign;
- }
-
- public function GetValues()
- {
- return $this->values;
- }
-
- public function SetPartnerId($value){
- $this->values['partnerid'] = $value;
- }
-
- public function SetPrepayId($value){
- $this->values['prepayid'] = $value;
- }
-
- public function MakeNewSign()
- {
-
- ksort($this->values);
- $string = $this->ToUrlParams();
-
- $string = $string . "&key=".WxPayConfig::KEY;
-
-
- $string = md5($string);
-
- $result = strtoupper($string);
- return $result;
- }
-
- public function SetOldStr($value)
- {
- $this->values['noncestr'] = $value;
- }
-
-
- }
6.新建控制器WechatController.php,在里面创建方法
- public function businessPay(){
- $money = Input::get('money');
- if($money == ''){
- return response()->json(['status'=>0,'msg'=>'请传入充值金额']);
- }
- return view('home.wechat.businessapppay')
- ->withMoney($money);
- }
7.注册路由,这里不多说了。。- Route::any('api/home/wechat/businesspay','Api\WechatController@businessPay');
8.注册好路由以后,我的路径是http://localhost:8000/api/home/wechat/businesspay?money=100,金额必须传哦,单位是分~
9.测试接口查看返回数据,如果你填写的参数都正确,应该是这样的
- {
- "appid":"xxxxxxxxxxx",
- "partnerid":"xxxxxxxxxx",
- "noncestr":"xxxxxxxxxx",
- "package":"Sign=WXPay",
- "prepayid":"xxxxxx",
- "timestamp":1472715881,
- "sign":"xxxxxxx"
- }
10.大功告成,很简单啊有木有~11.微信APP支付验证签名错误就是因为你的参数名不对,必须这样的小写的才行,例如timestamp不能写成timeStamp