微信APP支付至今也没一个完整的DEMO,在这里我写一下具体怎么实现。。
1.下载官方PHP DEMO
2.下载好以后,我们可以把里面的东西放到app\Wechat里(文件夹需要新建)
3.打开lib/WxPay.Config.php,把里面四个重要参数改成自己的
4.在example里创建APP支付类文件WxPay.AppPay.php
- <?php
- /**
- *
- * APP支付实现类
- * @author widyhu
- *
- */
- class AppPay
- {
- /**
- *
- * 参数数组转换为url参数
- * @param array $urlObj
- */
- private function ToUrlParams($urlObj)
- {
- $buff = "";
- foreach ($urlObj as $k => $v)
- {
- $buff .= $k . "=" . $v . "&";
- }
- $buff = trim($buff, "&");
- return $buff;
- }
- /**
- *
- * 生成直接支付url,支付url有效期为2小时,模式二
- * @param UnifiedOrderInput $input
- */
- public function GetPayPrepayId($input)
- {
- if($input->GetTrade_type()=="APP")
- {
- $result = WxPayApi::unifiedOrder($input);
- return $result;
- }
- }
- /*生成APP提交数据*/
- 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();
- /*首先生成prepayid*/
- $input = new WxPayUnifiedOrder();
- $input->SetBody("充值");//商品或支付单简要描述(必须填写)
- //$input->SetAttach("test2");//附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据(不必填)
- //$input->SetDetail("Ipad mini 16G 白色,黑色");//商品名称明细列表(不必填)
- $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));//订单号(必须填写)
- $input->SetTotal_fee($money);//订单金额(必须填写)
- //$input->SetTime_start(date("YmdHis"));//交易起始时间(不必填)
- //$input->SetTime_expire(date("YmdHis",time()+600));//交易结束时间10分钟之内不必填)
- // $input->SetGoods_tag("test");//商品标记(不必填)
- $input->SetNotify_url("http://www.pinxuejianyou.cn/wechat/notify");//回调URL(必须填写)
- $input->SetTrade_type("APP");//交易类型(必须填写)
- // $input->SetProduct_id("1001");//rade_type=NATIVE,此参数必传。此id为二维码中包含的商品ID,商户自行定义。
- $order = WxPayApi::unifiedOrder($input);//获得订单的基本信息,包括prepayid
- // die(json_encode($order));
- $appApiParameters = $notify->GetAppApiParameters($order);//生成提交给app的一些参数
- die($appApiParameters);
- ?>
5.在lib\WxPay.Data.php最后加上WxPayAppApiPay这个类
- //APP支付参数
- 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后加入KEY
- $string = $string . "&key=".WxPayConfig::KEY;
- // dd($string);
- //签名步骤三:MD5加密
- $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"
- }
11. 微信APP支付验证签名错误就是因为你的参数名不对,必须这样的小写的才行,例如timestamp不能写成timeStamp