-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AliPay.php
223 lines (192 loc) · 7.59 KB
/
AliPay.php
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* Copyright 2022-2023 FOSSBilling
* Copyright 2011-2021 BoxBilling, Inc.
* SPDX-License-Identifier: Apache-2.0
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
*/
class Payment_Adapter_AliPay extends Payment_AdapterAbstract
{
public function init()
{
if(!$this->getParam('partner')) {
throw new Payment_Exception('The ":pay_gateway" payment gateway is not fully configured. Please configure the :missing', [':pay_gateway' => 'AliPay', ':missing' => 'Partner ID']);
}
if (!$this->getParam('security_code')) {
throw new Payment_Exception('The ":pay_gateway" payment gateway is not fully configured. Please configure the :missing', [':pay_gateway' => 'AliPay', ':missing' => 'Security Code']);
}
if (!$this->getParam('seller_email')) {
throw new Payment_Exception('The ":pay_gateway" payment gateway is not fully configured. Please configure the :missing', [':pay_gateway' => 'AliPay', ':missing' => 'Seller Email']);
}
$this->_config['charset'] = 'utf-8';
}
public static function getConfig()
{
return array(
'supports_one_time_payments' => true,
'supports_subscriptions' => false,
'description' => 'Clients will be redirected to Alipay to make payment.',
'logo' => array(
'logo' => 'alipay.png',
'height' => '25px',
'width' => '85px',
),
'form' => array(
'partner' => array('text', array(
'label' => 'AliPay Partner ID. After signing contract online successfully, Alipay provides the partner id',
),
),
'security_code' => array('text', array(
'label' => 'AliPay security code. After signing contract online successfully, Alipay provides the 32bits security code',
),
),
'seller_email' => array('text', array(
'label' => 'AliPay seller email. Your Alipay account',
),
),
'type' => array('select', array(
'multiOptions' => array(
'1' => 'Type 1 - receive money at once. You need to be registered company in china',
'2' => 'Type 2 - receive money after buyer confirm it'
),
'label' => 'Contract type. Type 1: is mainly for virtual goods. So seller can receive money at once, but it requires seller is an register company; Type 2 is mainly for real goods, when buyer receive goods, then buyer check the goods is right and confirm to pay! During the process, the money is held by Alipay, if buyer dont claim to get money back, whether confirm or not, the money will transfer to seller after certain days.',
),
),
),
);
}
/**
* Return payment gateway type
* @return string
*/
public function getType()
{
return Payment_AdapterAbstract::TYPE_FORM;
}
/**
* Return payment gateway type
* @return string
*/
public function getServiceUrl()
{
if($this->testMode) {
return 'https://www.alipay.com/cooperate/gateway.do';
}
return 'https://www.alipay.com/cooperate/gateway.do';
}
public function getInvoiceId($data)
{
$id = parent::getInvoiceId($data);
if(is_null($id)) {
$id = isset($data['post']['out_trade_no']) ? (int)$data['post']['out_trade_no'] : NULL;
}
return $id;
}
public function singlePayment(Payment_Invoice $invoice)
{
$client = $invoice->getBuyer();
$contract = $this->getParam('type');
switch ($contract){
case '0':
$service = 'trade_create_by_buyer';
break;
case '1':
$service = 'create_direct_pay_by_user';
break;
case '2':
$service = 'create_partner_trade_by_buyer';
break;
}
if(!isset($service)){
throw new Payment_Exception("Error getting service for contract");
}
$parameter = array(
'service' => $service,
'partner' => $this->getParam('partner'),
'_input_charset' => $this->getParam('charset'),
'notify_url' => $this->getParam('notify_url'),
'return_url' => $this->getParam('thankyou_url'),
'subject' => $invoice->getTitle(),
'out_trade_no' => $invoice->getId(),
'price' => $invoice->getTotalWithTax(),
'quantity' => 1,
'payment_type' => 1,
'logistics_type' => 'EXPRESS',
'logistics_fee' => 0,
'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',
'seller_email' => $this->getParam('seller_email'),
);
ksort($parameter);
reset($parameter);
$data = $parameter;
$data['sign'] = $this->_generateSignature($parameter);
$data['sign_type'] = 'MD5';
return $data;
}
public function recurrentPayment(Payment_Invoice $invoice)
{
throw new Payment_Exception(':type: does not support :action:', [':type:' => 'AliPay', ':action:' => __trans('reccurent payments')]);
}
public function getTransaction($data, Payment_Invoice $invoice)
{
$ipn = $data['post'];
$uniqid = md5($ipn['trade_no'].$ipn['trade_status']);
$tx = new Payment_Transaction();
$tx->setId($uniqid);
$tx->setAmount($ipn['total_fee']);
$tx->setCurrency($invoice->getCurrency());
$contract = $this->getParam('type');
if($contract == '1') {
switch ($ipn['trade_status']) {
case 'TRADE_SUCCESS':
case 'TRADE_FINISHED':
$tx->setType(Payment_Transaction::TXTYPE_PAYMENT);
$tx->setStatus(Payment_Transaction::STATUS_COMPLETE);
break;
default:
$tx->setStatus($ipn['trade_status']);
break;
}
}
if($contract == '2') {
switch ($ipn['trade_status']) {
case 'WAIT_SELLER_SEND_GOODS':
$tx->setType(Payment_Transaction::TXTYPE_PAYMENT);
$tx->setStatus(Payment_Transaction::STATUS_COMPLETE);
break;
default:
$tx->setStatus($ipn['trade_status']);
break;
}
}
return $tx;
}
public function isIpnValid($data, Payment_Invoice $invoice)
{
$ipn = $data['post'];
ksort($ipn);
reset($ipn);
$sign = '';
foreach ($ipn AS $key=>$val)
{
if ($key != 'sign' && $key != 'sign_type' && $key != 'code' && $key !='bb_gateway_id' && $key != 'bb_invoice_id')
{
$sign .= "$key=$val&";
}
}
$sign = substr($sign, 0, -1) . $this->getParam('security_code');
return (md5($sign) == $ipn['sign']);
}
private function _generateSignature(array $parameter)
{
$sign = '';
foreach ($parameter AS $key => $val)
{
$sign .= "$key=$val&";
}
$sign = substr($sign, 0, -1) . $this->getParam('security_code');
return md5($sign);
}
}