PluginProbe ʕ •ᴥ•ʔ
پارسی دیت – Parsi Date / trunk
پارسی دیت – Parsi Date vtrunk
6.2.1 6.2 6.1 5.1.6 5.1.7 5.1.8 5.1.8.2 6.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.0.0-alpha 2.1 2.1.1 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0.1 2.3.0.2 2.3.1 2.3.2 2.3.3 2.3.4 3.0.1 3.0.2 3.0.3 4.0.0 4.0.1 4.0.2 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5
wp-parsidate / inc / App / Integration / WooCommerce / wc-gateways / wpp-pasargad-gateway.php
wp-parsidate / inc / App / Integration / WooCommerce / wc-gateways Last commit date
blocks 3 months ago lib 1 month ago index.php 3 months ago wpp-mellat-gateway.php 3 months ago wpp-melli-gateway.php 3 months ago wpp-parsian-gateway.php 3 months ago wpp-pasargad-gateway.php 3 months ago
wpp-pasargad-gateway.php
708 lines
1 <?php
2
3 use WPParsidate\Helper\Assets;
4
5 defined( 'ABSPATH' ) or exit( 'No direct script access allowed' );
6
7 if ( ! function_exists( 'wpp_pasargad_payment_gateway_init' ) ) {
8
9 function wpp_pasargad_payment_gateway_init() {
10 if ( ! class_exists( 'WC_Pasargad_Gateway' ) && ! class_exists( 'WPP_WC_Pasargad_Gateway' ) ) {
11
12 /**
13 * WPP_WC_Pasargad_Gateway class to add Pasargad Bank payment gateway to WooCommerce
14 *
15 * @package WP-Parsidate
16 * @subpackage Plugins/WooCommerce/PaymentGateways
17 * @since 5.0.0
18 */
19 class WPP_WC_Pasargad_Gateway extends WC_Payment_Gateway {
20
21 private $gateway_name;
22 public $redirect_uri;
23 public $pasargad_terminal_id;
24 public $pasargad_merchant_id;
25 public $description;
26 public $success_massage;
27 public $failed_massage;
28 public $cancelled_massage;
29
30 public function __construct() {
31 $this->id = 'pasargad';
32 $this->gateway_name = esc_html__( 'Pasargad Bank', 'wp-parsidate' );
33 $this->method_title = $this->gateway_name;
34 $this->method_description = $this->gateway_name . ' ' . esc_html__( 'payment gateway (By WP-Parsidate)',
35 'wp-parsidate' );
36 $this->has_fields = false;
37 $this->icon = apply_filters( $this->id . '_logo',
38 Assets::url( "images/woocommerce/$this->id-logo.png" ) );
39 $this->redirect_uri = WC()->api_request_url( strtolower( get_class( $this ) ) );
40
41 $this->init_form_fields();
42 $this->init_settings();
43
44 $this->pasargad_terminal_id = $this->settings['pasargad_terminal_id'];
45 $this->pasargad_merchant_id = $this->settings['pasargad_merchant_id'];
46 $this->title = $this->settings['title'];
47 $this->description = $this->settings['description'];
48 $this->success_massage = $this->settings['success_massage'];
49 $this->failed_massage = $this->settings['failed_massage'];
50 $this->cancelled_massage = $this->settings['cancelled_massage'];
51
52 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id,
53 array( $this, 'process_admin_options' ) );
54 add_action( 'woocommerce_receipt_' . $this->id, array( $this, 'receipt_page' ) );
55 add_action( 'woocommerce_api_' . strtolower( get_class( $this ) ), array( $this, 'callback' ) );
56 }
57
58 public function init_form_fields() {
59 $this->form_fields = apply_filters( 'wpp_wc_' . $this->id . '_gateway_config', array(
60 'enabled' => array(
61 'title' => esc_html__( 'Enabled/Disabled', 'wp-parsidate' ),
62 'type' => 'checkbox',
63 /* translators: %s: Bank name */
64 'label' => sprintf( esc_html__( 'Activate or deactivate %s gateway', 'wp-parsidate' ),
65 $this->gateway_name ),
66 'default' => 'no'
67 ),
68 'pasargad_terminal_id' => array(
69 'title' => esc_html__( 'Terminal No.', 'wp-parsidate' ),
70 'type' => 'text',
71 'required' => true,
72 'desc_tip' => true,
73 ),
74 'pasargad_merchant_id' => array(
75 'title' => esc_html__( 'Store No.', 'wp-parsidate' ),
76 'type' => 'text',
77 'required' => true,
78 'desc_tip' => true,
79 ),
80 'title' => array(
81 'title' => esc_html__( 'Gateway title', 'wp-parsidate' ),
82 'type' => 'text',
83 'description' => esc_html__( 'This name is displayed to the customer during the purchase process',
84 'wp-parsidate' ),
85 'default' => $this->gateway_name
86 ),
87 'description' => array(
88 'title' => esc_html__( 'Gateway description', 'wp-parsidate' ),
89 'type' => 'textarea',
90 'description' => esc_html__( 'The description that will be displayed during the purchase process for the gateway',
91 'wp-parsidate' ),
92 /* translators: %s: Bank name */
93 'default' => sprintf( esc_html__( "Secure payment by all Shatab's cards through %s",
94 'wp-parsidate' ), $this->gateway_name )
95 ),
96 'success_massage' => array(
97 'title' => esc_html__( 'Successful payment message', 'wp-parsidate' ),
98 'type' => 'textarea',
99 'description' => esc_html__( 'Enter the text of the message you want to display to the user after successful payment.',
100 'wp-parsidate' ),
101 'default' => esc_html__( 'Thank you for your purchase. Your order has been successfully placed.',
102 'wp-parsidate' )
103 ),
104 'failed_massage' => array(
105 'title' => esc_html__( 'Payment failed message', 'wp-parsidate' ),
106 'type' => 'textarea',
107 'description' => esc_html__( 'Enter the text of the message you want to display to the user after an unsuccessful payment.',
108 'wp-parsidate' ),
109 'default' => esc_html__( 'Your payment has failed. Please try again or contact us in case of problems.',
110 'wp-parsidate' )
111 ),
112 'cancelled_massage' => array(
113 'title' => esc_html__( 'Payment cancellation message', 'wp-parsidate' ),
114 'type' => 'textarea',
115 'description' => esc_html__( 'Enter the text of the message you want to display after the user cancels the payment. This message will be displayed after returning from the bank.',
116 'wp-parsidate' ),
117 'default' => esc_html__( 'The payment remained incomplete due to your cancellation.',
118 'wp-parsidate' )
119 )
120 )
121 );
122 }
123
124 public static function send_pay_request(
125 $invoice_number,
126 $TerminalCode,
127 $MerchantCode,
128 $Amount,
129 $RedirectAddress,
130 $Mobile = '',
131 $Email = ''
132 ) {
133 require_once( __DIR__ . '/lib/class-rsa.php' );
134
135 $processor = new RSAProcessor( __DIR__ . '/lib/pasargad-certificate.xml', RSAKeyType::XMLFile );
136 $data = array(
137 'InvoiceNumber' => $invoice_number,
138 'InvoiceDate' => parsidate( 'Y/m/d' ),
139 'TerminalCode' => $TerminalCode,
140 'MerchantCode' => $MerchantCode,
141 'Amount' => $Amount,
142 'RedirectAddress' => $RedirectAddress,
143 'Timestamp' => date( 'Y/m/d H:i:s' ),
144 'Action' => 1003,
145 'Mobile' => $Mobile,
146 'Email' => $Email
147 );
148
149 $sign_data = json_encode( $data );
150 $sign_data = sha1( $sign_data, true );
151 $sign_data = $processor->sign( $sign_data );
152 $sign = base64_encode( $sign_data );
153
154 $request_args = array(
155 'body' => json_encode( $data ),
156 'headers' => array(
157 'Content-Type' => 'application/json',
158 'Sign' => $sign,
159 ),
160 'timeout' => 30,
161 'redirection' => 5,
162 'blocking' => true,
163 );
164
165 $response = wp_remote_post( 'https://pep.shaparak.ir/Api/v1/Payment/GetToken', $request_args );
166
167 if ( is_wp_error( $response ) ) {
168 return false;
169 }
170
171 return json_decode( wp_remote_retrieve_body( $response ) );
172 }
173
174 public static function check_transaction_result(
175 $transaction_ref_id,
176 $invoice_number = '',
177 $invoice_date = '',
178 $TerminalCode = '',
179 $MerchantCode = ''
180 ) {
181 $data = array(
182 'InvoiceNumber' => $invoice_number,
183 'InvoiceDate' => $invoice_date,
184 'TerminalCode' => $TerminalCode,
185 'MerchantCode' => $MerchantCode,
186 'TransactionReferenceID' => $transaction_ref_id
187 );
188
189 $request_args = array(
190 'body' => json_encode( $data ),
191 'headers' => array(
192 'Content-Type' => 'application/json',
193 ),
194 'timeout' => 30,
195 'redirection' => 5,
196 'blocking' => true,
197 );
198
199 $response = wp_remote_post( 'https://pep.shaparak.ir/Api/v1/Payment/CheckTransactionResult',
200 $request_args );
201
202 if ( is_wp_error( $response ) ) {
203 return false;
204 }
205
206 return json_decode( wp_remote_retrieve_body( $response ) );
207 }
208
209 public static function verify_request(
210 $invoice_number,
211 $invoice_date,
212 $TerminalCode,
213 $MerchantCode,
214 $Amount
215 ) {
216 require_once( __DIR__ . '/lib/class-rsa.php' );
217
218 $processor = new RSAProcessor( __DIR__ . '/lib/pasargad-certificate.xml', RSAKeyType::XMLFile );
219 $data = array(
220 'InvoiceNumber' => $invoice_number,
221 'InvoiceDate' => $invoice_date,
222 'TerminalCode' => $TerminalCode,
223 'MerchantCode' => $MerchantCode,
224 'Amount' => $Amount,
225 'Timestamp' => date( 'Y/m/d H:i:s' )
226 );
227
228 $sign_data = json_encode( $data );
229 $sign_data = sha1( $sign_data, true );
230 $sign_data = $processor->sign( $sign_data );
231 $sign = base64_encode( $sign_data );
232
233 $request_args = array(
234 'body' => json_encode( $data ),
235 'headers' => array(
236 'Content-Type' => 'application/json',
237 'Sign' => $sign,
238 ),
239 'timeout' => 30,
240 'redirection' => 5,
241 'blocking' => true,
242 );
243
244 $response = wp_remote_post( 'https://pep.shaparak.ir/Api/v1/Payment/VerifyPayment', $request_args );
245
246 if ( is_wp_error( $response ) ) {
247 return false;
248 }
249
250 return json_decode( wp_remote_retrieve_body( $response ) );
251 }
252
253 public static function reversal_request(
254 $invoice_number,
255 $invoice_date,
256 $TerminalCode,
257 $MerchantCode
258 ) {
259 require_once( __DIR__ . '/lib/class-rsa.php' );
260
261 $processor = new RSAProcessor( __DIR__ . '/lib/pasargad-certificate.xml', RSAKeyType::XMLFile );
262 $data = array(
263 'InvoiceNumber' => $invoice_number,
264 'InvoiceDate' => $invoice_date,
265 'TerminalCode' => $TerminalCode,
266 'MerchantCode' => $MerchantCode,
267 'Timestamp' => date( 'Y/m/d H:i:s' )
268 );
269
270 $sign_data = json_encode( $data );
271 $sign_data = sha1( $sign_data, true );
272 $sign_data = $processor->sign( $sign_data );
273 $sign = base64_encode( $sign_data );
274
275 $request_args = array(
276 'body' => json_encode( $data ),
277 'headers' => array(
278 'Content-Type' => 'application/json',
279 'Sign' => $sign,
280 ),
281 'timeout' => 30,
282 'redirection' => 5,
283 'blocking' => true,
284 );
285
286 $response = wp_remote_post( 'https://pep.shaparak.ir/Api/v1/Payment/RefundPayment', $request_args );
287
288 if ( is_wp_error( $response ) ) {
289 return false;
290 }
291
292 return json_decode( wp_remote_retrieve_body( $response ) );
293 }
294
295
296 public static function error_response_handler( $code = '' ) {
297 $error_responses = array(
298 '-32768' => esc_html__( 'An unexpected error has occurred', 'wp-parsidate' ),
299 '-1552' => esc_html__( 'Transaction reversal is not allowed', 'wp-parsidate' ),
300 '-1551' => esc_html__( 'The transaction has already been reversed', 'wp-parsidate' ),
301 '-1550' => esc_html__( 'It is not possible to reverse the transaction in the current state',
302 'wp-parsidate' ),
303 '-1549' => esc_html__( 'The time allowed to request a transaction reversal has expired',
304 'wp-parsidate' ),
305 '-1548' => esc_html__( 'The bill payment request service call failed', 'wp-parsidate' ),
306 '-1540' => esc_html__( 'Transaction confirmation is unsuccessful', 'wp-parsidate' ),
307 '-1536' => esc_html__( 'The top-up charge request service call failed', 'wp-parsidate' ),
308 '-1533' => esc_html__( 'The transaction has already been confirmed', 'wp-parsidate' ),
309 '1532' => esc_html__( 'The transaction was confirmed by the recipient', 'wp-parsidate' ),
310 '-1531' => esc_html__( 'The transaction was unsuccessful due to your withdrawal from the bank',
311 'wp-parsidate' ),
312 '-1530' => esc_html__( 'The merchant is not authorized to confirm this transaction',
313 'wp-parsidate' ),
314 '-1528' => esc_html__( 'Payment information not found', 'wp-parsidate' ),
315 '-1527' => esc_html__( 'The purchase transaction payment request operation failed',
316 'wp-parsidate' ),
317 '-1507' => esc_html__( 'The transaction was sent back to the switch', 'wp-parsidate' ),
318 '-1505' => esc_html__( 'The transaction was confirmed by the merchant', 'wp-parsidate' ),
319 '-132' => esc_html__( 'The transaction amount is less than the minimum allowed',
320 'wp-parsidate' ),
321 '-131' => esc_html__( 'Token is invalid', 'wp-parsidate' ),
322 '-130' => esc_html__( 'Token has expired', 'wp-parsidate' ),
323 '-128' => esc_html__( 'The IP address format is not valid', 'wp-parsidate' ),
324 '-127' => esc_html__( 'The URL is not valid', 'wp-parsidate' ),
325 '-126' => esc_html__( "The merchant's identification code is not valid", 'wp-parsidate' ),
326 '-121' => esc_html__( 'The given string is not entirely numeric', 'wp-parsidate' ),
327 '-120' => esc_html__( 'The length of the input data is not valid', 'wp-parsidate' ),
328 '-119' => esc_html__( 'The organization is invalid', 'wp-parsidate' ),
329 '-118' => esc_html__( 'The value sent is not a number', 'wp-parsidate' ),
330 '-117' => esc_html__( 'The string length is less than the allowed limit', 'wp-parsidate' ),
331 '-116' => esc_html__( 'The length of the string exceeds the allowed limit', 'wp-parsidate' ),
332 '-115' => esc_html__( 'The payment ID is invalid', 'wp-parsidate' ),
333 '-114' => esc_html__( 'The bill ID is invalid', 'wp-parsidate' ),
334 '-113' => esc_html__( 'The input parameter is empty', 'wp-parsidate' ),
335 '-112' => esc_html__( 'The order number is duplicate', 'wp-parsidate' ),
336 '-111' => esc_html__( "The transaction amount is more than the merchant's limit",
337 'wp-parsidate' ),
338 '-108' => esc_html__( 'The ability to reverse the transaction is disabled for the merchant',
339 'wp-parsidate' ),
340 '-107' => esc_html__( 'The ability to send transaction confirmation is disabled for the merchant',
341 'wp-parsidate' ),
342 '-106' => esc_html__( 'Charging is disabled for the merchant', 'wp-parsidate' ),
343 '-105' => esc_html__( 'The top-up feature is disabled for the merchant', 'wp-parsidate' ),
344 '-104' => esc_html__( 'The ability to pay the bill is inactive for the merchant',
345 'wp-parsidate' ),
346 '-103' => esc_html__( 'The purchase feature is disabled for the merchant', 'wp-parsidate' ),
347 '-102' => esc_html__( 'The transaction was successfully rolled back', 'wp-parsidate' ),
348 '-101' => esc_html__( 'The merchant could not be authenticated', 'wp-parsidate' ),
349 '-100' => esc_html__( 'The merchant is inactive', 'wp-parsidate' ),
350 '-1' => esc_html__( 'Server error', 'wp-parsidate' ),
351 '0' => esc_html__( 'The operation is successful', 'wp-parsidate' ),
352 '1' => esc_html__( 'The card issuer refused to complete the transaction', 'wp-parsidate' ),
353 '2' => esc_html__( 'The confirmation operation of this transaction has already been done successfully',
354 'wp-parsidate' ),
355 '3' => esc_html__( 'The store merchant is invalid', 'wp-parsidate' ),
356 '5' => esc_html__( 'The transaction was abandoned', 'wp-parsidate' ),
357 '6' => esc_html__( 'An unknown error occurred', 'wp-parsidate' ),
358 '8' => esc_html__( 'By recognizing the identity of the card holder, the transaction is successful',
359 'wp-parsidate' ),
360 '9' => esc_html__( 'The received request is being followed up', 'wp-parsidate' ),
361 '10' => esc_html__( "The transaction with an amount lower than the requested amount (deficiency in the customer's account) has been accepted",
362 'wp-parsidate' ),
363 '12' => esc_html__( 'The transaction is invalid', 'wp-parsidate' ),
364 '13' => esc_html__( 'The transaction amount is incorrect', 'wp-parsidate' ),
365 '14' => esc_html__( 'The sent card number is invalid (does not exist)', 'wp-parsidate' ),
366 '15' => esc_html__( 'Card issuer is invalid (does not exist)', 'wp-parsidate' ),
367 '17' => esc_html__( 'The requesting customer has been deleted', 'wp-parsidate' ),
368 '20' => esc_html__( 'In a situation where the switch needs to query the card to accept the transaction, it may make a request from the card (terminal), this message indicates that the answer is invalid.',
369 'wp-parsidate' ),
370 '21' => esc_html__( 'If the response to the terminal request does not require any specific response or function, we will have this message.',
371 'wp-parsidate' ),
372 '22' => esc_html__( 'The transaction was suspected of malpractice (card, terminal, card holder), so it was not accepted',
373 'wp-parsidate' ),
374 '30' => esc_html__( 'The message format has errors', 'wp-parsidate' ),
375 '31' => esc_html__( 'The merchant is not supported by the switch', 'wp-parsidate' ),
376 '32' => esc_html__( "The transaction is incompletely completed (for example, a deposit transaction that is completed from the customer's point of view, but needs to be completed)",
377 'wp-parsidate' ),
378 '33' => esc_html__( 'The card has expired', 'wp-parsidate' ),
379 '38' => esc_html__( 'The number of incorrect password entries has exceeded the limit.',
380 'wp-parsidate' ),
381 '39' => esc_html__( 'There is no credit card account', 'wp-parsidate' ),
382 '40' => esc_html__( 'The requested operation is not supported', 'wp-parsidate' ),
383 '41' => esc_html__( 'The card has been declared lost', 'wp-parsidate' ),
384 '43' => esc_html__( 'The card has been declared stolen', 'wp-parsidate' ),
385 '45' => esc_html__( 'The bill can not be paid', 'wp-parsidate' ),
386 '51' => esc_html__( 'The account balance is insufficient', 'wp-parsidate' ),
387 '54' => esc_html__( 'The card has expired', 'wp-parsidate' ),
388 '55' => esc_html__( 'The card password is invalid', 'wp-parsidate' ),
389 '56' => esc_html__( 'The card is invalid', 'wp-parsidate' ),
390 '57' => esc_html__( 'The relevant transaction is not allowed by the card holder',
391 'wp-parsidate' ),
392 '58' => esc_html__( "It is not allowed to perform the relevant transaction by the operator's terminal",
393 'wp-parsidate' ),
394 '59' => esc_html__( 'The card is suspected of fraud', 'wp-parsidate' ),
395 '61' => esc_html__( 'The transaction amount is over the limit', 'wp-parsidate' ),
396 '62' => esc_html__( 'The card is limited', 'wp-parsidate' ),
397 '63' => esc_html__( 'The security measures have been violated', 'wp-parsidate' ),
398 '65' => esc_html__( 'The number of transaction requests exceeds the limit',
399 'wp-parsidate' ),
400 '68' => esc_html__( 'The response required to complete or complete the transaction has arrived too late',
401 'wp-parsidate' ),
402 '69' => esc_html__( 'The number of times the password has been repeated has exceeded the limit',
403 'wp-parsidate' ),
404 '75' => esc_html__( 'The number of incorrect password entries has exceeded the limit',
405 'wp-parsidate' ),
406 '78' => esc_html__( 'The card is not active', 'wp-parsidate' ),
407 '79' => esc_html__( 'The account connected to the card is invalid or has errors',
408 'wp-parsidate' ),
409 '80' => esc_html__( 'The transaction request has been rejected', 'wp-parsidate' ),
410 '81' => esc_html__( 'The card was not accepted', 'wp-parsidate' ),
411 '83' => esc_html__( 'The switch card service provider has not accepted the transaction',
412 'wp-parsidate' ),
413 '84' => esc_html__( 'In transactions that require communication with the exporter, if the exporter is not active, this message will be sent in response.',
414 'wp-parsidate' ),
415 '91' => esc_html__( 'The transaction authorization system is temporarily disabled or the time set for the authorization has expired',
416 'wp-parsidate' ),
417 '92' => esc_html__( 'The destination of the transaction was not found', 'wp-parsidate' ),
418 '93' => esc_html__( 'It is not possible to complete the transaction', 'wp-parsidate' )
419 );
420
421 return $error_responses[ $code ] ?? esc_html__( 'The payment of the transaction was unsuccessful due to cancellation on the bank page',
422 'wp-parsidate' );
423 }
424
425
426 public static function display_error(
427 $pay_status = '',
428 $tran_id = '',
429 $order_id = '',
430 $is_callback = 1
431 ) {
432 $page_html = '<div dir="rtl" style="font-family:inherit;font-size:12px;line-height: 25px;color:#000000;margin:-25px 0 -23px 0">';
433 $succeed_color = 'style="color:#008800"';
434 $failed_color = 'style="color: #ff0000"';
435 $desc_style = 'style="text-align:center;font-size:12px;margin:15px 0 20px;line-height:25px"';
436 $back_to_checkout = sprintf(
437 '<a href="%s" style="text-decoration:none">%s</a><br/>',
438 esc_url_raw( wc_get_checkout_url() ),
439 esc_html__( 'Back to checkout', 'wp-parsidate' )
440 );
441
442 if ( 'retry' === $pay_status ) {
443 $page_title = esc_html__( 'Temporary error in payment', 'wp-parsidate' );
444 $order_note = esc_html__( 'When the customer returned from the bank, the bank server did not respond, the customer was asked to refresh the page',
445 'wp-parsidate' );
446 $page_html .= '<div style="color:#ff0000;font-weight:bold;font-size:12px;margin:25px">::: ' . esc_html__( 'Temporary error in payment',
447 'wp-parsidate' ) . ' :::</div>
448 <div style="margin-bottom:21px;font-size:12px">' . esc_html__( 'The server of the internet portal has temporarily encountered a problem, to complete the transaction moments later, click on the button below',
449 'wp-parsidate' ) . '</div>
450 <div style="margin:20px 0 25px 0;color:#008800" id="reqreload"><button onclick="reload_page()">' . esc_html__( 'Try again',
451 'wp-parsidate' ) . '</button></div>
452 <script>
453 function reload_page(){
454 document.getElementById("reqreload").innerHTML = "' . esc_html__( 'Trying again please wait..',
455 'wp-parsidate' ) . '";
456 location.reload();
457 }
458 </script>
459 ';
460 } else {
461 $error_messages = array(
462 'reversal_done' => array(
463 'color' => $failed_color,
464 'title' => esc_html__( 'Service delivery error', 'wp-parsidate' ),
465 /* translators: %s: Tracking number */
466 'message' => sprintf( esc_html__( 'Your payment with the tracking number %s has been successfully processed at the bank, but there has been a problem with the service',
467 'wp-parsidate' ), esc_html( $tran_id ) ),
468 'description' => esc_html__( 'The order to return the money to your account has been registered in the bank, if the payment is not returned to your account within the next few hours, contact support (the maximum time to return to the account is 72 hours)',
469 'wp-parsidate' ),
470 'order_note' => esc_html__( "The customer paid the amount, but when returning from the bank, there was a problem in providing the service, the order to return the money to the customer's account was registered in the bank.",
471 'wp-parsidate' ),
472 ),
473 'reversal_error' => array(
474 'color' => $failed_color,
475 'title' => esc_html__( 'Service delivery error', 'wp-parsidate' ),
476 /* translators: %s: Tracking number */
477 'message' => sprintf( esc_html__( 'Your payment with the tracking number %s has been successfully made in the bank, but there is a problem in providing the service!',
478 'wp-parsidate' ), esc_html( $tran_id ) ),
479 'description' => esc_html__( 'The order to return the money to your account has been registered in the bank, if the payment is not returned to your account within the next few hours, contact support (the maximum time to return to the account is 72 hours)',
480 'wp-parsidate' ),
481 'order_note' => esc_html__( "The customer paid the amount, but when he returned from the bank, there was a problem in providing the service, there was an error in registering the order to return the money to the customer's account, this customer should either be provided with the service or the amount should be returned to his bank account.",
482 'wp-parsidate' ),
483 ),
484 'already_been_completed' => array(
485 'color' => $succeed_color,
486 'title' => esc_html__( 'The order has already been paid', 'wp-parsidate' ),
487 /* translators: %s: Tracking number */
488 'message' => sprintf( esc_html__( 'Your order has already been successfully placed with tracking number %s',
489 'wp-parsidate' ), esc_html( $tran_id ) ),
490 'description' => '',
491 'order_note' => ''
492 ),
493 'order_not_for_this_person' => array(
494 'color' => $failed_color,
495 'title' => esc_html__( 'The order number is incorrect', 'wp-parsidate' ),
496 'message' => esc_html__( 'The order number is incorrect', 'wp-parsidate' ),
497 'description' => esc_html__( 'The order number is incorrect; Call for support if needed',
498 'wp-parsidate' ),
499 'order_note' => ''
500 ),
501 'error_creating_order' => array(
502 'color' => $failed_color,
503 'title' => esc_html__( 'Problem with placing an order', 'wp-parsidate' ),
504 'message' => esc_html__( 'The order number is incorrect', 'wp-parsidate' ),
505 'description' => esc_html__( 'There is a problem with placing an order, please contact support',
506 'wp-parsidate' ),
507 'order_note' => ''
508 )
509 );
510
511 if ( array_key_exists( $pay_status, $error_messages ) ) {
512 $page_title = $error_messages[ $pay_status ]['title'];
513 $order_note = $error_messages[ $pay_status ]['order_note'];
514 $page_html .= sprintf(
515 '<span %s><strong>%s</strong></span><br/><p %s>%s<br>%s</p>%s',
516 $error_messages[ $pay_status ]['color'],
517 $page_title,
518 $desc_style,
519 $error_messages[ $pay_status ]['message'],
520 $error_messages[ $pay_status ]['description'],
521 $back_to_checkout
522 );
523 } elseif ( $is_callback == 0 ) {
524 $page_title = $order_note = esc_html__( 'Error sending to the bank', 'wp-parsidate' );
525 $page_html .= sprintf(
526 '<span %s><strong>%s</strong></span><br/><p %s>%s</p>%s',
527 $failed_color,
528 $page_title,
529 $desc_style,
530 self::error_response_handler( $pay_status ),
531 $back_to_checkout
532 );
533 } else {
534 $page_title = esc_html__( 'Payment was not made', 'wp-parsidate' );
535 $order_note = esc_html__( 'Payment was not made',
536 'wp-parsidate' ) . ' - ' . self::error_response_handler( $pay_status );
537 $page_html .= sprintf(
538 '<span %s><strong>%s</strong></span><br/><p %s>%s<br>%s</p>%s',
539 $failed_color,
540 $page_title,
541 $desc_style,
542 self::error_response_handler( $pay_status ),
543 esc_html__( 'If the payment has been deducted from your bank account, it will be automatically returned to your account by the bank (the final return time to the account is 72 hours) - contact support if needed.',
544 'wp-parsidate' ),
545 $back_to_checkout,
546 );
547 }
548 }
549
550 $page_html .= '</div>';
551
552 if ( ! empty( $order_id ) && ! empty( $order_note ) && 'order_not_for_this_person' !== $pay_status ) {
553 $order = wc_get_order( $order_id );
554
555 $order->add_order_note( $order_note );
556 }
557
558 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
559 wp_die( $page_html, $page_title );
560 }
561
562 public static function redirect_to_bank( $url = '' ) {
563 if ( ! empty( $url ) ) {
564 if ( headers_sent() ) {
565 echo '<script type="text/javascript">window.location.assign("' . esc_url_raw( $url ) . '")</script>';
566 } else {
567 header( "Location: $url" );
568 }
569
570 exit();
571 }
572 }
573
574 public function process_payment( $order_id ) {
575 $order = wc_get_order( $order_id );
576
577 return array(
578 'result' => 'success',
579 'redirect' => $order->get_checkout_payment_url( true )
580 );
581 }
582
583 public function receipt_page( $order_id ) {
584 if ( $order_id > 0 ) {
585 $order = wc_get_order( $order_id );
586 $currency = strtolower( $order->get_currency() );
587 $amount = absint( $order->get_total() );
588
589 if ( in_array( $currency, array(
590 'irt',
591 'toman',
592 'iran toman',
593 'iranian toman',
594 'iran-toman',
595 'iran_toman',
596 'تو�
597 ان',
598 'تو�
599 ان ایران'
600 ) ) ) {
601 $amount *= 10;
602 } elseif ( 'irht' === $currency ) {
603 $amount *= 1000 * 10;
604 } elseif ( 'irhr' === $currency ) {
605 $amount *= 1000;
606 }
607
608 $terminal_id = $this->pasargad_terminal_id;
609 $merchant_id = $this->pasargad_merchant_id;
610 $callback_url = $this->redirect_uri . "?order_id=" . $order_id;
611 $order_id .= wp_rand( 10, 100 );
612 $request = self::send_pay_request( $order_id, $terminal_id, $merchant_id, $amount,
613 $callback_url );
614
615 if ( isset( $request ) && $request->IsSuccess ) {
616 self::redirect_to_bank( 'https://pep.shaparak.ir/payment.aspx?n=' . $request->Token );
617
618 exit;
619 }
620
621 self::display_error( '', '', $order_id, 0 );
622 } else {
623 self::display_error( 'error_creating_order', '', $order_id, 0 );
624 }
625
626 return false;
627 }
628
629 public function callback() {
630 $order_id = sanitize_text_field( wp_unslash( $_GET['order_id'] ?? '' ) );
631 $transaction_ref_id = sanitize_text_field( wp_unslash( $_REQUEST['tref'] ?? '' ) );
632 $invoice_number = sanitize_text_field( wp_unslash( $_REQUEST['iN'] ?? '' ) );
633 $invoice_date = sanitize_text_field( wp_unslash( $_REQUEST['iD'] ?? '' ) );
634 $terminal_id = $this->pasargad_terminal_id;
635 $merchant_id = $this->pasargad_merchant_id;
636
637 if ( $order_id == substr( $invoice_number, 0, - 2 ) ) {
638 $order = wc_get_order( $order_id );
639
640 if ( $order ) {
641 if ( 'wc-completed' === $order->get_status() ) {
642 $error_code = 'already_been_completed';
643 } else {
644 if ( $transaction_ref_id != '' ) {
645 $check_result = self::check_transaction_result( $transaction_ref_id );
646 } else {
647 $check_result = self::check_transaction_result( null, $invoice_number,
648 $invoice_date, $terminal_id, $merchant_id );
649 }
650
651 if ( isset( $check_result ) && $check_result->IsSuccess && $check_result->InvoiceNumber == $invoice_number ) {
652 $amount = $check_result->Amount;
653 $request = self::verify_request( $invoice_number, $invoice_date, $terminal_id,
654 $merchant_id, $amount );
655
656 if ( isset( $request ) && $request->IsSuccess ) {
657 if ( $order->update_status( 'processing' ) ) {
658 $order->payment_complete();
659 $order->add_order_note( 'پرداخت ش�
660 ا با �
661 وفقیت با ش�
662 اره پیگیری ' . $transaction_ref_id . ' انجا�
663 شد.',
664 1 );
665
666 WC()->cart->empty_cart();
667
668 wp_safe_redirect( $this->get_return_url( $order ) );
669
670 exit();
671 } else {
672 $reversal_request = self::reversal_request( $invoice_number, $invoice_date,
673 $terminal_id, $merchant_id );
674
675 if ( isset( $reversal_request ) && $reversal_request->IsSuccess ) {
676 $error_code = 'reversal_done';
677 } else {
678 $error_code = 'reversal_error';
679 }
680 }
681 } else {
682 $message = $request->Message;
683 }
684 } else {
685 $message = 'پرداخت توسط ش�
686 ا انجا�
687 نشده است ، در صورت نیاز با پشتیبانی ت�
688 اس بگیرید';
689 }
690 }
691 } else {
692 $error_code = 'order_not_exist';
693 }
694 } else {
695 $error_code = 'order_not_for_this_person';
696 }
697
698 self::display_error( $error_code ?? null, $transaction_ref_id, 1, $message ?? '' );
699
700 exit;
701 }
702 }
703 }
704 }
705
706 add_action( 'before_woocommerce_init', 'wpp_pasargad_payment_gateway_init', 15 );
707 }
708