PluginProbe ʕ •ᴥ•ʔ
پارسی دیت – Parsi Date / 6.2.1
پارسی دیت – Parsi Date v6.2.1
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-melli-gateway.php
wp-parsidate / inc / App / Integration / WooCommerce / wc-gateways Last commit date
blocks 1 month ago lib 1 month ago index.php 1 month ago wpp-mellat-gateway.php 1 month ago wpp-melli-gateway.php 1 month ago wpp-parsian-gateway.php 1 month ago wpp-pasargad-gateway.php 1 month ago
wpp-melli-gateway.php
344 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_melli_payment_gateway_init' ) ) {
8
9 function wpp_melli_payment_gateway_init() {
10 if ( ! class_exists( 'WPP_WC_Melli_Gateway' ) && ! class_exists( 'WPP_WC_Melli_Gateway' ) ) {
11 /**
12 * WPP_WC_Melli_Gateway class to add Melli 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_Melli_Gateway extends WC_Payment_Gateway {
19 private $gateway_name;
20 public $terminal_id;
21 public $merchant_id;
22 public $key;
23 public $title;
24 public $description;
25 public $failed_massage;
26
27 public function __construct() {
28 $this->id = 'melli';
29 $this->gateway_name = esc_html__( 'Melli Bank', 'wp-parsidate' );
30 $this->method_title = $this->gateway_name;
31 $this->method_description = $this->gateway_name . ' ' . esc_html__( 'payment gateway (By WP-Parsidate)',
32 'wp-parsidate' );
33 $this->has_fields = true;
34 $this->icon = apply_filters( $this->id . '_logo',
35 Assets::url( "images/woocommerce/$this->id-logo.png" ) );
36
37 $this->init_form_fields();
38 $this->init_settings();
39
40 $this->terminal_id = $this->get_option( 'terminal_id' );
41 $this->merchant_id = $this->get_option( 'merchant_id' );
42 $this->key = $this->get_option( 'key' );
43 $this->title = $this->get_option( 'title' );
44 $this->description = $this->get_option( 'description' );
45 $this->failed_massage = $this->get_option( 'failed_massage' );
46
47 // Save Admin Option
48 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id,
49 array( $this, 'process_admin_options' ) );
50
51 // Handle Request
52 add_action( 'woocommerce_api_' . $this->get_class(), array( $this, 'handle_gateway_response' ) );
53 }
54
55 public function get_class() {
56 return strtolower( get_class( $this ) );
57 }
58
59 public function init_form_fields() {
60 $this->form_fields = apply_filters( 'wpp_wc_' . $this->id . '_gateway_config', array(
61 'enabled' => array(
62 'title' => esc_html__( 'Enabled/Disabled', 'wp-parsidate' ),
63 'type' => 'checkbox',
64 /* translators: %s: Bank name */
65 'label' => sprintf( esc_html__( 'Activate or deactivate %s gateway', 'wp-parsidate' ),
66 $this->gateway_name ),
67 'default' => 'no',
68 'description' => ( $this->is_enable_open_ssl() === false ? '<span style="color: red;">توجه: جهت فعال سازی درگاه �
69 ی بایست �
70 اژول OpenSSL در تنظی�
71 ات PHP هاست ش�
72 ا فعال باشد</span>' : '' ),
73 ),
74 'terminal_id' => array(
75 'title' => esc_html__( 'Terminal No.', 'wp-parsidate' ),
76 'type' => 'text',
77 'default' => '',
78 'desc_tip' => false
79 ),
80 'merchant_id' => array(
81 'title' => esc_html__( 'Merchant ID', 'wp-parsidate' ),
82 'type' => 'text',
83 'default' => '',
84 'desc_tip' => false
85 ),
86 'key' => array(
87 'title' => esc_html__( 'Gateway Key', 'wp-parsidate' ),
88 'type' => 'text',
89 'default' => '',
90 'desc_tip' => false
91 ),
92 'title' => array(
93 'title' => esc_html__( 'Gateway title', 'wp-parsidate' ),
94 'type' => 'text',
95 'description' => esc_html__( 'This name is displayed to the customer during the purchase process',
96 'wp-parsidate' ),
97 'default' => $this->gateway_name
98 ),
99 'description' => array(
100 'title' => esc_html__( 'Gateway description', 'wp-parsidate' ),
101 'type' => 'textarea',
102 'description' => esc_html__( 'The description that will be displayed during the purchase process for the gateway',
103 'wp-parsidate' ),
104 /* translators: %s: Bank name */
105 'default' => sprintf( esc_html__( "Secure payment by all Shetab's cards through %s",
106 'wp-parsidate' ), $this->gateway_name )
107 ),
108 'failed_massage' => array(
109 'title' => esc_html__( 'Payment failed message', 'wp-parsidate' ),
110 'type' => 'textarea',
111 'description' => esc_html__( 'Enter the text of the message you want to display to the user after an unsuccessful payment.',
112 'wp-parsidate' ),
113 'default' => esc_html__( 'Your payment has failed. Please try again or contact us in case of problems.',
114 'wp-parsidate' )
115 )
116 )
117 );
118 }
119
120 public function get_icon() {
121 $icon = $this->icon ? '<img src="' . esc_url_raw( WC_HTTPS::force_https_url( $this->icon ) ) . '" alt="' . esc_attr( $this->get_title() ) . '" />' : '';
122
123 return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
124 }
125
126 public function is_available() {
127 return parent::is_available();
128 }
129
130 public function encrypt_pkcs7( $str, $key ): string {
131 $key = base64_decode( $key );
132 $ciphertext = OpenSSL_encrypt( $str, "DES-EDE3", $key, OPENSSL_RAW_DATA );
133
134 return base64_encode( $ciphertext );
135 }
136
137 public function api( $url, $data = false ) {
138 try {
139 $args = array(
140 'headers' => array(
141 'Content-Type' => 'application/json; charset=utf-8',
142 ),
143 'timeout' => 20,
144 'sslverify' => false
145 );
146
147 if ( $data ) {
148 $args['body'] = json_encode( $data );
149 }
150
151 $response = wp_remote_post( $url, $args );
152
153 if ( is_wp_error( $response ) ) {
154 return false;
155 }
156
157 $body = wp_remote_retrieve_body( $response );
158
159 return ! empty( $body ) ? json_decode( $body ) : false;
160 } catch ( Exception $ex ) {
161 return false;
162 }
163 }
164
165 public function is_enable_open_ssl(): bool {
166 return ( extension_loaded( 'openssl' ) and function_exists( 'OpenSSL_encrypt' ) );
167 }
168
169 public function process_payment( $order_id ) {
170 // Get Order
171 $order = wc_get_order( $order_id );
172
173 // Get Gateway RefId
174 $refId = $this->get_ref_id_from_melli( $order );
175
176 // Action
177 do_action( 'wpp_wc_' . $this->id . '_gateway_process_payment', $order, $refId );
178
179 if ( $refId['status'] === false ) {
180
181 // setup Error Text
182 $errorText = 'خطا در اتصال به درگاه پرداخت: ' . $refId['message'];
183
184 // Add Notice
185 wc_add_notice( $errorText, 'error' );
186
187 // Return for Block Support
188 return [
189 'result' => 'failure',
190 'messages' => $refId['message'],
191 'reload' => false
192 ];
193 }
194
195 // Return
196 return [
197 'result' => 'success',
198 'redirect' => $refId['redirect']
199 ];
200 }
201
202 public function get_ref_id_from_melli( $order ): array {
203 $Amount = $this->get_amount( $order );
204 $OrderId = $order->get_id();
205 $LocalDateTime = date( "m/d/Y g:i:s a" );
206 $TerminalId = $this->terminal_id;
207 $key = $this->key;
208 $SignData = $this->encrypt_pkcs7( "$TerminalId;$OrderId;$Amount", "$key" );
209
210 $data = apply_filters( 'wpp_wc_' . $this->id . '_gateway_request_payment', array(
211 'TerminalId' => $this->terminal_id,
212 'MerchantId' => $this->merchant_id,
213 'Amount' => $this->get_amount( $order ),
214 'SignData' => $SignData,
215 'ReturnUrl' => add_query_arg(
216 array(
217 'wc-api' => $this->get_class(),
218 'order_id' => $order->get_id()
219 ),
220 get_site_url( null, '/' )
221 ),
222 'LocalDateTime' => $LocalDateTime,
223 'OrderId' => $OrderId
224 ) );
225
226 $result = $this->api( 'https://sadad.shaparak.ir/vpg/api/v0/Request/PaymentRequest', $data );
227
228 if ( $result->ResCode == 0 ) {
229
230 return [
231 'status' => true,
232 'token' => $result->Token,
233 'redirect' => "https://sadad.shaparak.ir/VPG/Purchase?Token=" . $result->Token
234 ];
235 }
236
237 return [
238 'status' => false,
239 'message' => $result->Description
240 ];
241 }
242
243 public function handle_gateway_response() {
244 $action = sanitize_text_field( wp_unslash( $_GET['action'] ?? '' ) );
245 $order_id = (int) sanitize_text_field( wp_unslash( $_GET['order_id'] ?? 0 ) );
246 $order = wc_get_order( $order_id );
247
248 if ( $action === 'redirect' ) {
249 $this->redirect_to_gateway( $order );
250
251 return;
252 }
253
254 $this->verify_payment( $order );
255 }
256
257 public function redirect_to_gateway( $order ) {
258 // Don't Need
259 }
260
261 public function verify_payment( $order ) {
262 $OrderId = sanitize_text_field( wp_unslash( $_POST["OrderId"] ?? '' ) );
263 $Token = sanitize_text_field( wp_unslash( $_POST["token"] ?? '' ) );
264 $ResCode = sanitize_text_field( wp_unslash( $_POST["ResCode"] ?? '' ) );
265
266 if ( $ResCode == 0 ) {
267
268 $verifyData = [
269 'Token' => $Token,
270 'SignData' => $this->encrypt_pkcs7( $Token, $this->key )
271 ];
272 $result = $this->api( 'https://sadad.shaparak.ir/vpg/api/v0/Advice/Verify', $verifyData );
273
274 if ( $result->ResCode != - 1 && $result->ResCode == 0 ) {
275
276 // Set Payment Completed
277 $order->payment_complete( $result->RetrivalRefNo );
278
279 // Add Order Note
280 $order->add_order_note( sprintf( 'پرداخت با �
281 وفقیت انجا�
282 شد. کد پیگیری: %s',
283 $result->RetrivalRefNo ) );
284
285 // Remove cart.
286 WC()->cart->empty_cart();
287
288 // Action
289 do_action( 'wpp_wc_' . $this->id . '_gateway_completed_payment', $order, [
290 'RetrivalRefNo' => $result->RetrivalRefNo,
291 'SystemTraceNo' => $result->SystemTraceNo,
292 'OrderId' => $result->OrderId,
293 ] );
294
295 // Redirect
296 wp_safe_redirect( esc_url_raw( $this->get_return_url( $order ) ) );
297 exit;
298 }
299 }
300
301 $this->set_failed_payment( $order );
302 }
303
304 public function get_amount( $order ) {
305 $currency = $order->get_currency();
306 $order_total = $order->get_total();
307 $amount = (int) $order_total;
308 $currency = strtolower( $currency );
309
310 if ( in_array( $currency, array(
311 'irt',
312 'toman',
313 'iran toman',
314 'iranian toman',
315 'iran-toman',
316 'iran_toman',
317 'تو�
318 ان',
319 'تو�
320 ان ایران'
321 ) ) ) {
322 $amount *= 10;
323 } elseif ( 'irht' === $currency ) {
324 $amount *= 1000 * 10;
325 } elseif ( 'irhr' === $currency ) {
326 $amount *= 1000;
327 }
328
329 return $amount;
330 }
331
332 public function set_failed_payment( $order ) {
333 wc_add_notice( $this->failed_massage, 'error' );
334 do_action( 'wpp_wc_' . $this->id . '_gateway_failed_payment', $order );
335 wp_safe_redirect( esc_url_raw( wc_get_checkout_url() ) );
336 exit;
337 }
338 }
339 }
340 }
341
342 add_action( 'before_woocommerce_init', 'wpp_melli_payment_gateway_init', 15 );
343 }
344