|
| 1 | +<?php |
| 2 | + |
| 3 | +use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; |
| 4 | +use Automattic\WooCommerce\StoreApi\Payments\PaymentContext; |
| 5 | +use Automattic\WooCommerce\StoreApi\Payments\PaymentResult; |
| 6 | + |
| 7 | +final class WC_Gateway_Paystack_Blocks_Support extends AbstractPaymentMethodType { |
| 8 | + |
| 9 | + /** |
| 10 | + * Payment method name/id/slug. |
| 11 | + * |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + protected $name = 'paystack'; |
| 15 | + |
| 16 | + /** |
| 17 | + * Initializes the payment method type. |
| 18 | + */ |
| 19 | + public function initialize() { |
| 20 | + $this->settings = get_option( 'woocommerce_paystack_settings', array() ); |
| 21 | + |
| 22 | + add_action( 'woocommerce_rest_checkout_process_payment_with_context', array( $this, 'failed_payment_notice' ), 8, 2 ); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Returns if this payment method should be active. If false, the scripts will not be enqueued. |
| 27 | + * |
| 28 | + * @return boolean |
| 29 | + */ |
| 30 | + public function is_active() { |
| 31 | + $payment_gateways_class = WC()->payment_gateways(); |
| 32 | + $payment_gateways = $payment_gateways_class->payment_gateways(); |
| 33 | + return $payment_gateways['paystack']->is_available(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns an array of scripts/handles to be registered for this payment method. |
| 38 | + * |
| 39 | + * @return array |
| 40 | + */ |
| 41 | + public function get_payment_method_script_handles() { |
| 42 | + $script_asset_path = plugins_url( '/assets/js/blocks/frontend/blocks.asset.php', WC_PAYSTACK_MAIN_FILE ); |
| 43 | + $script_asset = file_exists( $script_asset_path ) |
| 44 | + ? require $script_asset_path |
| 45 | + : array( |
| 46 | + 'dependencies' => array(), |
| 47 | + 'version' => WC_PAYSTACK_VERSION, |
| 48 | + ); |
| 49 | + |
| 50 | + $script_url = plugins_url( '/assets/js/blocks/frontend/blocks.js', WC_PAYSTACK_MAIN_FILE ); |
| 51 | + |
| 52 | + wp_register_script( |
| 53 | + 'wc-paystack-blocks', |
| 54 | + $script_url, |
| 55 | + $script_asset['dependencies'], |
| 56 | + $script_asset['version'], |
| 57 | + true |
| 58 | + ); |
| 59 | + |
| 60 | + if ( function_exists( 'wp_set_script_translations' ) ) { |
| 61 | + wp_set_script_translations( 'wc-paystack-blocks', 'woo-paystack', ); |
| 62 | + } |
| 63 | + |
| 64 | + return array( 'wc-paystack-blocks' ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns an array of key=>value pairs of data made available to the payment methods script. |
| 69 | + * |
| 70 | + * @return array |
| 71 | + */ |
| 72 | + public function get_payment_method_data() { |
| 73 | + $payment_gateways_class = WC()->payment_gateways(); |
| 74 | + $payment_gateways = $payment_gateways_class->payment_gateways(); |
| 75 | + $gateway = $payment_gateways['paystack']; |
| 76 | + |
| 77 | + return array( |
| 78 | + 'title' => $this->get_setting( 'title' ), |
| 79 | + 'description' => $this->get_setting( 'description' ), |
| 80 | + 'supports' => array_filter( $gateway->supports, array( $gateway, 'supports' ) ), |
| 81 | + 'allow_saved_cards' => $gateway->saved_cards && is_user_logged_in(), |
| 82 | + 'logo_url' => array( $payment_gateways['paystack']->get_logo_url() ), |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Add failed payment notice to the payment details. |
| 88 | + * |
| 89 | + * @param PaymentContext $context Holds context for the payment. |
| 90 | + * @param PaymentResult $result Result object for the payment. |
| 91 | + */ |
| 92 | + public function failed_payment_notice( PaymentContext $context, PaymentResult &$result ) { |
| 93 | + if ( 'paystack' === $context->payment_method ) { |
| 94 | + add_action( |
| 95 | + 'wc_gateway_paystack_process_payment_error', |
| 96 | + function( $failed_notice ) use ( &$result ) { |
| 97 | + $payment_details = $result->payment_details; |
| 98 | + $payment_details['errorMessage'] = wp_strip_all_tags( $failed_notice ); |
| 99 | + $result->set_payment_details( $payment_details ); |
| 100 | + } |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments