1、
2、
private static $WeixinConfig = array(
‘wxAppId’ => ‘xxxxx’,
‘wxMchId’ => ‘xxxx’, // 商户号
‘wxPayKey’ => ‘xxxxxx’, // 支付密钥
‘wxAppSecret’ => ‘xxxxxxx’,
‘openId’=>‘xxxxxxxx’, // 公众号支付需要
‘body’ => ‘模拟下单测试’,
‘unifiedOrder’ => ‘https://api.mch.weixin.qq.com/pay/unifiedorder’, // 统一下单
‘orderQuery’ => ‘https://api.mch.weixin.qq.com/pay/orderquery’, //查询订单
// ‘notifyUrl’ => ‘http://dev.api.live.3ttech.cn/notify/notify-process’, //回调地址
‘notifyUrl’ => ‘xxxxxxx’, //回调地址
‘transfers’ => ‘https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers’,
‘template’ => ‘https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=’,
‘accessToken’ => ‘https://api.weixin.qq.com/cgi-bin/token’,
‘ticket’ => ‘https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=’
);
3、数据库
CREATE TABLE order
(
id
int(10) NOT NULL AUTO_INCREMENT,
userid
bigint(11) NOT NULL COMMENT ‘用户id’,
goodsid
bigint(11) NOT NULL COMMENT ‘商品id’,
price
float(6,2) NOT NULL COMMENT ‘价格’,
orderIdAlias
bigint(20) NOT NULL DEFAULT ‘0’ COMMENT ‘订单号’,
transactionId
varchar(30) NOT NULL DEFAULT ‘0’ COMMENT ‘第三方交易号(微信回调会返回)’,
status
varchar(20) NOT NULL COMMENT ‘交易状态 1未支付 2 交易完成 3交易取消 4交易关闭’,
source
int(10) NOT NULL DEFAULT ‘0’ COMMENT ‘1:微信 2:支付宝’,
orderCreateTime
int(11) NOT NULL DEFAULT ‘0’ COMMENT ‘交易下单时间’,
orderPayTime
bigint(20) NOT NULL DEFAULT ‘0’ COMMENT ‘交易支付完成时间(微信会返回)’,
created
int(11) NOT NULL,
updated
int(11) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
5、
控制器(PaymentController)
use app\common\models\TestOrder;
use app\common\models\TestPayment;
$params = array(‘price’ => 0.01, ‘goodsid’ => 55, ‘userid’ => 65);
d
a
t
=
T
e
s
t
O
r
d
e
r
:
:
q
u
e
r
y
A
l
l
(
dat = TestOrder::queryAll(
dat=TestOrder::queryAll(params); // 入库操作
if ($dat[‘code’] == 1) {
//微信支付
$data = array(
‘price’ => $dat[‘price’],
‘orderIdAlias’ => $dat[‘orderIdAlias’],
‘goodsid’ => $dat[‘goodsid’]
);
c
o
d
e
u
r
l
=
T
e
s
t
P
a
y
m
e
n
t
:
:
W
e
i
X
i
n
N
a
t
i
v
e
P
a
y
(
code_url = TestPayment::WeiXinNativePay(
codeurl=TestPayment::WeiXinNativePay(data);
if (!empty($code_url)) {
return $this->render(‘placeanorder’, [
‘codeurl’ => KaTeX parse error: Expected 'EOF', got '}' at position 54: … ]); }̲ } echo…params)
{
if (isset(KaTeX parse error: Expected 'EOF', got '&' at position 20: …ms['orderid']) &̲& !empty(params[‘orderid’])) {
$model = static::findOne([‘id’ =>
p
a
r
a
m
s
[
′
o
r
d
e
r
i
d
′
]
]
)
;
i
f
(
e
m
p
t
y
(
params['orderid']]); if (empty(
params[′orderid′]]);if(empty(model)) {
return [‘code’ => -1, ‘message’ => ‘订单号不存在’];
}
$model->orderIdAlias = static::orderIdAlias();
$model->updated = time();
$model->save();
} else {
$model = new self();
$model->userid = $params[‘userid’];
$model->price = $params[‘price’];
$model->goodsid = $params[‘goodsid’];
$model->orderIdAlias = time() . rand(10000, 99999);
$model->source = 1;
$model->status = 1;
$model->orderCreateTime = time();
$model->created = time();
$model->updated = time();
$model->save();
}
return [
‘code’ => 1,
‘message’ => ‘’,
‘price’ => $model->price,
‘orderIdAlias’ => $model->orderIdAlias,
‘goodsid’ => $model->goodsid // 二维码支付需要传商品id
];
}
7、
模型层(TestPayment) 应该放在服务层,我放在了model层
首先调微信的统一下单接口.(微信官方文档https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1)
d
a
t
a
=
a
r
r
a
y
(
′
a
p
p
i
d
′
=
>
s
e
l
f
:
:
data = array( 'appid' => self::
data=array(′appid′=>self::WeixinConfig[‘wxAppId’],
‘mch_id’ => self::
W
e
i
x
i
n
C
o
n
f
i
g
[
′
w
x
M
c
h
I
d
′
]
,
′
n
o
n
c
e
s
t
r
′
=
>
m
d
5
(
m
i
c
r
o
t
i
m
e
(
)
.
′
w
e
i
x
i
n
′
.
r
a
n
d
(
100
,
9999
)
)
,
′
b
o
d
y
′
=
>
s
e
l
f
:
:
WeixinConfig['wxMchId'], 'nonce_str' => md5(microtime() . 'weixin' . rand(100, 9999)), 'body' => self::
WeixinConfig[′wxMchId′],′noncestr′=>md5(microtime().′weixin′.rand(100,9999)),′body′=>self::WeixinConfig[‘body’],
‘out_trade_no’ => $params[‘orderIdAlias’],
‘fee_type’ => ‘CNY’,
‘total_fee’ => $params[‘price’] * 100,
‘spbill_create_ip’ =>
S
E
R
V
E
R
[
"
R
E
M
O
T
E
A
D
D
R
"
]
,
/
/
或
者
是
你
服
务
器
的
i
p
(
最
好
是
这
个
)
′
t
i
m
e
s
t
a
r
t
′
=
>
d
a
t
e
(
′
Y
m
d
H
i
s
′
)
,
′
t
i
m
e
e
x
p
i
r
e
′
=
>
d
a
t
e
(
′
Y
m
d
H
i
s
′
,
s
t
r
t
o
t
i
m
e
(
′
+
2
h
o
u
r
s
′
)
)
,
′
n
o
t
i
f
y
u
r
l
′
=
>
s
e
l
f
:
:
_SERVER["REMOTE_ADDR"], // 或者是你服务器的ip(最好是这个) 'time_start' => date('YmdHis'), 'time_expire' => date('YmdHis', strtotime('+2 hours')), 'notify_url' => self::
SERVER["REMOTEADDR"],//或者是你服务器的ip(最好是这个)′timestart′=>date(′YmdHis′),′timeexpire′=>date(′YmdHis′,strtotime(′+2hours′)),′notifyurl′=>self::WeixinConfig[‘notifyUrl’],
‘trade_type’ => ‘NATIVE’,
‘product_id’ =>
p
a
r
a
m
s
[
′
g
o
o
d
s
i
d
′
]
,
)
;
k
s
o
r
t
(
params['goodsid'], ); ksort(
params[′goodsid′],);ksort(data);
s
t
r
=
h
t
t
p
b
u
i
l
d
q
u
e
r
y
(
str = http_build_query(
str=httpbuildquery(data);
s
t
r
=
s
t
a
t
i
c
:
:
j
o
i
n
A
P
I
K
E
Y
2
(
str = static::joinAPI_KEY2(
str=static::joinAPIKEY2(str);
d
a
t
a
[
′
s
i
g
n
′
]
=
s
t
r
t
o
u
p
p
e
r
(
m
d
5
(
u
r
l
d
e
c
o
d
e
(
data['sign'] = strtoupper(md5(urldecode(
data[′sign′]=strtoupper(md5(urldecode(str)));
x
m
l
=
s
t
a
t
i
c
:
:
a
r
r
T
o
X
M
L
(
xml = static::arrToXML(
xml=static::arrToXML(data);
//请求统一下单订单接口,微信返回的xml
r
e
s
u
l
t
=
s
t
a
t
i
c
:
:
p
o
s
t
X
m
l
C
u
r
l
(
result = static::postXmlCurl(
result=static::postXmlCurl(xml, self::$WeixinConfig[‘unifiedOrder’]);
// 解析微信返回的xml
d
a
t
a
=
s
t
a
t
i
c
:
:
x
m
l
T
o
A
r
r
(
data = static::xmlToArr(
data=static::xmlToArr(result);
if($data[‘RETURN_CODE’] == ‘SUCCESS’ && $data[‘RESULT_CODE’] == ‘SUCCESS’){
return $data[‘CODE_URL’];
} else {
return ‘’;
}
8、视图层
视图层
9、回掉
//微信回调
public function actionWeixinCallBack()
{
r
e
s
u
l
t
=
f
i
l
e
g
e
t
c
o
n
t
e
n
t
s
(
′
p
h
p
:
/
/
i
n
p
u
t
′
)
;
/
/
l
l
(
result = file_get_contents('php://input'); // ll(
result=filegetcontents(′php://input′);//ll(result,‘call_back.log’);
// 将微信回调返回的xml转换成数组
d
a
t
=
T
e
s
t
P
a
y
m
e
n
t
:
:
x
m
l
T
o
A
r
r
(
dat = TestPayment::xmlToArr(
dat=TestPayment::xmlToArr(result);
ll(json_encode(
d
a
t
)
,
′
c
a
l
l
b
a
c
k
.
l
o
g
′
)
;
i
f
(
dat), 'call_back.log'); if(
dat),′callback.log′);if(dat[‘RETURN_CODE’] == ‘SUCCESS’ && KaTeX parse error: Expected '}', got 'EOF' at end of input: …tOrder::saveDb(dat);
// 给微信返回xml格式,告诉微信我已经接收到回调了
$xml = TestPayment::arrToXML([‘return_code’ => ‘SUCCESS’, ‘return_msg’ => ‘OK’],true);
echo $xml;
}
}
微信回调返回的xml转换成json串
{“APPID”:“wx70a3358e75e061f7”,“BANK_TYPE”:“CFT”,“CASH_FEE”:“1”,“FEE_TYPE”:“CNY”,“IS_SUBSCRIBE”:“N”,“MCH_ID”:“1440798702”,“NONCE_STR”:“41e004bd8dc1410243b4c55a99c9dfa6”,“OPENID”:“oUxR_vwBP_CIYnv10CE7TV3lt6D4”,“OUT_TRADE_NO”:“152852591294292”,“RESULT_CODE”:“SUCCESS”,“RETURN_CODE”:“SUCCESS”,“SIGN”:“6AE498803A960A8EEF3F66BDA331CA78”,“TIME_END”:“20180609143210”,“TOTAL_FEE”:“1”,“TRADE_TYPE”:“NATIVE”,“TRANSACTION_ID”:“4200000133201806092062434584”}
9、app
微信的支付(二维码,APP)
最新推荐文章于 2023-05-11 13:57:13 发布