easywechat是神一样的存在。非常好用。希望大家都能转到这上面来。
用easywechat来开发微信支付功能,步骤如下:
一,需要有一个商品下单页面,页面上有你的商品的信息,还要有购买数量,和一个购买按钮。
用户在这个页面浏览商品信息的时候,可以选择购买的数量,然后点击提交进入下一个页面。
(这个页面,跟淘宝的商品购买页面一样。)
二,创建一个页面,接收上面一步提交来的数据,然后做如下计算 。
1,根据商品ID或是提交来的商品信息,生成商品的参数数组。
一般是前端发起一个链接:
前面再加一个生成账单请求,在后台生产csrf,再返回一个订单id
//csrf验证
function
csrf_val (){
$val =
md5 (
time ().
rand (
1000 ,
9999 ));
session(
'csrf_val' ,
$val );
return
$val ;
}
收到后直接跳转
location .
href =
this .
$host +
"/pay/wxpay?wxid=" +
res .
data .
data .
wxid +
"&csrf=" +
res .
data .
data .
csrf
public
function
wxpayAction ()
{
$get = I(
'get.' );
$csrf =
$get [
"csrf" ];
if (csrf_valid(
$csrf )){//判断
$wxid =
$get [
"wxid" ];
$openid =open_id();//
openid是授权后微信服务器获取的,也是用户唯一凭证!!!
$model =
new
WalletModel ();
$ret =
$model ->wxpay(
$wxid ,
$openid );
$json =
$ret [
"json" ];
$url =
$ret [
"url" ];
}
//$this->_view->assign(compact('json', 'url'));
$this ->getView()->display(
'pay/cashpay.phtml' );
}
public
function
wxpay (
$wxid ,
$openid )
{
$pay = M(
"awxpay" )
->field([
"fdcNo" ,
"fdiType" ,
"fdlSrcID" ,
"fdiState" ,
"fdmAmt" ,
"fdcPrePay" ])
->where([
"ID" =>
$wxid ])->find();
if ((
$pay ) && (
$pay [
"fdiState" ] ==
0 )) {
$type =
$pay [
"fdiType" ];
//支付类型
$url =
'http://' .
$_SERVER [
'HTTP_HOST' ] .
'/notify/cashpay' ;
//微信充值 每个类型要修改这里
if (
$type ==
1 ) {
$rurl =
"/#/finance/recharsuccess?type=1" ;
$json =
$this ->postWxPay(
$wxid ,
$openid ,
$pay [
"fdmAmt" ],
$pay [
'fdcNo' ],
"充值" ,
"平台充值" ,
$url ,
$pay [
"fdcPrePay" ]);
}
else
if (
$type ==
2 ) {
$rurl =
"/#/finance/paysuccess" ;
$json =
$this ->postWxPay(
$wxid ,
$openid ,
$pay [
"fdmAmt" ],
$pay [
'fdcNo' ],
"订单支付" ,
"平台订单支付" ,
$url ,
$pay [
"fdcPrePay" ]);
}
}
return [
"json" =>
$json ,
"url" =>
$rurl ];
}
//生成微信支付订单,并返回数据前端用于调起支付
public
function
postWxPay (
$wxpayid ,
$openid ,
$amt ,
$tradeno ,
$body ,
$detail ,
$notifyurl ,
$PrePay )
{
if (!
$PrePay ) {
//之前没有发起过支付
$money =
intval (
$amt *
100 );
if (
$money >
0 ) {
$attributes = [
'trade_type' =>
'JSAPI' ,
// JSAPI,NATIVE,APP...
'body' =>
$body ,
'detail' =>
$detail ,
'out_trade_no' =>
$tradeno ,
'total_fee' =>
$money ,
'notify_url' =>
$notifyurl ,
'openid' =>
$openid ,
'attach' =>
2 ,
];
$order =
new \EasyWeChat\Payment\
Order (
$attributes );
$wechat = wechat();
$result =
$wechat ->
payment ->prepare(
$order );
$prepayId =
$result ->
prepay_id ;
if (
$result ->
return_code ==
'SUCCESS' &&
$result ->
result_code ==
'SUCCESS' ) {
$this ->upPrePayID(
$wxpayid ,
$prepayId );
$json =
$wechat ->
payment ->configForPayment(
$prepayId );
}
else {
$this ->upPrePayID(
$wxpayid ,
$prepayId );
$res =
json_encode (
$result );
//生成支付订单报错
throw
new
Exception (
$res ,
Constants ::ERROR_PAY_CODE);
}
return
$json ;
}
}
else {
//重新调起之前发起过的支付
$wechat = wechat();
$json =
$wechat ->
payment ->configForPayment(
$PrePay );
return
$json ;
}
}
class
WechatModel
extends \EasyWeChat\Foundation\
Application {
//初始化配置
public
function
__construct () {
$option =[
'debug' =>
true ,
'app_id' => sysparam(
'app_id' ),
// AppID//微信接口应用ID
'secret' => sysparam(
'secret' ),
// AppSecret//微信接口密匙
'token' => sysparam(
'token' ),
// Token//微信接口Token
'aes_key'
=> sysparam(
'aes_key'
),
//消息加解密密钥 //这些在微信后台都有
'log' => [
'level' =>
'info' ,
'file' =>
'/data/log/wechat.log' ,
],
'oauth' => [
'scopes' => [
'snsapi_base' ],
'callback' =>
'/notify/notify' ,
],
'payment' => [
'merchant_id' => sysparam(
'merchant_id' ),//这个开通微信支付后才有,商户号
'key'
=> sysparam(
'key'
),
//支付key
'cert_path' => sysparam(
'cert_path' ),
// XXX: 绝对路径!!!!//证书cert
'key_path' => sysparam(
'key_path' ),
// XXX: 绝对路径!!!!//证书key
以上开通支付账号都会有
证书pem格式
(apiclient_cert.pem)
],
'guzzle' => [
'timeout' =>
3.0 ,
// 超时时间(秒)
//'verify' => false, // 关掉 SSL 认证(强烈不建议!)
],
];
debuglog(
$option );
parent ::__construct(
$option );
}
$product = [
'trade_type' => 'JSAPI' ,
'body' => '一盒火柴' ,
'detail' => '一盒火些' ,
'out_trade_no' => 'MYERPORDERID12345678' ,
'total_fee' => 8888,
'notify_url' => 'http://www.xxx.com/order_notify' , // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'openid' => 'you-open-id' ,
];
2,接下来,生成商品对象。
$order = new Order( $product );
3,再接下来,要进行调用前计算了。
$app = new Application(config( 'wechat' ));
$payment = $app ->payment;
$result = $payment ->prepare( $order );
$prepayId = null;
if ( $result ->return_code == 'SUCCESS' && $result ->result_code == 'SUCCESS' ){
$prepayId = $result ->prepay_id;
} else {
var_dump($result );
die ( "出错了。" );
}
$config = $payment ->configForJSSDKPayment( $prepayId );
https://blog.csdn.net/b850824/article/details/73911027
4,就快进入正题,但是,还没完。还要取得一个数据。
$app = new Application(config( 'wechat' ));
$js = $app ->js;
5,上面获得的$js 和 $config数据,要在页面里显示出来。
注意,上面几步都不涉及显示,只是在生成页面显示所需要的数据。
现在才需要把这些显示在页面上。
没错,只需要修改这些。
再来理一个,第二步里,这个页面需要显示一个确认信息,比如商品的名称,和支付的总金额。
页面上应该有一个按钮,用来触发支付(.btn-do-it)。
页面里的js就是第5步里要显示的内容。
总结:
第一步,商品信息页面,用户选择购买数据,点购买,提交到第二步的确认页面。
第二步的确认页面,根据商品信息,和微信配置信息,生成必要的支付数据,并显示购买确认信息,和一个支付按钮,
用户点击支付按钮,发起支付。
支付完成,提示用户,或转到相应页面。
微信的开发团队提醒我们,请以微信的推送信息为准来处理订单,不然有可能人财两空。
-分隔-
product.html
>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title > 一盒火柴 title >
head >
< body >
< h1 > 一盒火柴 h1 >
< div >
卖火柴的小女孩,手里有一堆火柴。
div >
< hr >
< div >
< form name = "form1" action = "payment.php" method = "post" >
< lable > 数量: lable >
< input name = "qty" value = "1" />
< input name = "submit" type = "submit" value = "购买" />
form >
div >
body >
html >
payment.php
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
$product = [
'body' => '一盒火柴' ,
'trade_type' => 'JSAPI' ,
'out_trade_no' => 'ERP' .time(),
'total_fee' => 1,
'notify_url' => 'http://test.xxoo.com/wechat/notify/' ,
'openid' => $_SESSION [ 'openid' ],
'attach' => '卖火柴的小女孩' ,
];
$order = new Order( $product );
$app = new Application(config( 'wechat' ));
$js = $app ->js;
$payment = $app ->payment;
$result = $payment ->prepare( $order );
$prepayId = null;
if ( $result ->return_code == 'SUCCESS' && $result ->result_code == 'SUCCESS' ){
$prepayId = $result ->prepay_id;
} else {
var_dump($result );
die ( "出错了。" );
}
$config = $payment ->configForJSSDKPayment( $prepayId );
?>
"en" >
"UTF-8" >
购买支付
一盒火柴
您购买了“一盒火柴”,总价格: 0.01元。
数量:1盒。
"button" id= "btnPay" type= "button" value= "支付" />
这些是伪代码,payment.php并没有对product.html传来的商品信息做处理。
其实只要看payment.php文件应该知道怎么做了。
其它就两点。a,获取$config,b, 自己写wx.chooseWXPay。
其它的看easywechat文档就行了。