AbstractPaymentMethodType.php
2 years ago
BankTransfer.php
1 year ago
CashOnDelivery.php
1 year ago
Cheque.php
1 year ago
PayPal.php
5 months ago
PayPal.php
134 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Payments\Integrations; |
| 3 | |
| 4 | use WC_Gateway_Paypal; |
| 5 | use Automattic\WooCommerce\Blocks\Assets\Api; |
| 6 | use Automattic\WooCommerce\Gateways\PayPal\Buttons as PayPalButtons; |
| 7 | |
| 8 | /** |
| 9 | * PayPal Standard payment method integration |
| 10 | * |
| 11 | * @since 2.6.0 |
| 12 | */ |
| 13 | final class PayPal extends AbstractPaymentMethodType { |
| 14 | /** |
| 15 | * Payment method name defined by payment methods extending this class. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | protected $name = WC_Gateway_Paypal::ID; |
| 20 | |
| 21 | /** |
| 22 | * An instance of the Asset Api |
| 23 | * |
| 24 | * @var Api |
| 25 | */ |
| 26 | private $asset_api; |
| 27 | |
| 28 | /** |
| 29 | * Constructor |
| 30 | * |
| 31 | * @param Api $asset_api An instance of Api. |
| 32 | */ |
| 33 | public function __construct( Api $asset_api ) { |
| 34 | $this->asset_api = $asset_api; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Initializes the payment method type. |
| 39 | */ |
| 40 | public function initialize() { |
| 41 | $this->settings = get_option( 'woocommerce_paypal_settings', [] ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns if this payment method should be active. If false, the scripts will not be enqueued. |
| 46 | * |
| 47 | * @return boolean |
| 48 | */ |
| 49 | public function is_active() { |
| 50 | return filter_var( $this->get_setting( 'enabled', false ), FILTER_VALIDATE_BOOLEAN ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns an array of scripts/handles to be registered for this payment method. |
| 55 | * |
| 56 | * @return array |
| 57 | */ |
| 58 | public function get_payment_method_script_handles() { |
| 59 | $this->asset_api->register_script( |
| 60 | 'wc-payment-method-paypal', |
| 61 | 'assets/client/blocks/wc-payment-method-paypal.js' |
| 62 | ); |
| 63 | return [ 'wc-payment-method-paypal' ]; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns an array of key=>value pairs of data made available to the payment methods script. |
| 68 | * |
| 69 | * @return array |
| 70 | */ |
| 71 | public function get_payment_method_data() { |
| 72 | $gateway = WC_Gateway_Paypal::get_instance(); |
| 73 | |
| 74 | if ( ! $gateway->is_available() ) { |
| 75 | return []; |
| 76 | } |
| 77 | |
| 78 | $buttons = new PayPalButtons( $gateway ); |
| 79 | $options = $buttons->get_options(); |
| 80 | |
| 81 | return [ |
| 82 | 'title' => $this->get_setting( 'title' ), |
| 83 | 'description' => $this->get_description(), |
| 84 | 'supports' => $this->get_supported_features(), |
| 85 | 'isButtonsEnabled' => $buttons->is_enabled(), |
| 86 | 'isProductPage' => is_product(), |
| 87 | 'appSwitchRequestOrigin' => $buttons->get_current_page_for_app_switch(), |
| 88 | 'buttonsOptions' => $options, |
| 89 | 'wc_store_api_nonce' => wp_create_nonce( 'wc_store_api' ), |
| 90 | 'create_order_nonce' => wp_create_nonce( 'wc_gateway_paypal_standard_create_order' ), |
| 91 | 'cancel_payment_nonce' => wp_create_nonce( 'wc_gateway_paypal_standard_cancel_payment' ), |
| 92 | ]; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the description for the payment method. Add sandbox instructions if sandbox mode is enabled. |
| 97 | * |
| 98 | * @return string |
| 99 | */ |
| 100 | public function get_description() { |
| 101 | $gateway = WC_Gateway_Paypal::get_instance(); |
| 102 | $testmode = $gateway->testmode; |
| 103 | $description = $this->get_setting( 'description' ) ?? ''; |
| 104 | if ( $testmode ) { |
| 105 | /* translators: %s: Link to PayPal sandbox testing guide page */ |
| 106 | $description .= '<br>' . sprintf( __( '<strong>Sandbox mode enabled</strong>. Only sandbox test accounts can be used. See the <a href="%s">PayPal Sandbox Testing Guide</a> for more details.', 'woocommerce' ), 'https://developer.paypal.com/tools/sandbox/' ); |
| 107 | } |
| 108 | return trim( $description ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns an array of supported features. |
| 113 | * |
| 114 | * @return string[] |
| 115 | */ |
| 116 | public function get_supported_features() { |
| 117 | $gateway = WC_Gateway_Paypal::get_instance(); |
| 118 | $features = array_filter( $gateway->supports, array( $gateway, 'supports' ) ); |
| 119 | |
| 120 | /** |
| 121 | * Filter to control what features are available for each payment gateway. |
| 122 | * |
| 123 | * @since 4.4.0 |
| 124 | * |
| 125 | * @example See docs/examples/payment-gateways-features-list.md |
| 126 | * |
| 127 | * @param array $features List of supported features. |
| 128 | * @param string $name Gateway name. |
| 129 | * @return array Updated list of supported features. |
| 130 | */ |
| 131 | return apply_filters( '__experimental_woocommerce_blocks_payment_gateway_features_list', $features, $this->get_name() ); |
| 132 | } |
| 133 | } |
| 134 |