AddressRequirements.php
8 months ago
Buttons.php
5 months ago
Constants.php
3 months ago
Helper.php
3 months ago
Notices.php
5 months ago
Request.php
2 months ago
TransactAccountManager.php
5 months ago
WebhookHandler.php
3 months ago
Buttons.php
177 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Automattic\WooCommerce\Gateways\PayPal\Buttons file. |
| 4 | * |
| 5 | * @package WooCommerce\Gateways |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Automattic\WooCommerce\Gateways\PayPal; |
| 11 | |
| 12 | use Automattic\WooCommerce\Proxies\LegacyProxy; |
| 13 | use Automattic\WooCommerce\Gateways\PayPal\Request as PayPalRequest; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Handles PayPal Buttons. |
| 21 | * |
| 22 | * @since 10.5.0 |
| 23 | */ |
| 24 | class Buttons { |
| 25 | |
| 26 | /** |
| 27 | * The option for the client-id. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | private const CLIENT_ID_OPTION = 'woocommerce_paypal_client_id'; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * The gateway instance. |
| 36 | * |
| 37 | * @var \WC_Gateway_Paypal |
| 38 | */ |
| 39 | private \WC_Gateway_Paypal $gateway; |
| 40 | |
| 41 | /** |
| 42 | * Whether the gateway should use Orders v2 API. |
| 43 | * |
| 44 | * @var bool |
| 45 | */ |
| 46 | private bool $enabled; |
| 47 | |
| 48 | /** |
| 49 | * The request instance. |
| 50 | * |
| 51 | * @var PayPalRequest |
| 52 | */ |
| 53 | private PayPalRequest $request; |
| 54 | |
| 55 | /** |
| 56 | * Constructor. |
| 57 | * |
| 58 | * @param \WC_Gateway_Paypal $gateway The gateway instance. |
| 59 | */ |
| 60 | public function __construct( \WC_Gateway_Paypal $gateway ) { |
| 61 | $this->gateway = $gateway; |
| 62 | $this->request = new PayPalRequest( $this->gateway ); |
| 63 | |
| 64 | $this->enabled = $this->gateway->should_use_orders_v2() && 'yes' === $this->gateway->get_option( 'paypal_buttons', 'yes' ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the options for the PayPal buttons. |
| 69 | * |
| 70 | * @since 10.5.0 |
| 71 | * |
| 72 | * @return array |
| 73 | */ |
| 74 | public function get_options(): array { |
| 75 | $common_options = $this->get_common_options(); |
| 76 | $options = array( |
| 77 | 'partner-attribution-id' => 'Woo_Cart_CoreUpgrade', |
| 78 | 'page-type' => $this->get_page_type(), |
| 79 | ); |
| 80 | |
| 81 | return array_merge( $common_options, $options ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the common attributes for the PayPal JS SDK script and modules. |
| 86 | * |
| 87 | * @since 10.5.0 |
| 88 | * |
| 89 | * @return array |
| 90 | */ |
| 91 | public function get_common_options(): array { |
| 92 | $intent = $this->gateway->get_option( 'paymentaction' ) === 'authorization' ? 'authorize' : 'capture'; |
| 93 | |
| 94 | return array( |
| 95 | 'client-id' => $this->get_client_id(), |
| 96 | 'components' => 'buttons,funding-eligibility,messages', |
| 97 | 'disable-funding' => 'card,applepay', |
| 98 | 'enable-funding' => 'venmo,paylater', |
| 99 | 'currency' => get_woocommerce_currency(), |
| 100 | 'intent' => $intent, |
| 101 | 'merchant-id' => $this->gateway->email, |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get the client-id for the PayPal buttons. |
| 107 | * |
| 108 | * @since 10.5.0 |
| 109 | * |
| 110 | * @return string|null The PayPal client-id, or null if the request fails. |
| 111 | */ |
| 112 | public function get_client_id(): ?string { |
| 113 | if ( ! $this->gateway->should_use_orders_v2() ) { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | $option_key = self::CLIENT_ID_OPTION . ( $this->gateway->testmode ? '_sandbox' : '_live' ); |
| 118 | $client_id = get_option( $option_key, null ); |
| 119 | |
| 120 | if ( empty( $client_id ) ) { |
| 121 | $client_id = $this->request->fetch_paypal_client_id(); |
| 122 | if ( empty( $client_id ) ) { |
| 123 | return null; |
| 124 | } |
| 125 | update_option( $option_key, $client_id ); |
| 126 | } |
| 127 | |
| 128 | return $client_id; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the page type for the PayPal buttons. |
| 133 | * |
| 134 | * @since 10.5.0 |
| 135 | * |
| 136 | * @return string |
| 137 | */ |
| 138 | public function get_page_type(): string { |
| 139 | $page_type = 'checkout'; |
| 140 | if ( is_cart() || has_block( 'woocommerce/cart' ) ) { |
| 141 | $page_type = 'cart'; |
| 142 | } elseif ( is_product() ) { |
| 143 | $page_type = 'product-details'; |
| 144 | } |
| 145 | |
| 146 | return $page_type; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Whether PayPal Buttons is enabled. |
| 151 | * |
| 152 | * @since 10.5.0 |
| 153 | * |
| 154 | * @return bool |
| 155 | */ |
| 156 | public function is_enabled(): bool { |
| 157 | return $this->enabled; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get the current page URL, to be used for App Switch. |
| 162 | * Limited to checkout, cart, and product pages for security. |
| 163 | * |
| 164 | * @since 10.5.0 |
| 165 | * |
| 166 | * @return string |
| 167 | */ |
| 168 | public function get_current_page_for_app_switch(): string { |
| 169 | // If checkout, cart or product page, return the current page URL. |
| 170 | if ( wc_get_container()->get( LegacyProxy::class )->call_function( 'is_checkout' ) || is_cart() || is_product() ) { |
| 171 | return get_permalink( get_the_ID() ); |
| 172 | } |
| 173 | |
| 174 | return ''; |
| 175 | } |
| 176 | } |
| 177 |