forked from KOgames/edd-payza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payza.gateway.php
executable file
·355 lines (308 loc) · 7.41 KB
/
payza.gateway.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* Payza Payment Gateway
*
* @author Abid Omar
*/
class wp_payza_gateway
{
/**
* The Payza Merchant Email
* @var string
*/
public $merchant_id;
/**
* Purchase Type (item, service, subscription...)
* @var string
*/
public $purchase_type;
/**
* Return and Status URL
* @var string
*/
public $return_url;
/**
* Cancel URL
* @var string
*/
public $cancel_url;
/**
* IPN URL
* @var string
*/
public $ipn_url;
/**
* Sandbox mode for Payza
* @var int
*/
public $sandbox = false;
/**
* Purchase Description
* @var string
*/
public $description = null;
/**
* Transaction total amount
* @var int
*/
public $amount;
/**
* Transaction currency
* @var string
*/
public $currency;
/**
* Payza real Payment URL
* @var string
*/
public $real_api_url = 'https://secure.payza.com/checkout';
/**
* Payza sandbox Payment URL
* @var string
*/
public $sandbox_api_url = 'https://sandbox.payza.com/sandbox/payprocess.aspx';
/**
*
*/
public $test_mode;
/**
* Checkout URL
*
* @var string
*/
private $checkout_url;
/**
* Saves error info.
* @var
*/
public $debug_info;
/**
* Creates a new instance of the Payza Class
*
* @param string $merchant_id Merchant Email Address
* @param string $purchase_type Purchase Type (usually item)
* @param string $currency Currency
* @param string $return_url Return URL
* @param string $cancel_url Cancel URL
* @param string $ipn_url IPN URL
* @param boolean $sandbox_mode (false)
* @param string $description Description (null)
*/
function __construct($merchant_id, $purchase_type, $currency, $return_url, $cancel_url, $ipn_url, $sandbox_mode = false, $description = null)
{
// Fill the Class Properties
$this->merchant_id = $merchant_id;
$this->purchase_type = $purchase_type;
$this->currency = $currency;
$this->return_url = $return_url;
$this->cancel_url = $cancel_url;
$this->ipn_url = $ipn_url;
$this->sandbox = $sandbox_mode;
$this->description = $description;
$this->test_mode = $sandbox_mode;
$this->checkout_url = $this->real_api_url;
}
/**
* Creates a new transaction and returns the redirect URL
*
* @param integer $transaction_id
* @param mixed $cart_details
* @return string $redirect_url
*/
public function transaction($transaction_id, $cart_details = false)
{
// Required Details
$args = array(
'ap_merchant' => $this->merchant_id,
'ap_purchasetype' => $this->purchase_type,
'ap_currency' => $this->currency,
'ap_returnurl' => $this->return_url,
'ap_cancelurl' => $this->cancel_url,
'apc_1' => $transaction_id, // Transaction ID
'ap_alerturl' => $this->ipn_url, //$this->ipn_url,//$this->ipn_url, // IPN URL
'ap_ipnversion' => 2 // IPN Version 2.0
);
if ( $this->test_mode ) {
$args['ap_testmode'] = 1;
}
// Cart Details
if ($cart_details) {
$payment = edd_get_payment( $transaction_id );
$cart_details = $this->get_cart_details($cart_details);
$cart_details['ap_taxamount'] = $payment->tax;
$shipping_total = 0;
$fee_discounts = 0;
foreach ( $payment->fees as $fee ) {
if ( floatval( $fee['amount'] ) < 0 ) {
$fee_discounts += $fee['amount'];
} else if ( false !== strpos( $fee['id'], 'simple_shipping' ) ) {
$shipping_total += floatval( $fee['amount'] );
}
}
$cart_details['ap_shippingcharges'] = edd_format_amount( floatval( $shipping_total ) );
$cart_details['ap_discountamount'] = edd_format_amount( floatval( $payment->subtotal - $payment->discounted_amount ) - $fee_discounts );
$cart_details['ap_currency'] = $payment->currency;
$args = array_merge($cart_details, $args);
} else {
$this->debug_info = 'Cart Details empty';
return false;
}
// Build the redirect URL
$query_str = http_build_query( apply_filters( 'edd_payza_transaction_args', $args ) );
$redirect_url = $this->checkout_url . '?' . $query_str;
return $redirect_url;
}
/**
* Format the Easy Digital Download Cart details to a format
* compatible with the Payza payment gateway
*
* @param array $cart_details
* @return array $formatted_cart
*/
private function get_cart_details($cart_details)
{
$formatted_cart = array();
$key = 1;
foreach( $cart_details as $i => $item ) {
$item_amount = edd_prices_include_tax() ? $item['item_price'] - ( $item['tax'] / $item['quantity'] ) : $item['item_price'];
if( $item_amount <= 0 ) {
$item_amount = 0;
}
$formatted_cart['ap_itemname_' . $key] = $item['name'];
$formatted_cart['ap_quantity_' . $key] = $item['quantity'];
$formatted_cart['ap_amount_' . $key] = $item_amount;
$key++;
}
return $formatted_cart;
}
}
/**
* IPN Handler
*
* @author Abid Omar
*/
class wp_payza_ipn
{
/**
* Currency
* @var string
*/
public $currency;
/**
* Request time out
*
* @var integer
*/
public $timeout;
/**
* SSL Verification
*
* @var boolean
*/
public $sslverify;
/**
* Debug Information
*
* @var string
*/
public $debug_info;
/**
* Real IPN Handler
*
* @var string
*/
private $real_ipn_handler = 'https://secure.payza.com/ipn2.ashx';
/**
* Sandbox IPN Handler
*
* @var string
*/
private $sandbox_ipn_handler = 'https://sandbox.Payza.com/sandbox/IPN2.ashx';
/**
* IPN Handler
*
* @var string
*/
private $ipn_handler;
/**
* Creates a new instance of the IPN Class
*
* @param string $currency
* @param bool $sandbox_mode
*/
function __construct($currency = "USD", $sandbox_mode = false)
{
// Sandbox Mode
$this->ipn_handler = $this->real_ipn_handler;
if ($sandbox_mode) {
$this->ipn_handler = $this->sandbox_ipn_handler;
}
// Currency
$this->currency = $currency;
// SSL Verification
$this->sslverify = apply_filters('https_local_ssl_verify', false);
}
/**
* Handle IPN request
*
* @static
* @param string $token
* @return boolean $success
*/
public function handle_ipn($token)
{
$response = $this->retrieve_response($token);
if (strlen($response) > 0 && $response != 'INVALID TOKEN') {
// Extract Data
parse_str($response, $data);
$transaction_id = $data['apc_1']; // Transaction ID
$currency = $data['ap_currency']; // Currency
$status = $data['ap_status']; // Status
// Retrieve transaction details
$payment_meta = get_post_meta($transaction_id, '_edd_payment_meta', true);
$amount = edd_format_amount($payment_meta['amount']); // amount
if ($this->currency != $currency) {
return false;
}
if ($amount != $data['ap_totalamount']) {
return false;
}
if ($status != 'Success') {
return false;
}
return $transaction_id; // Payment Successful!
}
return false;
}
/**
* Retrieve the transaction details
*
* @static
* @param string $token Transaction Token
* @return string $response Transaction details
*/
private function retrieve_response($token)
{
$body = array(
'token' => $token
);
$request = array(
'body' => $body,
'timeout' => $this->timeout,
'sslverify' => $this->sslverify
);
$response = wp_remote_post($this->ipn_handler, $request);
// HTTP Request fails
if (is_wp_error($response)) {
$this->debug_info = $response;
return false;
}
// Status code returned other than 200
if ($response['response']['code'] != 200) {
$this->debug_info = 'Response code different than 200';
return false;
}
// Request succeeded, return the Session ID
return $response['body'];
}
}