您的位置:首页 > 编程语言 > PHP开发

微信JS-SDK官方支付接口在THINKPHP中使用

2015-04-01 16:07 841 查看
1、下载demo

进入微信JS-SDK说明文档http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html

在页面底部下载demo

2、将demo中的文件放入ThinkPHP中

进入sample/php,除了sample.php其余全部放入ThinkPHP项目的Common中

打开sample.php可以看到是php和html组合的,我按照ThinkPHP的写法将sample.php拆成一个Action和一个html模版文件

3、直接贴代码JsAction.class.php

01
<?php
02
class
JsAction
extends
CommonAction
{
03
04
public
function
index(){
05
 
load
(
"@.jssdk"
);
06
 
$jssdk
=
new
JSSDK(
"yourAppID"
,
"yourAppSecret"
);
07
 
$signPackage
=
$jssdk
->GetSignPackage();
08
 
$this
->assign(
'js'
,
$signPackage
);
09
 
$this
->display();
10
}
11
public
function
getPackage(){
12
/**统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口
13
 
统一支付中以下参数从配置中获取,或由类自动生成,不需要用户填写
14
 
微信支付统一下单接口文档:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=9_1*/
15
$rand
=
md5(time() . mt_rand(0,1000));
16
$param
[
"appid"
]
=
"yourAppSecret"
;
17
$param
[
"openid"
]
=
$_SESSION
[
"userInfo"
][
"wxID"
];
18
$param
[
"mch_id"
]
=
"商户ID"
;
19
$param
[
"nonce_str"
]
=
$rand
;
20
$param
[
"body"
]
=
"测试支付"
;
21
$param
[
"out_trade_no"
]
=
$_SESSION
[
"userInfo"
][
"userID"
].time();
22
$param
[
"total_fee"
]
=1;
23
$param
[
"spbill_create_ip"
]
=
$_SERVER
[
"REMOTE_ADDR"
];
24
$param
[
"notify_url"
]
=
"http://www.xxx.com/xxx.php/Js/callback"
;
25
$param
[
"trade_type"
]
=
"JSAPI"
;
26
27
 
//签名算法:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3
28
$signStr
=
'appid='
.
$param
[
"appid"
].
"&body="
.
$param
[
"body"
].
"&mch_id="
.
$param
[
"mch_id"
].
"&nonce_str="
.
$param
[
"nonce_str"
].
"¬ify_url="
.
$param
[
"notify_url"
].
"&openid="
.
$param
[
"openid"
].
"&out_trade_no="
.
$param
[
"out_trade_no"
].
"&spbill_create_ip="
.
$param
[
"spbill_create_ip"
].
"&total_fee="
.
$param
[
"total_fee"
].
"&trade_type="
.
$param
[
"trade_type"
];
29
$signStr
=
$signStr
.
"&key=youAPIkey"
;
30
31
$param
[
"sign"
]
=
strtoupper
(MD5(
$signStr
));
32
33
$data
=
'<xml>
34
<appid><![CDATA[
'.$param["appid"].'
]]></appid>
35
<openid><![CDATA[
'.$param["openid"].'
]]></openid>
36
<mch_id>
'.$param["mch_id"].'
</mch_id>
37
<nonce_str><![CDATA[
'.$param["nonce_str"].'
]]></nonce_str>
38
<body><![CDATA[
'.$param["body"].'
]]></body>
39
<out_trade_no><![CDATA[
'.$param["out_trade_no"].'
]]></out_trade_no>
40
<total_fee>
'.$param["total_fee"].'
</total_fee>
41
<spbill_create_ip><![CDATA[
'.$param["spbill_create_ip"].'
]]></spbill_create_ip>
42
<notify_url><![CDATA[
'.$param["notify_url"].'
]]></notify_url>
43
<trade_type><![CDATA[
'.$param["trade_type"].'
]]></trade_type>
44
<sign><![CDATA[
'.$param["sign"].'
]]></sign>
45
</xml>';
46
47
$postResult
=
myCurl(
"https://api.mch.weixin.qq.com/pay/unifiedorder"
,
$data
);
48
$postObj
=
simplexml_load_string(
$postResult
,
'SimpleXMLElement'
,
LIBXML_NOCDATA);
49
50
$msg
=
""
.
$postObj
->return_msg;
51
if
(
$msg
==
"OK"
){
52
$result
[
"timestamp"
]
=time();
53
$result
[
"nonceStr"
]
=
""
.
$postObj
->nonce_str;
//不加""拿到的是一个json对象
54
$result
[
"package"
]
=
"prepay_id="
.
$postObj
->prepay_id;
55
$result
[
"signType"
]
=
"MD5"
;
56
57
 
/**支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
58
 
备注:prepay_id
通过微信支付统一下单接口拿到,paySign 采用统一的微信支付 Sign 签名生成方法,注意这里 appId 也要参与签名,
59
 
appId
与 config 中传入的 appId 一致,即最后参与签名的参数有appId,timeStamp,nonceStr,package,signType。*/
60
$paySignStr
=
'appId=wxd7c487b7a2d9bc65'
.
'&nonceStr='
.
$result
[
"nonceStr"
].
'&package='
.
$result
[
"package"
].
'&signType='
.
$result
[
"signType"
].
'&timeStamp='
.
$result
[
"timestamp"
];
61
$paySignStr
=
$paySignStr
.
"&key=youAPIkey"
;
62
63
$result
[
"paySign"
]
=
strtoupper
(MD5(
$paySignStr
));
64
$this
->ajaxReturn(
$result
,
""
.
$msg
,
1);
65
}
else
{
66
 
$this
->ajaxReturn(
""
,
""
.
$msg
,
0);
67
}
68
}
69
70
public
function
callback(){
71
72
}
73
}
4、index.html

