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-mellat-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-mellat-gateway.php
517 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_mellat_payment_gateway_init' ) ) {
8
9 function wpp_mellat_payment_gateway_init() {
10 if ( ! class_exists( 'WPP_WC_Mellat_Gateway' ) && ! class_exists( 'WPP_WC_Mellat_Gateway' ) ) {
11 /**
12 * WPP_WC_Mellat_Gateway class to add Mellat Bank payment gateway to WooCommerce
13 *
14 * @package WP-Parsidate
15 * @subpackage Plugins/WooCommerce/PaymentGateways
16 * @since 5.0.0
17 */
18 class WPP_WC_Mellat_Gateway extends WC_Payment_Gateway {
19
20 private $gateway_name;
21 public $method_title;
22 public $terminal_id;
23 public $username;
24 public $password;
25 public $title;
26 public $description;
27 public $failed_massage;
28
29 public function __construct() {
30 $this->id = 'mellat';
31 $this->gateway_name = esc_html__( 'Mellat Bank', 'wp-parsidate' );
32 $this->method_title = $this->gateway_name;
33 $this->method_description = $this->gateway_name . ' ' . esc_html__( 'payment gateway (By WP-Parsidate)',
34 'wp-parsidate' );
35 $this->has_fields = true;
36 $this->icon = apply_filters( $this->id . '_logo',
37 Assets::url( "images/woocommerce/$this->id-logo.png" ) );
38 $this->init_form_fields();
39 $this->init_settings();
40
41 $this->terminal_id = $this->get_option( 'terminal' );
42 $this->username = $this->get_option( 'username' );
43 $this->password = $this->get_option( 'password' );
44 $this->title = $this->get_option( 'title' );
45 $this->description = $this->get_option( 'description' );
46 $this->failed_massage = $this->get_option( 'failed_massage' );
47
48 // Save Admin Option
49 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id,
50 array( $this, 'process_admin_options' ) );
51
52 // Handle Request
53 add_action( 'woocommerce_api_' . $this->get_class(), array( $this, 'handle_gateway_response' ) );
54 }
55
56 public function get_class() {
57 return strtolower( get_class( $this ) );
58 }
59
60 public function init_form_fields() {
61 $this->form_fields = apply_filters( 'wpp_wc_' . $this->id . '_gateway_config', array(
62 'enabled' => array(
63 'title' => esc_html__( 'Enabled/Disabled', 'wp-parsidate' ),
64 'type' => 'checkbox',
65 /* translators: %s: Bank name */
66 'label' => sprintf( esc_html__( 'Activate or deactivate %s gateway',
67 'wp-parsidate' ), $this->gateway_name ),
68 'default' => 'no'
69 ),
70 'terminal' => array(
71 'title' => esc_html__( 'Terminal No.', 'wp-parsidate' ),
72 'type' => 'text',
73 'default' => '',
74 'desc_tip' => false
75 ),
76 'username' => array(
77 'title' => esc_html__( 'Gateway user name', 'wp-parsidate' ),
78 'type' => 'text',
79 'default' => '',
80 'desc_tip' => true
81 ),
82 'password' => array(
83 'title' => esc_html__( 'Gateway password', 'wp-parsidate' ),
84 'type' => 'text',
85 'default' => '',
86 'desc_tip' => true
87 ),
88 'title' => array(
89 'title' => esc_html__( 'Gateway title', 'wp-parsidate' ),
90 'type' => 'text',
91 'description' => esc_html__( 'This name is displayed to the customer during the purchase process',
92 'wp-parsidate' ),
93 'default' => $this->gateway_name
94 ),
95 'description' => array(
96 'title' => esc_html__( 'Gateway description', 'wp-parsidate' ),
97 'type' => 'textarea',
98 'description' => esc_html__( 'The description that will be displayed during the purchase process for the gateway',
99 'wp-parsidate' ),
100 /* translators: %s: Bank name */
101 'default' => sprintf( esc_html__( "Secure payment by all Shetab's cards through %s",
102 'wp-parsidate' ), $this->gateway_name )
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 )
113 );
114 }
115
116 public function get_icon() {
117 $icon = $this->icon ? '<img src="' . esc_url_raw( WC_HTTPS::force_https_url( $this->icon ) ) . '" alt="' . esc_attr( $this->get_title() ) . '" />' : '';
118
119 return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
120 }
121
122 public function process_payment( $order_id ) {
123 // Get Order
124 $order = wc_get_order( $order_id );
125
126 // Get Gateway RefId
127 $refId = $this->get_ref_id_from_mellat( $order );
128
129 // Action
130 do_action( 'wpp_wc_' . $this->id . '_gateway_process_payment', $order, $refId );
131
132 if ( $refId['status'] === false ) {
133
134 // Add Notice
135 wc_add_notice( $refId['message'], 'error' );
136
137 // Return for Block Support
138 return [
139 'result' => 'failure',
140 'messages' => $refId['message'],
141 'reload' => false
142 ];
143 }
144
145 // Save Session
146 WC()->session->set( 'mellat_ref_id', $refId['refId'] );
147 WC()->session->set( 'mellat_order_id', $order_id );
148
149 // Return
150 return [
151 'result' => 'success',
152 'redirect' => add_query_arg(
153 array(
154 'wc-api' => $this->get_class(),
155 'action' => 'redirect',
156 'order_id' => $order_id
157 ),
158 get_site_url( null, '/' )
159 )
160 ];
161 }
162
163 public function get_ref_id_from_mellat( $order ) {
164 if ( ! class_exists( 'nusoap_client' ) ) {
165 require_once( __DIR__ . '/lib/nusoap.php' );
166 }
167
168 $client = new nusoap_client( 'https://bpm.shaparak.ir/pgwchannel/services/pgw?wsdl' );
169 $err = $client->getError();
170 if ( $err ) {
171
172 return [
173 'status' => false,
174 'message' => 'خطا در ارتباط با بانک �
175 لت: ' . $err
176 ];
177 }
178
179 $description = 'خرید به ش�
180 اره سفارش: ' . $order->get_order_number();
181 $description .= ' | خریدار: ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
182
183 $parameters = apply_filters( 'wpp_wc_' . $this->id . '_gateway_request_payment', array(
184 'terminalId' => $this->terminal_id,
185 'userName' => $this->username,
186 'userPassword' => $this->password,
187 'orderId' => $order->get_id(),
188 'amount' => $this->get_amount( $order ),
189 'localDate' => date( 'Ymd' ),
190 'localTime' => date( 'His' ),
191 'additionalData' => $description,
192 'callBackUrl' => add_query_arg(
193 array(
194 'wc-api' => $this->get_class(),
195 'order_id' => $order->get_id()
196 ),
197 get_site_url( null, '/' )
198 ),
199 'payerId' => $order->get_customer_id()
200 ) );
201 $result = $client->call( 'bpPayRequest', $parameters, 'http://interfaces.core.sw.bps.com/' );
202
203 if ( $client->fault ) {
204
205 return [
206 'status' => false,
207 'message' => $this->failed_massage
208 ];
209 } else {
210
211 $resultStr = $result;
212 $err = $client->getError();
213 if ( $err ) {
214
215 return [
216 'status' => false,
217 'message' => 'خطا در ارتباط با بانک �
218 لت: ' . $err
219 ];
220 } else {
221
222 $res = explode( ',', $resultStr );
223 $ResCode = $res[0];
224 if ( $ResCode == "0" ) {
225
226 return [
227 'status' => true,
228 'refId' => $res[1]
229 ];
230 } else {
231
232 return [
233 'status' => false,
234 'message' => 'خطا در دریافت RefId از بانک �
235 لت: ' . $this->get_error_message( $ResCode )
236 ];
237 }
238 }
239 }
240 }
241
242 public function handle_gateway_response() {
243 $action = sanitize_text_field( wp_unslash( $_GET['action'] ?? '' ) );
244 $order_id = (int) sanitize_text_field( wp_unslash( $_GET['order_id'] ?? 0 ) );
245 $order = wc_get_order( $order_id );
246
247 if ( ! $order ) {
248 wp_die( 'سفارش یافت نشد' );
249 }
250
251 if ( $action === 'redirect' ) {
252 $this->redirect_to_gateway( $order );
253
254 return;
255 }
256
257 $this->verify_payment( $order );
258 }
259
260 public function redirect_to_gateway( $order ) {
261 $refId = WC()->session->get( 'mellat_ref_id' );
262
263 if ( empty( $refId ) ) {
264 wc_add_notice( $this->failed_massage, 'error' );
265 wp_safe_redirect( esc_url_raw( wc_get_checkout_url() ) );
266 exit;
267 }
268
269 ?>
270 <html lang="fa-IR">
271 <head>
272 <meta charset="UTF-8"/>
273 </head>
274 <body onload="document.forms['mellat_redirect'].submit()">
275 <form name="mellat_redirect" method="post"
276 action="https://bpm.shaparak.ir/pgwchannel/startpay.mellat">
277 <input type="hidden" name="RefId" value="<?php echo esc_attr( $refId ); ?>">
278 </form>
279 <script type="text/javascript">
280 setTimeout(function () {
281 document.forms['mellat_redirect'].submit();
282 }, 300);
283 </script>
284 </body>
285 </html>
286 <?php
287 exit;
288 }
289
290 public function verify_payment( $order ) {
291 $resCode = sanitize_text_field( wp_unslash( $_POST['ResCode'] ?? '' ) );
292 $saleOrderId = sanitize_text_field( wp_unslash( $_POST['SaleOrderId'] ?? '' ) );
293 $saleReferenceId = sanitize_text_field( wp_unslash( $_POST['SaleReferenceId'] ?? '' ) );
294 $CardHolderInfo = sanitize_text_field( wp_unslash( $_POST['CardHolderInfo'] ?? '' ) );
295 $CardHolderPan = sanitize_text_field( wp_unslash( $_POST['CardHolderPan'] ?? '' ) );
296 $FinalAmount = sanitize_text_field( wp_unslash( $_POST['FinalAmount'] ?? '' ) );
297
298 $params = [
299 'ResCode' => $resCode,
300 'SaleOrderId' => $saleOrderId,
301 'SaleReferenceId' => $saleReferenceId,
302 'CardHolderInfo' => $CardHolderInfo,
303 'CardHolderPan' => $CardHolderPan,
304 'FinalAmount' => $FinalAmount,
305 ];
306
307 if ( $resCode == '0' ) {
308
309 // Verify Payment
310 $verify_result = $this->verify( $params );
311 if ( $verify_result['status'] === true ) {
312
313 // Settle Request
314 $settle_result = $this->verify( $params, 'bpSettleRequest' );
315 if ( $settle_result['status'] === true ) {
316
317 // Set Payment Completed
318 $order->payment_complete( $saleReferenceId );
319
320 // Add Order Note
321 $order->add_order_note( sprintf( 'پرداخت با �
322 وفقیت انجا�
323 شد. کد پیگیری: %s',
324 $saleReferenceId ) );
325
326 // Remove WC Session
327 WC()->session->__unset( 'mellat_ref_id' );
328 WC()->session->__unset( 'mellat_order_id' );
329
330 // Remove cart.
331 WC()->cart->empty_cart();
332
333 // Action
334 do_action( 'wpp_wc_' . $this->id . '_gateway_completed_payment', $order, $params );
335
336 // Redirect
337 wp_safe_redirect( esc_url_raw( $this->get_return_url( $order ) ) );
338 exit;
339 } else {
340
341 $this->set_failed_payment( $order );
342 }
343 } else {
344
345 $this->set_failed_payment( $order );
346 }
347 } else {
348
349 $error_message = $this->get_error_message( $resCode );
350 wc_add_notice( $error_message, 'error' );
351 do_action( 'wpp_wc_' . $this->id . '_gateway_failed_payment', $order );
352 wp_safe_redirect( esc_url_raw( wc_get_checkout_url() ) );
353 exit;
354 }
355 }
356
357 public function get_amount( $order ) {
358 $currency = $order->get_currency();
359 $order_total = $order->get_total();
360 $amount = (int) $order_total;
361 $currency = strtolower( $currency );
362
363 if ( in_array( $currency, array(
364 'irt',
365 'toman',
366 'iran toman',
367 'iranian toman',
368 'iran-toman',
369 'iran_toman',
370 'تو�
371 ان',
372 'تو�
373 ان ایران'
374 ) ) ) {
375 $amount = $amount * 10;
376 } elseif ( 'irht' === $currency ) {
377 $amount = $amount * 1000 * 10;
378 } elseif ( 'irhr' === $currency ) {
379 $amount = $amount * 1000;
380 }
381
382 return $amount;
383 }
384
385 public function set_failed_payment( $order ) {
386 wc_add_notice( $this->failed_massage, 'error' );
387 do_action( 'wpp_wc_' . $this->id . '_gateway_failed_payment', $order );
388 wp_safe_redirect( esc_url_raw( wc_get_checkout_url() ) );
389 exit;
390 }
391
392 public function verify( $params, $method = 'bpVerifyRequest' ): array {
393 if ( ! class_exists( 'nusoap_client' ) ) {
394 require_once( __DIR__ . '/lib/nusoap.php' );
395 }
396
397 $client = new nusoap_client( 'https://bpm.shaparak.ir/pgwchannel/services/pgw?wsdl' );
398 $orderId = $params["SaleOrderId"];
399 $verifySaleReferenceId = $params['SaleReferenceId'];
400
401 $err = $client->getError();
402 if ( $err ) {
403
404 return [
405 'status' => false,
406 'message' => $err,
407 'code' => ''
408 ];
409 }
410
411 $parameters = array(
412 'terminalId' => $this->terminal_id,
413 'userName' => $this->username,
414 'userPassword' => $this->password,
415 'orderId' => $orderId,
416 'saleOrderId' => $orderId,
417 'saleReferenceId' => $verifySaleReferenceId
418 );
419 $result = $client->call( $method, $parameters, 'http://interfaces.core.sw.bps.com/' );
420
421 // Check Success
422 if ( $result == '0' ) {
423
424 return [
425 'status' => true
426 ];
427 }
428
429 // Check Error Code
430 if ( is_numeric( $result ) ) {
431
432 return [
433 'status' => false,
434 'message' => 'خطا در اعتبار سنجی پرداخت �
435 لت رخ داده است. کد خطا: ' . $this->get_error_message( $result ),
436 'code' => ''
437 ];
438 }
439
440 return [
441 'status' => false,
442 'message' => $this->failed_massage,
443 'code' => ''
444 ];
445 }
446
447 public function get_error_message( $resCode ) {
448 $messages = array(
449 '11' => 'ش�
450 اره کارت نا�
451 عتبر است',
452 '12' => '�
453 وجودی کافی نیست',
454 '13' => 'ر�
455 ز نادرست است',
456 '14' => 'تعداد دفعات وارد کردن ر�
457 ز بیش از حد �
458 جاز است',
459 '15' => 'کارت نا�
460 عتبر است',
461 '16' => 'دفعات برداشت وجه بیش از حد �
462 جاز است',
463 '17' => 'کاربر از انجا�
464 تراکنش �
465 نصرف شده است',
466 '18' => 'تاریخ انقضای کارت گذشته است',
467 '19' => '�
468 بلغ برداشت وجه بیش از حد �
469 جاز است',
470 '21' => 'پذیرنده نا�
471 عتبر است',
472 '23' => 'خطای ا�
473 نیتی رخ داده است',
474 '24' => 'اطلاعات کاربری پذیرنده نا�
475 عتبر است',
476 '25' => '�
477 بلغ نا�
478 عتبر است',
479 '31' => 'پاسخ نا�
480 عتبر است',
481 '32' => 'فر�
482 ت اطلاعات وارد شده صحیح نیست',
483 '33' => 'حساب نا�
484 عتبر است',
485 '34' => 'خطای سیست�
486 ی',
487 '35' => 'تاریخ نا�
488 عتبر است',
489 '41' => 'ش�
490 اره درخواست تکراری است',
491 '42' => 'تراکنش Sale یافت نشد',
492 '43' => 'قبلا درخواست Verify داده شده است',
493 '44' => 'درخواست Verify یافت نشد',
494 '45' => 'تراکنش Settle شده است',
495 '46' => 'تراکنش Settle نشده است',
496 '47' => 'تراکنش Settle یافت نشد',
497 '48' => 'تراکنش Reverse شده است',
498 '49' => 'تراکنش Refund یافت نشد',
499 '51' => 'تراکنش تکراری است',
500 '54' => 'تراکنش �
501 رجع �
502 وجود نیست',
503 '55' => 'تراکنش نا�
504 عتبر است',
505 '61' => 'خطا در واریز'
506 );
507
508 return $messages[ $resCode ] ?? 'کد خطا: ' . $resCode;
509 }
510
511 }
512 }
513 }
514
515 add_action( 'before_woocommerce_init', 'wpp_mellat_payment_gateway_init', 15 );
516 }
517