用Laravel5.2从零开始做PHP微信APP支付接口

24 篇文章 0 订阅
6 篇文章 0 订阅

微信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"
}
10.大功告成,很简单啊有木有~
11. 深渊大坑:微信APP支付验证签名错误就是因为你的参数名不对,必须这样的小写的才行,例如timestamp不能写成timeStamp

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值