-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathclass-wc-gateway-paystack-blocks-support.php
More file actions
120 lines (103 loc) · 3.78 KB
/
Copy pathclass-wc-gateway-paystack-blocks-support.php
File metadata and controls
120 lines (103 loc) · 3.78 KB
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
<?php
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use Automattic\WooCommerce\StoreApi\Payments\PaymentContext;
use Automattic\WooCommerce\StoreApi\Payments\PaymentResult;
final class WC_Gateway_Paystack_Blocks_Support extends AbstractPaymentMethodType {
/**
* Payment method name/id/slug.
*
* @var string
*/
protected $name = 'paystack';
/**
* Initializes the payment method type.
*/
public function initialize() {
$this->settings = get_option( 'woocommerce_paystack_settings', array() );
add_action( 'woocommerce_rest_checkout_process_payment_with_context', array( $this, 'failed_payment_notice' ), 8, 2 );
}
/**
* Returns if this payment method should be active. If false, the scripts will not be enqueued.
*
* @return boolean
*/
public function is_active() {
$payment_gateways_class = WC()->payment_gateways();
// Check if payment gateways are available
if ($payment_gateways_class) {
$payment_gateways = $payment_gateways_class->payment_gateways();
// Check if payment gateways is not empty and Paystack gateway exists
if (!empty($payment_gateways) && isset($payment_gateways['paystack'])) {
// Check if Paystack gateway is available
return $payment_gateways['paystack']->is_available();
} else {
// Paystack gateway not found
return false;
}
} else {
// Payment gateways not initialized
throw new Exception('Payment gateways not initialized');
}
}
/**
* Returns an array of scripts/handles to be registered for this payment method.
*
* @return array
*/
public function get_payment_method_script_handles() {
$script_asset_path = plugins_url( '/assets/js/blocks/frontend/blocks.asset.php', WC_PAYSTACK_MAIN_FILE );
$script_asset = file_exists( $script_asset_path )
? require $script_asset_path
: array(
'dependencies' => array(),
'version' => WC_PAYSTACK_VERSION,
);
$script_url = plugins_url( '/assets/js/blocks/frontend/blocks.js', WC_PAYSTACK_MAIN_FILE );
wp_register_script(
'wc-paystack-blocks',
$script_url,
$script_asset['dependencies'],
$script_asset['version'],
true
);
if ( function_exists( 'wp_set_script_translations' ) ) {
wp_set_script_translations( 'wc-paystack-blocks', 'woo-paystack', );
}
return array( 'wc-paystack-blocks' );
}
/**
* Returns an array of key=>value pairs of data made available to the payment methods script.
*
* @return array
*/
public function get_payment_method_data() {
$payment_gateways_class = WC()->payment_gateways();
$payment_gateways = $payment_gateways_class->payment_gateways();
$gateway = $payment_gateways['paystack'];
return array(
'title' => $this->get_setting( 'title' ),
'description' => $this->get_setting( 'description' ),
'supports' => array_filter( $gateway->supports, array( $gateway, 'supports' ) ),
'allow_saved_cards' => $gateway->saved_cards && is_user_logged_in(),
'logo_urls' => array( $payment_gateways['paystack']->get_logo_url() ),
);
}
/**
* Add failed payment notice to the payment details.
*
* @param PaymentContext $context Holds context for the payment.
* @param PaymentResult $result Result object for the payment.
*/
public function failed_payment_notice( PaymentContext $context, PaymentResult &$result ) {
if ( 'paystack' === $context->payment_method ) {
add_action(
'wc_gateway_paystack_process_payment_error',
function( $failed_notice ) use ( &$result ) {
$payment_details = $result->payment_details;
$payment_details['errorMessage'] = wp_strip_all_tags( $failed_notice );
$result->set_payment_details( $payment_details );
}
);
}
}
}