-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpaypal.php
210 lines (193 loc) · 6.79 KB
/
paypal.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
<?php
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
*
* Payment Gateway paypal.com
*
* created by @ibnux <me@ibnux.com>
*
**/
function paypal_validate_config()
{
global $config;
if (empty($config['paypal_client_id']) || empty($config['paypal_secret_key'])) {
sendTelegram("PayPal payment gateway not configured");
r2(U . 'order/package', 'w', "Admin has not yet setup Paypal payment gateway, please tell admin");
}
}
function paypal_show_config()
{
global $ui;
$ui->assign('_title', 'Paypal - Payment Gateway');
$ui->assign('currency', json_decode(file_get_contents('system/paymentgateway/paypal_currency.json'), true));
$ui->display('paypal.tpl');
}
function paypal_save_config()
{
global $admin, $_L;
$paypal_client_id = _post('paypal_client_id');
$paypal_secret_key = _post('paypal_secret_key');
$paypal_currency = _post('paypal_currency');
$d = ORM::for_table('tbl_appconfig')->where('setting', 'paypal_secret_key')->find_one();
if ($d) {
$d->value = $paypal_secret_key;
$d->save();
} else {
$d = ORM::for_table('tbl_appconfig')->create();
$d->setting = 'paypal_secret_key';
$d->value = $paypal_secret_key;
$d->save();
}
$d = ORM::for_table('tbl_appconfig')->where('setting', 'paypal_client_id')->find_one();
if ($d) {
$d->value = $paypal_client_id;
$d->save();
} else {
$d = ORM::for_table('tbl_appconfig')->create();
$d->setting = 'paypal_client_id';
$d->value = $paypal_client_id;
$d->save();
}
$d = ORM::for_table('tbl_appconfig')->where('setting', 'paypal_currency')->find_one();
if ($d) {
$d->value = $paypal_currency;
$d->save();
} else {
$d = ORM::for_table('tbl_appconfig')->create();
$d->setting = 'paypal_currency';
$d->value = $paypal_currency;
$d->save();
}
_log('[' . $admin['username'] . ']: Paypal ' . Lang::T('Settings_Saved_Successfully'), 'Admin', $admin['id']);
r2(U . 'paymentgateway/paypal', 's', Lang::T('Settings_Saved_Successfully'));
}
function paypal_create_transaction($trx, $user)
{
global $config;
$json = [
'intent' => 'CAPTURE',
'purchase_units' => [
[
'amount' => [
'currency_code' => $config['paypal_currency'],
'value' => strval($trx['price'])
]
]
],
"application_context" => [
"return_url" => U . "order/view/" . $trx['id'] . '/check',
"cancel_url" => U . "order/view/" . $trx['id'],
]
];
$result = json_decode(
Http::postJsonData(
paypal_get_server() . 'checkout/orders',
$json,
[
'Prefer: return=minimal',
'PayPal-Request-Id: paypal_' . $trx['id'],
'Authorization: Bearer ' . paypalGetAccessToken()
]
),
true
);
if (!$result['id']) {
sendTelegram("paypal_create_transaction FAILED: \n\n" . json_encode($result, JSON_PRETTY_PRINT));
r2(U . 'order/package', 'e', "Failed to create Paypal transaction.");
}
$urlPayment = "";
foreach ($result['links'] as $link) {
if ($link['rel'] == 'approve') {
$urlPayment = $link['href'];
break;
}
}
$d = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->where('status', 1)
->find_one();
$d->gateway_trx_id = $result['id'];
$d->pg_url_payment = $urlPayment;
$d->pg_request = json_encode($result);
$d->expired_date = date('Y-m-d H:i:s', strtotime("+ 6 HOUR"));
$d->save();
header('Location: ' . $urlPayment);
exit();
}
/*
*/
function paypal_payment_notification()
{
// Not yet implemented
die('OK');
}
function paypal_get_status($trx, $user)
{
$capture = [];
if (empty($trx->pg_paid_response)) {
$capture = paypal_capture_transaction($trx['gateway_trx_id']);
} else {
$capture = json_decode($trx->pg_paid_response, true)['paypal_capture'];
if (empty($capture)) {
$capture = paypal_capture_transaction($trx['gateway_trx_id']);
}
}
$result = json_decode(Http::getData(paypal_get_server() . 'checkout/orders/' . $trx['gateway_trx_id'], ['Authorization: Bearer ' . paypalGetAccessToken()]), true);
if (in_array($result['status'], ['APPROVED', 'COMPLETED']) && $trx['status'] != 2) {
if ($capture['status'] == 'COMPLETED' || ($capture['name'] == 'UNPROCESSABLE_ENTITY' && $capture['details'][0]['issue'] == 'ORDER_ALREADY_CAPTURED')) {
if (!Package::rechargeUser($user['id'], $trx['routers'], $trx['plan_id'], $trx['gateway'], 'Paypal')) {
r2(U . "order/view/" . $trx['id'], 'd', "Failed to activate your Package, try again later.");
}
$result['paypal_capture'] = json_encode($capture);
$trx->pg_paid_response = json_encode($result);
$trx->payment_method = 'PAYPAL';
$trx->payment_channel = 'paypal';
$trx->paid_date = date('Y-m-d H:i:s', strtotime($result['updated']));
$trx->status = 2;
$trx->save();
r2(U . "order/view/" . $trx['id'], 's', "Transaction has been paid.");
} else {
r2(U . "order/view/" . $trx['id'], 'e', "Transaction Success, but not yet captured.");
}
} else if ($result['status'] == 'VOIDED') {
$trx->pg_paid_response = json_encode($result);
$trx->status = 3;
$trx->save();
r2(U . "order/view/" . $trx['id'], 'd', "Transaction expired.");
} else {
sendTelegram("xendit_get_status: unknown result\n\n" . json_encode($result, JSON_PRETTY_PRINT));
r2(U . "order/view/" . $trx['id'], 'w', "Transaction status :" . $result['status']);
}
}
function paypal_capture_transaction($trx_id)
{
return json_decode(
Http::postJsonData(
paypal_get_server() . 'checkout/orders/' . $trx_id . '/capture',
[],
[
'PayPal-Partner-Attribution-Id: <BN-Code>',
'Authorization: Bearer ' . paypalGetAccessToken()
]
),
true
);
}
function paypalGetAccessToken()
{
global $config;
$result = Http::postData(str_replace('v2', 'v1', paypal_get_server()) . 'oauth2/token', [
"grant_type" => "client_credentials"
], [], $config['paypal_client_id'] . ":" . $config['paypal_secret_key']);
$json = json_decode($result, true);
return $json['access_token'];
}
function paypal_get_server()
{
global $_app_stage;
if ($_app_stage == 'Live') {
return 'https://api-m.paypal.com/v2/';
} else {
return 'https://api-m.sandbox.paypal.com/v2/';
}
}