-
Notifications
You must be signed in to change notification settings - Fork 0
/
tradesafe-payment-gateway.php
405 lines (335 loc) · 12 KB
/
tradesafe-payment-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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/**
* Plugin Name: TradeSafe Payment Gateway
* Plugin URI: https://developer.tradesafe.co.za/docs/1.2/plugins/woocommerce
* Description: Process secure payments with TradeSafe. Customers can make payment either through Card, EFT, Instant EFT, Buy Now Pay Later, or QR code enabled devices. Give your store instant credibility. TradeSafe is backed by Standard Bank.
* Version: 2.18.12
* Author: TradeSafe Escrow
* Author URI: https://www.tradesafe.co.za
* Text Domain: tradesafe-payment-gateway
* Requires Plugins: woocommerce
* Requires at least: 5.5
* Requires PHP: 7.4
* Tested up to: 6.6
* WC tested up to: 8.9
* WC requires at least: 4.6
*
* @package TradeSafe Payment Gateway
*/
defined( 'ABSPATH' ) || exit;
define( 'WC_GATEWAY_TRADESAFE_VERSION', '2.18.12' );
define( 'TRADESAFE_PAYMENT_GATEWAY_BASE_DIR', untrailingslashit( plugin_dir_url( __FILE__ ) ) );
define( 'TRADESAFE_PAYMENT_GATEWAY_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
/**
* Initialize the gateway.
*
* @since 1.0.0
*/
function tradesafe_payment_gateway_init() {
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
return;
}
$autoloader = dirname( __DIR__ ) . DIRECTORY_SEPARATOR . plugin_basename( __DIR__ ) . '/vendor/autoload.php';
if ( ! is_readable( $autoloader ) ) {
return;
}
$autoloader_result = require $autoloader;
if ( ! $autoloader_result ) {
return;
}
$settings = get_option( 'woocommerce_tradesafe_settings' );
if ( ! isset( $settings['client_id'] ) || ( '' === $settings['client_id'] && '' !== get_option( 'tradesafe_client_id', '' ) ) ) {
$settings['client_id'] = get_option( 'tradesafe_client_id' );
$settings['client_secret'] = get_option( 'tradesafe_client_secret' );
$settings['industry'] = get_option( 'tradesafe_transaction_industry' );
$settings['environment'] = get_option( 'tradesafe_production_mode', 'SANDBOX' ) ? 'PROD' : 'SANDBOX';
$settings['is_marketplace'] = get_option( 'tradesafe_transaction_marketplace', null ) ? 'yes' : 'no';
$settings['processing_fee'] = get_option( 'tradesafe_transaction_fee_allocation' );
$settings['commission'] = get_option( 'tradesafe_transaction_fee', null );
$settings['commission_type'] = get_option( 'tradesafe_transaction_fee_type', null );
update_option( 'woocommerce_tradesafe_settings', apply_filters( 'woocommerce_settings_api_sanitized_fields_tradesafe', $settings ), 'yes' );
}
require_once plugin_basename( 'src/class-tradesafe.php' );
require_once plugin_basename( 'src/class-tradesafedokan.php' );
require_once plugin_basename( 'src/class-tradesafeprofile.php' );
require_once plugin_basename( 'src/class-wc-gateway-tradesafe.php' );
require_once plugin_basename( 'helpers/class-tradesafeapiclient.php' );
add_action( 'init', array( 'TradeSafe', 'init' ) );
add_action( 'init', array( 'TradeSafeProfile', 'init' ) );
add_action( 'init', array( 'TradeSafeDokan', 'init' ) );
load_plugin_textdomain( 'tradesafe-payment-gateway', false, trailingslashit( dirname( plugin_basename( __FILE__ ) ) ) );
add_filter( 'woocommerce_payment_gateways', 'tradesafe_payment_gateway_add' );
}
add_action( 'plugins_loaded', 'tradesafe_payment_gateway_init', 10 );
add_action( 'plugins_loaded', 'tradesafe_payment_gateway_update_db_check', 15 );
add_action( 'before_woocommerce_init', 'tradesafe_payment_gateway_declare_feature_compatibility' );
add_action( 'woocommerce_blocks_loaded', 'tradesafe_payment_gateway_woocommerce_blocks_support' );
/**
* Add action links to the entry on the plugin page.
*
* @param array $links Array of action links.
* @return array
*/
function woocommerce_tradesafe_plugin_links( $links ): array {
$settings_url = add_query_arg(
array(
'page' => 'wc-settings',
'tab' => 'checkout',
'section' => 'tradesafe',
),
admin_url( 'admin.php' )
);
$plugin_links = array(
'<a href="' . esc_url( $settings_url ) . '">' . __( 'Settings', 'tradesafe-payment-gateway' ) . '</a>',
);
return array_merge( $plugin_links, $links );
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'woocommerce_tradesafe_plugin_links' );
/**
* Add additional links under the plugin description.
*
* @param array $links An array of existing links.
* @param string $file Name of the plugin file been loaded.
* @return array
*/
function tradesafe_payment_gateway_plugin_row_meta( array $links, string $file ): array {
if ( strpos( $file, 'tradesafe-payment-gateway.php' ) !== false ) {
$new_links = array(
'<a href="https://developer.tradesafe.co.za/docs/1.2/plugins/woocommerce">' . __( 'Docs', 'tradesafe-payment-gateway' ) . '</a>',
'<a href="https://www.tradesafe.co.za/support/">' . __( 'Support', 'tradesafe-payment-gateway' ) . '</a>',
);
$links = array_merge( $links, $new_links );
}
return $links;
}
add_filter( 'plugin_row_meta', 'tradesafe_payment_gateway_plugin_row_meta', 10, 2 );
/**
* Add the gateway to WooCommerce.
*
* @param array $methods Array of payment gateway methods.
* @since 1.0.0
*/
function tradesafe_payment_gateway_add( $methods ) {
$methods[] = 'WC_Gateway_TradeSafe';
return $methods;
}
/**
* Check if production environment is enabled.
*
* @return bool
*/
function tradesafe_is_prod(): bool {
$settings = get_option( 'woocommerce_tradesafe_settings', array() );
if ( isset( $settings['environment'] ) ) {
return 'PROD' === $settings['environment'];
}
return get_option( 'tradesafe_production_mode', false );
}
/**
* Check if production environment is enabled.
*
* @return bool
*/
function tradesafe_is_marketplace(): bool {
$settings = get_option( 'woocommerce_tradesafe_settings', array() );
if ( tradesafe_has_dokan() ) {
return true;
}
if ( isset( $settings['is_marketplace'] ) ) {
return 'yes' === $settings['is_marketplace'];
}
return get_option( 'tradesafe_transaction_marketplace', false );
}
/**
* Return true if Dokan class exists.
*
* @return bool
*/
function tradesafe_has_dokan(): bool {
return class_exists( 'WeDevs_Dokan' );
}
/**
* Who pays the escrow fee.
*
* @return string
*/
function tradesafe_fee_allocation(): string {
$settings = get_option( 'woocommerce_tradesafe_settings', array() );
$fee_allocation = get_option( 'processing_fee', 'SELLER' );
if ( isset( $settings['processing_fee'] ) ) {
$fee_allocation = $settings['processing_fee'];
}
// Check that valid value is set
switch ( $fee_allocation ) {
case 'BUYER':
case 'SELLER':
case 'BUYER_SELLER':
return $fee_allocation;
default:
return 'SELLER';
}
}
/**
* Which industry does the store operate.
*
* @return string
*/
function tradesafe_industry(): string {
$settings = get_option( 'woocommerce_tradesafe_settings', array() );
if ( isset( $settings['industry'] ) ) {
return $settings['industry'];
}
return get_option( 'tradesafe_transaction_industry' );
}
/**
* What type of fee is charged.
*
* @return string
*/
function tradesafe_commission_type(): string {
$settings = get_option( 'woocommerce_tradesafe_settings', array() );
if ( isset( $settings['commission_type'] ) ) {
return $settings['commission_type'];
}
return get_option( 'tradesafe_transaction_fee_type' );
}
/**
* What is the value of the fee charged.
*
* @return string
*/
function tradesafe_commission_value(): string {
$settings = get_option( 'woocommerce_tradesafe_settings', array() );
if ( isset( $settings['commission'] ) ) {
return $settings['commission'];
}
return get_option( 'tradesafe_transaction_fee' );
}
/**
* Set meta key based on environment.
*
* @return string
*/
function tradesafe_token_meta_key(): string {
$meta_key = 'tradesafe_token_id';
if ( tradesafe_is_prod() ) {
$meta_key = 'tradesafe_prod_token_id';
}
return $meta_key;
}
/**
* Get or create token ID for user.
*
* @param int $user_id
* @return string
* @throws Exception
*/
function tradesafe_get_token_id( int $user_id ): ?string {
$token = get_user_meta( $user_id, tradesafe_token_meta_key(), true );
if ( ! empty( $token ) ) {
return $token;
}
// If Token was not found for user create one and return the id
$client = new \TradeSafe\Helpers\TradeSafeApiClient();
$settings = get_option( 'woocommerce_tradesafe_settings', array() );
$customer = new WC_Customer( $user_id );
$payout_interval = 'IMMEDIATE';
if ( isset( $settings['payout_method'] ) ) {
$payout_interval = $settings['payout_method'];
}
$user = array(
'givenName' => $customer->get_first_name() ?? null,
'familyName' => $customer->get_last_name() ?? null,
'email' => $customer->get_email(),
'mobile' => $customer->get_billing_phone() ?? null,
);
try {
$token_data = $client->createToken(
$user,
null,
null,
$payout_interval
);
$customer->update_meta_data( tradesafe_token_meta_key(), sanitize_text_field( $token_data['id'] ) );
$customer->save_meta_data();
return sanitize_text_field( $token_data['id'] );
} catch ( \GraphQL\Exception\QueryError $e ) {
$logger = wc_get_logger();
$logger->error( $e->getMessage() . ': ' . $e->getErrorDetails()['message'] ?? null, array( 'source' => 'tradesafe-payment-gateway' ) );
} catch ( \Exception $e ) {
$logger = wc_get_logger();
$logger->error( $e->getMessage() ?? null, array( 'source' => 'tradesafe-payment-gateway' ) );
} catch ( \Throwable $e ) {
$logger = wc_get_logger();
$logger->error( $e->getMessage() ?? null, array( 'source' => 'tradesafe-payment-gateway' ) );
}
return null;
}
/**
* Get or create token ID for user.
*
* @param int $user_id
* @return string
*/
function tradesafe_get_token( int $user_id ): ?array {
try {
$tokenId = get_user_meta( $user_id, tradesafe_token_meta_key(), true );
// If Token was not found for user create one and return the id
$client = new \TradeSafe\Helpers\TradeSafeApiClient();
return $client->getToken( $tokenId );
} catch ( \GraphQL\Exception\QueryError $e ) {
$logger = wc_get_logger();
$logger->error( $e->getMessage() . ': ' . $e->getErrorDetails()['message'] ?? null, array( 'source' => 'tradesafe-payment-gateway' ) );
} catch ( \Exception $e ) {
$logger = wc_get_logger();
$logger->error( $e->getMessage() ?? null, array( 'source' => 'tradesafe-payment-gateway' ) );
} catch ( \Throwable $e ) {
$logger = wc_get_logger();
$logger->error( $e->getMessage() ?? null, array( 'source' => 'tradesafe-payment-gateway' ) );
}
return null;
}
/**
* Ensure description is updated after plugin update.
*
* @return void
*/
function tradesafe_payment_gateway_update_db_check() {
if ( defined( 'WC_GATEWAY_TRADESAFE_VERSION' ) && get_option( 'tradesafe_payment_gateway_version' ) !== WC_GATEWAY_TRADESAFE_VERSION ) {
$settings = get_option( 'woocommerce_tradesafe_settings' );
$settings['description'] = __( 'TradeSafe, backed by Standard Bank, allows for your money to be kept safely until you receive what you ordered. Simply pay using Credit/Debit card, EFT, SnapScan, Ozow, or buy it now and pay later with PayJustNow.', 'tradesafe-payment-gateway' );
update_option( 'woocommerce_tradesafe_settings', apply_filters( 'woocommerce_settings_api_sanitized_fields_tradesafe', $settings ), 'yes' );
update_option( 'tradesafe_payment_gateway_version', WC_GATEWAY_TRADESAFE_VERSION );
}
}
/**
* Custom function to register a payment method type
*/
function tradesafe_payment_gateway_woocommerce_blocks_support() {
if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
require_once TRADESAFE_PAYMENT_GATEWAY_PATH . '/src/class-wc-gateway-tradesafe-blocks-support.php';
add_action(
'woocommerce_blocks_payment_method_type_registration',
function ( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
$payment_method_registry->register( new WC_Gateway_TradeSafe_Blocks_Support() );
}
);
}
}
/**
* Declares compatibility with Woocommerce features.
*
* List of features:
* - custom_order_tables
*
* @return void
*/
function tradesafe_payment_gateway_declare_feature_compatibility() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
'custom_order_tables',
__FILE__,
true
);
}
}