-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcommerce_wechatpay.inc
167 lines (137 loc) · 4.02 KB
/
commerce_wechatpay.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
use Overtrue\Wechat\Payment;
use Overtrue\Wechat\Payment\Order;
use Overtrue\Wechat\Payment\Business;
use Overtrue\Wechat\Payment\UnifiedOrder;
use Overtrue\Wechat\Payment\Notify;
use Overtrue\Wechat\Auth;
/**
* Class CommerceWechatpay
* Main handler class for Commerce wechatpay, you can extend this class
* and set your own name using hook_commerce_wechatpay_class_name_alter()
*
* @see hook_commerce_wechatpay_class_name_alter()
* @see commerce_wechatpay_get_handler_class()
*
*/
Class CommerceWechatpay {
protected $appId;
protected $appSecret;
protected $mchId;
protected $mchKey;
protected $jsParm;
protected $transaction_info;
protected $notify_obj;
public function __construct($appId, $appSecret, $mchId, $mchKey){
$this->appId = $appId;
$this->appSecret = $appSecret;
$this->mchId = $mchId;
$this->mchKey = $mchKey;
return $this;
}
/**
* Prepare all parameters and send to Wechat
*/
public function proceed($order_num, $oder_name, $price, $notify_url){
/**
* Set business
*/
$business = new Business($this->appId, $this->appSecret, $this->mchId, $this->mchKey);
/**
* Get openid
*/
$auth = new Auth($this->appId, $this->appSecret);
if (empty($_SESSION['wechat_logged_user'])) {
$user = $auth->authorize(null, 'snsapi_base', 'STATE');
$_SESSION['wechat_logged_user'] = $user->all();
} else {
$user = $_SESSION['wechat_logged_user'];
}
$openid = $user['openid'];
/**
* Create order
*/
$order = new Order();
$order->body = $oder_name;
$order->out_trade_no = $this->orderId2outTradeNo($order_num);
$order->total_fee = $price; // 单位为 “分”, 字符串类型
$order->openid = $openid;
$order->notify_url = $notify_url;
/**
* Process Unified order
*/
$unifiedOrder = new UnifiedOrder($business, $order);
/**
* Generate payment parameters
*/
$payment = new Payment($unifiedOrder);
$this->jsParm = $payment->getConfig();
}
public function proceed_qrpay($order_num, $oder_name, $price, $notify_url) {
/**
* Set business
*/
$business = new Business($this->appId, $this->appSecret, $this->mchId, $this->mchKey);
/**
* Create order
*/
$order = new Order();
$order->trade_type = 'NATIVE';
$order->body = $oder_name;
$order->out_trade_no = $this->orderId2outTradeNo($order_num);
$order->total_fee = $price; // 单位为 “分”, 字符串类型
$order->notify_url = $notify_url;
/**
* Process Unified order
*/
$unifiedOrder = new UnifiedOrder($business, $order);
$uni_res = $unifiedOrder->getResponse();
if (empty($uni_res["code_url"])) {
return false;
}
return $uni_res["code_url"];
}
/**
* Generate out trade number from order_id, feel free to extend
*/
public function orderId2outTradeNo($order_num) {
$out_trade_no = $order_num;
return $out_trade_no;
}
/**
* Get order_id from out trade number, feel free to extend
*/
public function outTradeNo2orderId($out_trade_no) {
$order_num = $out_trade_no;
return $order_num;
}
public function getJsParm($order_num, $oder_name, $price = 1, $notify_url, $sendOpenId = TRUE){
$this->proceed($order_num, $oder_name, $price, $notify_url, $sendOpenId);
return $this->jsParm;
}
/**
* Receive notification and verify it
*
* @return bool|array
*/
public function receiveNotify() {
$transaction = $this->getNotifyInstance()->verify();
if (!$transaction) {
return false;
}
$this->transaction_info = $transaction->all();
return $transaction->all();
}
public function getTransactionInfo() {
return $this->transaction_info;
}
public function getReplyReceived() {
return $this->getNotifyInstance()->reply();
}
public function getNotifyInstance() {
if (!$this->notify_obj) {
$this->notify_obj = new Notify($this->appId, $this->appSecret, $this->mchId, $this->mchKey);
}
return $this->notify_obj;
}
}