01
<include
file=
'Public:header'
/>
02
03
</head>
04
<body>
05
<div
data-role=
"page"
>
06
<script
src=
"http://res.wx.qq.com/open/js/jweixin-1.0.0.js"
></script>
07
<script
type=
"text/javascript"
>
08
wx.config({
09
appId:
'{$js["appId"]}'
,
10
timestamp:
{$js[
"timestamp"
]},
11
nonceStr:
'{$js["nonceStr"]}'
,
12
signature:
'{$js["signature"]}'
,
13
jsApiList:
[
'chooseWXPay'
]
14
});
15
16
wx.ready(
function
()
{
17
 
18
document.querySelector(
'#pay'
).onclick
=
function
()
{
19
showLoad();
20
$.ajax({
21
type:
"POST"
,
22
url:
"__URL__/getPackage"
,
23
data:{},
24
success:
function
(data){
25
hideLoad();
26
if
(data.status==1){
27
wx.chooseWXPay({
28
timestamp:
data.data.timestamp,
//
支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
29
nonceStr:
data.data.nonceStr,
//
支付签名随机串,不长于 32 位
30
package:
data.data.package,
//
统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
31
signType:
data.data.signType,
//
签名方式,默认为'SHA1',使用新版支付需传入'MD5'
32
paySign:
data.data.paySign,
//
支付签名
33
success:
function
(res)
{
34
var
str
=JSON.stringify(res);
35
//
支付成功后的回调函数
36
alert(str);
37
if
(res.errMsg
==
"chooseWXPay:ok"
){
38
alert(
"支付成功!"
);
39
}
else
{
40
alert(
"支付失败!"
);
41
}
42
}
43
});
44
}
else
{
45
show_msg(data.info);
46
}
47
}
48
49
});
50
};
51
52
});
53
54
wx.error(
function
(res)
{
55
alert(res.errMsg);
56
});
57
</script>
58
<div
data-role=
"content"
>
59
60
<button
id=
"pay"
>支付1分钱</button>
61
</div>
62
63
<div
data-role=
"popup"
id=
"info_pop"
class=
"ui-content"
data-theme=
"d"
style=
'font-weight:bold;font-size:14px;
z-index: 99999; background-color: white;'
>
64
<p>未知错误</p>
65
</div>
66
</div>
67
</body>
68
</html>
Common/jssdk.php代码包括了获取 access_token 和 jsapi_ticket 的操作,只需传入 appid 和 appsecret 即可,获取 access_token 部分代码从全局缓存中获取,防止重复获取 access_token
,超过调用频率。

应注意access_token.json和jsapi_ticket.json路径



5、支付通知

支付成功后,通知接口中也将收到支付成功的xml通知

支付结果通用通知http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=9_7

01
<
xml
>
02
<
appid
>
<![CDATA[wx8888888888888888]]>
</
appid
>
03
<
bank_type
>
<![CDATA[CFT]]>
</
bank_type
>
04
<
fee_type
>
<![CDATA[CNY]]>
</
fee_type
>
05
<
is_subscribe
>
<![CDATA[Y]]>
</
is_subscribe
>
06
<
mch_id
>
<![CDATA[10012345]]>
</
mch_id
>
07
<
nonce_str
>
<![CDATA[60uf9sh6nmppr9azveb2bn7arhy79izk]]>
</
nonce_str
>
08
<
openid
>
<![CDATA[ou9dHt0L8qFLI1foP-kj5x1mDWsM]]>
</
openid
>
09
<
out_trade_no
>
<![CDATA[wx88888888888888881414411779]]>
</
out_trade_no
>
10
<
result_code
>
<![CDATA[SUCCESS]]>
</
result_code
>
11
<
return_code
>
<![CDATA[SUCCESS]]>
</
return_code
>
12
<
sign
>
<![CDATA[0C1D7F2534F1473247550A5A138F0CEB]]>
</
sign
>
13
<
sub_mch_id
>
<![CDATA[10012345]]>
</
sub_mch_id
>
14
<
time_end
>
<![CDATA[20141027200958]]>
</
time_end
>
15
<
total_fee
>1</
total_fee
>
16
<
trade_type
>
<![CDATA[JSAPI]]>
</
trade_type
>
17
<
transaction_id
>
<![CDATA[1002750185201410270005514026]]>
</
transaction_id
>
18
</
xml
>
callback

<span style="white-space:pre">	</span>$postStr =$GLOBALS["HTTP_RAW_POST_DATA"];
$postObj =simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
$weChatOrder =$postObj->transaction_id.""; //微信返回的微信订单号
$weOrder =$postStr->out_trade_no.""; //微信返回的用户订单号
$status =$postStr->return_code."";//$stauts ="SUCCESS"



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: