This commit is contained in:
carmenmaymo 2023-09-08 16:58:32 +02:00
parent 9139c54cec
commit c05041eee1
No known key found for this signature in database
GPG key ID: 6023F686B0F3102E
9 changed files with 503 additions and 401 deletions

View file

@ -15,93 +15,93 @@ use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
return array(
'wcgateway.settings.fields' => function (ContainerInterface $container, array $fields): array {
$insert_after = function (array $array, string $key, array $new): array {
$keys = array_keys($array);
$index = array_search($key, $keys, true);
$pos = false === $index ? count($array) : $index + 1;
'wcgateway.settings.fields' => function ( ContainerInterface $container, array $fields ): array {
$insert_after = function ( array $array, string $key, array $new ): array {
$keys = array_keys( $array );
$index = array_search( $key, $keys, true );
$pos = false === $index ? count( $array ) : $index + 1;
return array_merge(array_slice($array, 0, $pos), $new, array_slice($array, $pos));
return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );
};
return $insert_after(
$fields,
'allow_card_button_gateway',
array(
'applepay_button_enabled' => array(
'title' => __('Apple Pay Button', 'woocommerce-paypal-payments'),
'type' => 'checkbox',
'label' => __('Enable Apple Pay button', 'woocommerce-paypal-payments'),
'default' => 'yes',
'screens' => array(State::STATE_ONBOARDED),
'gateway' => 'paypal',
'requirements' => array(),
'applepay_button_enabled' => array(
'title' => __( 'Apple Pay Button', 'woocommerce-paypal-payments' ),
'type' => 'checkbox',
'label' => __( 'Enable Apple Pay button', 'woocommerce-paypal-payments' ),
'default' => 'yes',
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'paypal',
'requirements' => array(),
'custom_attributes' => array(
'data-ppcp-handlers' => wp_json_encode(
array(
array(
'handler' => 'SubElementsHandler',
'options' => array(
'values' => array('1'),
'elements' => array('#field-applepay_button_color', '#field-applepay_button_type', '#field-applepay_button_language'),
'values' => array( '1' ),
'elements' => array( '#field-applepay_button_color', '#field-applepay_button_type', '#field-applepay_button_language' ),
),
),
)
),
),
),
'applepay_live_validation_file' => array(
'title' => __('Apple Pay Live Validation File', 'woocommerce-paypal-payments'),
'type' => 'text',
'applepay_live_validation_file' => array(
'title' => __( 'Apple Pay Live Validation File', 'woocommerce-paypal-payments' ),
'type' => 'text',
'desc_tip' => true,
'description' => __('Paste here the validation file content', 'woocommerce-paypal-payments'),
'default' => null,
'screens' => array(State::STATE_ONBOARDED),
'gateway' => 'paypal',
'description' => __( 'Paste here the validation file content', 'woocommerce-paypal-payments' ),
'default' => null,
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'paypal',
'requirements' => array(),
),
'applepay_sandbox_validation_file' => array(
'title' => __('Apple Pay Sandbox Validation File', 'woocommerce-paypal-payments'),
'type' => 'text',
'desc_tip' => true,
'description' => __('Paste here the validation file content', 'woocommerce-paypal-payments'),
'default' => null,
'screens' => array(State::STATE_ONBOARDED),
'gateway' => 'paypal',
'title' => __( 'Apple Pay Sandbox Validation File', 'woocommerce-paypal-payments' ),
'type' => 'text',
'desc_tip' => true,
'description' => __( 'Paste here the validation file content', 'woocommerce-paypal-payments' ),
'default' => null,
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'paypal',
'requirements' => array(),
),
'applepay_button_color' => array(
'title' => str_repeat(' ', 6) . __('Button Color', 'woocommerce-paypal-payments'),
'type' => 'select',
'label' => '',
'input_class' => array('wc-enhanced-select'),
'class' => array(),
'default' => 'black',
'options' => PropertiesDictionary::button_colors(),
'screens' => array(State::STATE_ONBOARDED),
'gateway' => 'paypal',
'applepay_button_color' => array(
'title' => str_repeat( ' ', 6 ) . __( 'Button Color', 'woocommerce-paypal-payments' ),
'type' => 'select',
'label' => '',
'input_class' => array( 'wc-enhanced-select' ),
'class' => array(),
'default' => 'black',
'options' => PropertiesDictionary::button_colors(),
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'paypal',
'requirements' => array(),
),
'applepay_button_type' => array(
'title' => str_repeat(' ', 6) . __('Button Type', 'woocommerce-paypal-payments'),
'type' => 'select',
'class' => array(),
'input_class' => array('wc-enhanced-select'),
'default' => 'pay',
'options' => PropertiesDictionary::button_types(),
'screens' => array(State::STATE_ONBOARDED),
'gateway' => 'paypal',
'applepay_button_type' => array(
'title' => str_repeat( ' ', 6 ) . __( 'Button Type', 'woocommerce-paypal-payments' ),
'type' => 'select',
'class' => array(),
'input_class' => array( 'wc-enhanced-select' ),
'default' => 'pay',
'options' => PropertiesDictionary::button_types(),
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'paypal',
'requirements' => array(),
),
'applepay_button_language' => array(
'title' => str_repeat(' ', 6) . __('Button Language', 'woocommerce-paypal-payments'),
'type' => 'select',
'class' => array(),
'input_class' => array('wc-enhanced-select'),
'default' => 'en',
'options' => PropertiesDictionary::button_languages(),
'screens' => array(State::STATE_ONBOARDED),
'gateway' => 'paypal',
'applepay_button_language' => array(
'title' => str_repeat( ' ', 6 ) . __( 'Button Language', 'woocommerce-paypal-payments' ),
'type' => 'select',
'class' => array(),
'input_class' => array( 'wc-enhanced-select' ),
'default' => 'en',
'options' => PropertiesDictionary::button_languages(),
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'paypal',
'requirements' => array(),
),
)

View file

@ -19,10 +19,10 @@ use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
return array(
'applepay.status-cache' => static function( ContainerInterface $container ): Cache {
'applepay.status-cache' => static function( ContainerInterface $container ): Cache {
return new Cache( 'ppcp-paypal-apple-status-cache' );
},
'applepay.apple-product-status' => static function( ContainerInterface $container ): AppleProductStatus {
'applepay.apple-product-status' => static function( ContainerInterface $container ): AppleProductStatus {
return new AppleProductStatus(
$container->get( 'wcgateway.settings' ),
$container->get( 'api.endpoint.partners' ),
@ -30,15 +30,15 @@ return array(
$container->get( 'onboarding.state' )
);
},
'applepay.enabled' => static function ( ContainerInterface $container ): bool {
'applepay.enabled' => static function ( ContainerInterface $container ): bool {
$status = $container->get( 'applepay.apple-product-status' );
assert( $status instanceof AppleProductStatus);
assert( $status instanceof AppleProductStatus );
return $status->apple_is_active();
},
'applepay.server_supported' => static function ( ContainerInterface $container ): bool {
'applepay.server_supported' => static function ( ContainerInterface $container ): bool {
return ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off';
},
'applepay.url' => static function ( ContainerInterface $container ): string {
'applepay.url' => static function ( ContainerInterface $container ): string {
$path = realpath( __FILE__ );
if ( false === $path ) {
return '';
@ -48,22 +48,22 @@ return array(
dirname( $path, 3 ) . '/woocommerce-paypal-payments.php'
);
},
'applepay.sdk_script_url' => static function ( ContainerInterface $container ): string {
'applepay.sdk_script_url' => static function ( ContainerInterface $container ): string {
return 'https://applepay.cdn-apple.com/jsapi/v1/apple-pay-sdk.js';
},
'applepay.data_to_scripts' => static function ( ContainerInterface $container ): DataToAppleButtonScripts {
return new DataToAppleButtonScripts($container->get( 'applepay.sdk_script_url' ), $container->get( 'wcgateway.settings' ));
'applepay.data_to_scripts' => static function ( ContainerInterface $container ): DataToAppleButtonScripts {
return new DataToAppleButtonScripts( $container->get( 'applepay.sdk_script_url' ), $container->get( 'wcgateway.settings' ) );
},
'applepay.button' => static function ( ContainerInterface $container ): ApplePayButton {
'applepay.button' => static function ( ContainerInterface $container ): ApplePayButton {
return new ApplePayButton(
$container->get( 'wcgateway.settings' ),
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'wcgateway.order-processor' ),
$container->get( 'applepay.url' ),
$container->get('ppcp.asset-version'),
$container->get('applepay.data_to_scripts'),
$container->get( 'wcgateway.settings.status' ),
$container->get( 'ppcp.asset-version' ),
$container->get( 'applepay.data_to_scripts' ),
$container->get( 'wcgateway.settings.status' )
);
},
'applepay.blocks-payment-method' => static function ( ContainerInterface $container ): PaymentMethodTypeInterface {

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Applepay;
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\Applepay\Assets\ApplePayButton;
use WooCommerce\PayPalCommerce\Applepay\Assets\AppleProductStatus;
use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
@ -18,6 +19,7 @@ use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
* Class ApplepayModule
@ -63,7 +65,7 @@ class ApplepayModule implements ModuleInterface {
return;
}
$settings = $c->get( 'wcgateway.settings' );
$settings = $c->get( 'wcgateway.settings' );
$merchant_validated = $settings->has( 'applepay_validated' ) ? $settings->get( 'applepay_validated' ) === true : false;
if ( ! $merchant_validated ) {
add_action(
@ -84,9 +86,9 @@ class ApplepayModule implements ModuleInterface {
);
}
$this->load_assets( $c );
$this->handle_validation_file($c);
$this->handle_validation_file( $c );
$this->render_buttons( $c );
assert( $apple_payment_method instanceof ButtonInterface );
assert( $apple_payment_method instanceof ApplepayButton );
$apple_payment_method->bootstrap_ajax_request();
add_action(
'woocommerce_blocks_payment_method_type_registration',
@ -95,7 +97,7 @@ class ApplepayModule implements ModuleInterface {
}
);
$this->remove_status_cache($c);
$this->remove_status_cache( $c );
}
/**
@ -109,16 +111,17 @@ class ApplepayModule implements ModuleInterface {
/**
* Loads the validation string.
*
* @param boolean $is_sandbox The environment for this merchant.
* @param boolean $is_sandbox The environment for this merchant.
* @param Settings $settings The settings.
*/
protected function load_domain_association_file(bool $is_sandbox, $settings ): void {
protected function load_domain_association_file( bool $is_sandbox, Settings $settings ): void {
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
return;
}
$request_uri = (string) filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL );
if ( strpos( $request_uri, '.well-known/apple-developer-merchantid-domain-association' ) !== false ) {
$validation_string = $is_sandbox ? 'applepay_sandbox_validation_file' : 'applepay_live_validation_file';
$validation_string = $settings->has( $validation_string ) ? $settings->get( $validation_string ) : '';
$validation_string = $settings->has( $validation_string ) ? $settings->get( $validation_string ) : '';
$validation_string = preg_replace( '/\s+/', '', $validation_string );
$validation_string = $validation_string ? preg_replace( '/[^a-zA-Z0-9]/', '', $validation_string ) : '';
nocache_headers();
@ -139,8 +142,7 @@ class ApplepayModule implements ModuleInterface {
'wp_enqueue_scripts',
function () use ( $c ) {
$button = $c->get( 'applepay.button' );
assert( $button instanceof ButtonInterface );
assert( $button instanceof ApplePayButton );
if ( $button->should_load_script() ) {
$button->enqueue();
}
@ -174,51 +176,54 @@ class ApplepayModule implements ModuleInterface {
}
/**
* @param ContainerInterface $c
* Removes the status cache.
*
* @param ContainerInterface $c The container.
*
* @return void
*/
public function remove_status_cache(ContainerInterface $c): void
{
public function remove_status_cache( ContainerInterface $c ): void {
add_action(
'woocommerce_paypal_payments_gateway_migrate_on_update',
static function () use ($c) {
$apple_status_cache = $c->get('applepay.status-cache');
assert($apple_status_cache instanceof Cache);
static function () use ( $c ) {
$apple_status_cache = $c->get( 'applepay.status-cache' );
assert( $apple_status_cache instanceof Cache );
$apple_status_cache->delete(AppleProductStatus::APPLE_STATUS_CACHE_KEY);
$apple_status_cache->delete( AppleProductStatus::APPLE_STATUS_CACHE_KEY );
$settings = $c->get('wcgateway.settings');
$settings->set('products_apple_enabled', false);
$settings = $c->get( 'wcgateway.settings' );
$settings->set( 'products_apple_enabled', false );
$settings->persist();
// Update caches.
$apple_status = $c->get('applepay.apple-product-status');
assert($apple_status instanceof AppleProductStatus);
$apple_status = $c->get( 'applepay.apple-product-status' );
assert( $apple_status instanceof AppleProductStatus );
$apple_status->apple_is_active();
}
);
add_action(
'woocommerce_paypal_payments_on_listening_request',
static function () use ($c) {
$apple_status = $c->get('applepay.status-cache');
if ($apple_status->has(AppleProductStatus::APPLE_STATUS_CACHE_KEY)) {
$apple_status->delete(AppleProductStatus::APPLE_STATUS_CACHE_KEY);
static function () use ( $c ) {
$apple_status = $c->get( 'applepay.status-cache' );
if ( $apple_status->has( AppleProductStatus::APPLE_STATUS_CACHE_KEY ) ) {
$apple_status->delete( AppleProductStatus::APPLE_STATUS_CACHE_KEY );
}
}
);
}
/**
* @param ContainerInterface $c
* Handles the validation file.
*
* @param ContainerInterface $c The container.
* @return void
*/
public function handle_validation_file(ContainerInterface $c): void
{
$env = $c->get('onboarding.environment');
assert($env instanceof Environment);
$is_sandobx = $env->current_environment_is(Environment::SANDBOX);
$settings = $c->get('wcgateway.settings');
$this->load_domain_association_file($is_sandobx, $settings);
public function handle_validation_file( ContainerInterface $c ): void {
$env = $c->get( 'onboarding.environment' );
assert( $env instanceof Environment );
$is_sandobx = $env->current_environment_is( Environment::SANDBOX );
$settings = $c->get( 'wcgateway.settings' );
$this->load_domain_association_file( $is_sandobx, $settings );
}
}

View file

@ -45,12 +45,16 @@ class ApplePayButton implements ButtonInterface {
*/
private $logger;
/**
* The response templates.
*
* @var ResponsesToApple
*/
private $response_templates;
/**
* @var array The old cart contents.
* The old cart contents.
*
* @var array
*/
private $old_cart_contents;
@ -72,16 +76,29 @@ class ApplePayButton implements ButtonInterface {
* @var OrderProcessor
*/
protected $order_processor;
/**
* @var bool Whether to reload the cart after the order is processed.
* Whether to reload the cart after the order is processed.
*
* @var bool
*/
protected $reload_cart = false;
/**
* The module version.
*
* @var string
*/
private $version;
/**
* The module URL.
*
* @var string
*/
private $module_url;
/**
* The data to send to the ApplePay button script.
*
* @var DataToAppleButtonScripts
*/
private $script_data;
@ -95,29 +112,33 @@ class ApplePayButton implements ButtonInterface {
/**
* PayPalPaymentMethod constructor.
*
* @param Settings $settings The settings.
* @param LoggerInterface $logger The logger.
* @param Settings $settings The settings.
* @param LoggerInterface $logger The logger.
* @param OrderProcessor $order_processor The Order processor.
* @param string $module_url The module URL.
* @param string $version The module version.
* @param DataToAppleButtonScripts $data The data to send to the ApplePay button script.
* @param SettingsStatus $settings_status The settings status helper.
*/
public function __construct(
Settings $settings,
LoggerInterface $logger,
OrderProcessor $order_processor,
string $module_url,
string $version,
Settings $settings,
LoggerInterface $logger,
OrderProcessor $order_processor,
string $module_url,
string $version,
DataToAppleButtonScripts $data,
SettingsStatus $settings_status
) {
$this->settings = $settings;
$this->settings = $settings;
$this->response_templates = new ResponsesToApple();
$this->logger = $logger;
$this->id = 'applepay';
$this->method_title = __( 'Apple Pay', 'woocommerce-paypal-payments' );
$this->order_processor = $order_processor;
$this->module_url = $module_url;
$this->version = $version;
$this->script_data = $data;
$this->settings_status = $settings_status;
$this->id = 'applepay';
$this->method_title = __( 'Apple Pay', 'woocommerce-paypal-payments' );
$this->order_processor = $order_processor;
$this->module_url = $module_url;
$this->version = $version;
$this->script_data = $data;
$this->settings_status = $settings_status;
}
/**
@ -241,15 +262,14 @@ class ApplePayButton implements ButtonInterface {
* On fail triggers and option that shows an admin notice showing the error
* On success removes such flag
*/
public function validate()
{
public function validate() {
$applepay_request_data_object = $this->applepay_data_object_http();
if (!$this->is_nonce_valid()) {
if ( ! $this->is_nonce_valid() ) {
return;
}
$applepay_request_data_object->validation_data();
$settings = $this->settings;
$settings->set('applepay_validated', $applepay_request_data_object->validated_flag());
$settings->set( 'applepay_validated', $applepay_request_data_object->validated_flag() );
$settings->persist();
wp_send_json_success();
}
@ -258,7 +278,6 @@ class ApplePayButton implements ButtonInterface {
* It updates the amount paying information if needed
* On error returns an array of errors to be handled by the script
* On success returns the new contact data
* @throws \Exception
*/
public function update_shipping_contact(): void {
$applepay_request_data_object = $this->applepay_data_object_http();
@ -312,7 +331,6 @@ class ApplePayButton implements ButtonInterface {
* It updates the amount paying information if needed
* On error returns an array of errors to be handled by the script
* On success returns the new contact data
* @throws \Exception
*/
public function update_shipping_method(): void {
$applepay_request_data_object = $this->applepay_data_object_http();
@ -324,7 +342,7 @@ class ApplePayButton implements ButtonInterface {
$this->response_templates->response_with_data_errors( $applepay_request_data_object->errors() );
}
$payment_details = $this->which_calculate_totals( $applepay_request_data_object );
$response = $this->response_templates->apple_formatted_response( $payment_details );
$response = $this->response_templates->apple_formatted_response( $payment_details );
$this->response_templates->response_success( $response );
}
@ -332,22 +350,23 @@ class ApplePayButton implements ButtonInterface {
* Method to create a WC order from the data received from the ApplePay JS
* On error returns an array of errors to be handled by the script
* On success returns the new order data
* @throws \Exception
*
* @throws \Exception When validation fails.
*/
public function create_wc_order() {
//$this->response_after_successful_result();
// $this->response_after_successful_result();
$applepay_request_data_object = $this->applepay_data_object_http();
$applepay_request_data_object->order_data('productDetail');
$this->update_posted_data($applepay_request_data_object);
$cart_item_key = $this->prepare_cart($applepay_request_data_object);
$cart = WC()->cart;
$address = $applepay_request_data_object->shipping_address();
$applepay_request_data_object->order_data( 'productDetail' );
$this->update_posted_data( $applepay_request_data_object );
$cart_item_key = $this->prepare_cart( $applepay_request_data_object );
$cart = WC()->cart;
$address = $applepay_request_data_object->shipping_address();
$this->calculate_totals_single_product(
$cart,
$address,
$applepay_request_data_object->shipping_method()
);
if (! $cart_item_key) {
if ( ! $cart_item_key ) {
$this->response_templates->response_with_data_errors(
array(
array(
@ -358,13 +377,16 @@ class ApplePayButton implements ButtonInterface {
);
return;
}
$this->add_addresses_to_order($applepay_request_data_object);
//add_action('woocommerce_checkout_order_processed', array($this, 'process_order_as_paid'), 10, 3);
add_filter('woocommerce_payment_successful_result', function (array $result) use ($cart, $cart_item_key) : array {
$this->clear_current_cart($cart, $cart_item_key);
$this->reload_cart( $cart );
return $result;
});
$this->add_addresses_to_order( $applepay_request_data_object );
// add_action('woocommerce_checkout_order_processed', array($this, 'process_order_as_paid'), 10, 3).
add_filter(
'woocommerce_payment_successful_result',
function ( array $result ) use ( $cart, $cart_item_key ) : array {
$this->clear_current_cart( $cart, $cart_item_key );
$this->reload_cart( $cart );
return $result;
}
);
WC()->checkout()->process_checkout();
}
@ -413,18 +435,18 @@ class ApplePayButton implements ButtonInterface {
/**
* Selector between product detail and cart page calculations
*
* @param $applepay_request_data_object
* @param ApplePayDataObjectHttp $applepay_request_data_object The data object.
*
* @return array|bool
*/
protected function which_calculate_totals(
$applepay_request_data_object
) {
$address = empty($applepay_request_data_object->shipping_address()) ? $applepay_request_data_object->simplified_contact() : $applepay_request_data_object->shipping_address();
if ( $applepay_request_data_object->caller_page === 'productDetail' ) {
$cart_item_key = $this->prepare_cart($applepay_request_data_object);
$cart = WC()->cart;
if (! assert($cart instanceof WC_Cart)) {
$address = empty( $applepay_request_data_object->shipping_address() ) ? $applepay_request_data_object->simplified_contact() : $applepay_request_data_object->shipping_address();
if ( $applepay_request_data_object->caller_page() === 'productDetail' ) {
$cart_item_key = $this->prepare_cart( $applepay_request_data_object );
$cart = WC()->cart;
if ( ! assert( $cart instanceof WC_Cart ) ) {
return false;
}
$totals = $this->calculate_totals_single_product(
@ -432,11 +454,11 @@ class ApplePayButton implements ButtonInterface {
$address,
$applepay_request_data_object->shipping_method()
);
$this->clear_current_cart($cart, $cart_item_key);
$this->clear_current_cart( $cart, $cart_item_key );
$this->reload_cart( $cart );
return $totals;
}
if ( $applepay_request_data_object->caller_page === 'cart' ) {
if ( $applepay_request_data_object->caller_page() === 'cart' ) {
return $this->calculate_totals_cart_page(
$address,
$applepay_request_data_object->shipping_method()
@ -451,19 +473,18 @@ class ApplePayButton implements ButtonInterface {
* If no shippingMethodId provided will return the first available shipping
* method
*
* @param $product_id
* @param $product_quantity
* @param $customer_address
* @param null $shipping_method
* @param WC_Cart $cart The cart.
* @param array $customer_address customer address to use.
* @param array|null $shipping_method shipping method to use.
*/
protected function calculate_totals_single_product(
$cart,
$customer_address,
$shipping_method = null
): array {
$results = array();
$results = array();
try {
// I just care about apple address details
// I just care about apple address details.
$shipping_method_id = '';
$shipping_methods_array = array();
$selected_shipping_method = array();
@ -476,6 +497,9 @@ class ApplePayButton implements ButtonInterface {
);
}
if ( $cart->needs_shipping() ) {
if ( ! $shipping_method ) {
return array();
}
list(
$shipping_methods_array, $selected_shipping_method
) = $this->cart_shipping_methods(
@ -495,6 +519,7 @@ class ApplePayButton implements ButtonInterface {
$shipping_methods_array
);
} catch ( Exception $exception ) {
return array();
}
return $results;
}
@ -503,6 +528,8 @@ class ApplePayButton implements ButtonInterface {
* Sets the customer address with ApplePay details to perform correct
* calculations
* If no parameter passed then it resets the customer to shop details
*
* @param array $address customer address.
*/
protected function customer_address( array $address = array() ) {
$base_location = wc_get_base_location();
@ -524,10 +551,10 @@ class ApplePayButton implements ButtonInterface {
/**
* Add shipping methods to cart to perform correct calculations
*
* @param $cart
* @param $customer_address
* @param $shipping_method
* @param $shipping_method_id
* @param WC_Cart $cart WC Cart instance.
* @param array $customer_address Customer address.
* @param array $shipping_method Shipping method.
* @param array $shipping_method_id Shipping method id.
*/
protected function cart_shipping_methods(
$cart,
@ -573,13 +600,13 @@ class ApplePayButton implements ButtonInterface {
/**
* Sets shipping packages for correct calculations
*
* @param $customer_address
* @param $total
* @param array $customer_address ApplePay address details.
* @param float $total Total amount of the cart.
*
* @return mixed|void|null
*/
protected function getShippingPackages( $customer_address, $total ) {
// Packages array for storing 'carts'
// Packages array for storing 'carts'.
$packages = array();
$packages[0]['contents'] = WC()->cart->cart_contents;
$packages[0]['contents_cost'] = $total;
@ -597,9 +624,9 @@ class ApplePayButton implements ButtonInterface {
/**
* Returns the formatted results of the cart calculations
*
* @param $cart
* @param $selected_shipping_method
* @param $shipping_methods_array
* @param WC_Cart $cart WC Cart object.
* @param array $selected_shipping_method Selected shipping method.
* @param array $shipping_methods_array Shipping methods array.
*/
protected function cart_calculation_results(
$cart,
@ -629,12 +656,12 @@ class ApplePayButton implements ButtonInterface {
* If no shippingMethodId provided will return the first available shipping
* method
*
* @param $customer_address
* @param null $shipping_method
* @param array $customer_address The customer address.
* @param array|null $shipping_method The shipping method.
*/
protected function calculate_totals_cart_page(
$customer_address = null,
$shipping_method = null
array $customer_address,
$shipping_method = null
): array {
$results = array();
@ -644,7 +671,7 @@ class ApplePayButton implements ButtonInterface {
try {
$shipping_methods_array = array();
$selected_shipping_method = array();
// I just care about apple address details
// I just care about apple address details.
$this->customer_address( $customer_address );
$cart = WC()->cart;
if ( $shipping_method ) {
@ -655,6 +682,9 @@ class ApplePayButton implements ButtonInterface {
}
if ( $cart->needs_shipping() ) {
if ( ! $shipping_method ) {
return [];
}
list(
$shipping_methods_array, $selected_shipping_method
) = $this->cart_shipping_methods(
@ -676,6 +706,7 @@ class ApplePayButton implements ButtonInterface {
$this->customer_address();
} catch ( Exception $e ) {
return array();
}
return $results;
@ -684,19 +715,18 @@ class ApplePayButton implements ButtonInterface {
/**
* Add address billing and shipping data to order
*
* @param ApplePayDataObjectHttp $applepay_request_data_object
* @param $order
* @param ApplePayDataObjectHttp $applepay_request_data_object ApplePayDataObjectHttp.
*/
protected function add_addresses_to_order(
ApplePayDataObjectHttp $applepay_request_data_object
) {
): void {
add_action(
'woocommerce_checkout_create_order',
static function ( $order, $data ) use ( $applepay_request_data_object ) {
if ( $applepay_request_data_object->shipping_method() !== null ) {
static function (WC_Order $order, array $data ) use ( $applepay_request_data_object ) {
if ( !empty($applepay_request_data_object->shipping_method()) ) {
$billing_address = $applepay_request_data_object->billing_address();
$shipping_address = $applepay_request_data_object->shipping_address();
// apple puts email in shipping_address while we get it from WC's billing_address
// apple puts email in shipping_address while we get it from WC's billing_address.
$billing_address['email'] = $shipping_address['email'];
$billing_address['phone'] = $shipping_address['phone'];
@ -712,9 +742,9 @@ class ApplePayButton implements ButtonInterface {
* Empty the cart to use for calculations
* while saving its contents in a field
*/
protected function save_old_cart() {
protected function save_old_cart(): void {
$cart = WC()->cart;
if ( $cart->is_empty() || ! assert($cart instanceof WC_Cart)) {
if ( $cart->is_empty() ) {
return;
}
$this->old_cart_contents = $cart->get_cart_contents();
@ -725,7 +755,9 @@ class ApplePayButton implements ButtonInterface {
}
/**
* @param WC_Cart $cart
* Reloads the previous cart contents
*
* @param WC_Cart $cart The cart to reload.
*/
protected function reload_cart( WC_Cart $cart ): void {
if ( ! $this->reload_cart ) {
@ -736,106 +768,86 @@ class ApplePayButton implements ButtonInterface {
}
}
protected function response_after_successful_result(): void {
add_filter(
'woocommerce_payment_successful_result',
function ( $result, $order_id ) {
if (
isset( $result['result'] )
&& 'success' === $result['result']
) {
$this->response_templates->response_success(
$this->response_templates->authorization_result_response(
'STATUS_SUCCESS',
$order_id
)
);
} else {
wp_send_json_error(
$this->response_templates->authorization_result_response(
'STATUS_FAILURE',
0,
array( array( 'errorCode' => 'unknown' ) )
)
);
}
return $result;
},
10,
2
);
}
/**
* @param WC_Cart|null $cart
* @param $cart_item_key
* Clear the current cart
*
* @param WC_Cart|null $cart The cart object.
* @param string $cart_item_key The cart item key.
* @return void
*/
public function clear_current_cart(?WC_Cart $cart, $cart_item_key): void
{
$cart->remove_cart_item($cart_item_key);
public function clear_current_cart(?WC_Cart $cart, string $cart_item_key ): void {
if ( ! $cart ) {
return;
}
$cart->remove_cart_item( $cart_item_key );
$this->customer_address();
}
/**
* Removes the old cart, saves it, and creates a new one
* @param ApplePayDataObjectHttp $applepay_request_data_object
* @return bool | string The cart item key after adding to the new cart
* @throws \Exception
*
* @param ApplePayDataObjectHttp $applepay_request_data_object The request data object.
* @return bool | string The cart item key after adding to the new cart.
* @throws \Exception If cannot be added to cart.
*/
public function prepare_cart(ApplePayDataObjectHttp $applepay_request_data_object): string
{
public function prepare_cart( ApplePayDataObjectHttp $applepay_request_data_object ): string {
$this->save_old_cart();
$cart = WC()->cart;
return $cart->add_to_cart(
(int) $applepay_request_data_object->product_id(),
(int) $applepay_request_data_object->product_quantity());
(int) $applepay_request_data_object->product_quantity()
);
}
public function process_order_as_paid($order_id): void
{
$order = wc_get_order($order_id);
if (!assert($order instanceof WC_Order)) {
/*
TODO
public function process_order_as_paid( $order_id ): void {
$order = wc_get_order( $order_id );
if ( ! assert( $order instanceof WC_Order ) ) {
return;
}
$order->payment_complete();
wc_reduce_stock_levels($order_id);
wc_reduce_stock_levels( $order_id );
$order->save();
}
*/
protected function update_posted_data( $applepay_request_data_object )
{
/**
* Update the posted data to match the Apple Pay request data
*
* @param ApplePayDataObjectHttp $applepay_request_data_object The Apple Pay request data.
*/
protected function update_posted_data( $applepay_request_data_object ): void {
add_filter(
'woocommerce_checkout_posted_data',
function ($data) use ($applepay_request_data_object) {
function (array $data ) use ( $applepay_request_data_object ): array {
$data['payment_method'] = "ppcp-gateway";
$data['shipping_method'] = $applepay_request_data_object->shipping_method();
$data['payment_method'] = 'ppcp-gateway';
$data['shipping_method'] = $applepay_request_data_object->shipping_method();
$data['billing_first_name'] = $applepay_request_data_object->billing_address()['first_name'] ?? '';
$data['billing_last_name'] = $applepay_request_data_object->billing_address()['last_name'] ?? '';
$data['billing_company'] = $applepay_request_data_object->billing_address()['company'] ?? '';
$data['billing_country'] = $applepay_request_data_object->billing_address()['country'] ?? '';
$data['billing_address_1'] = $applepay_request_data_object->billing_address()['address_1'] ?? '';
$data['billing_address_2'] = $applepay_request_data_object->billing_address()['address_2'] ?? '';
$data['billing_city'] = $applepay_request_data_object->billing_address()['city'] ?? '';
$data['billing_state'] = $applepay_request_data_object->billing_address()['state'] ?? '';
$data['billing_postcode'] = $applepay_request_data_object->billing_address()['postcode'] ?? '';
$data['billing_last_name'] = $applepay_request_data_object->billing_address()['last_name'] ?? '';
$data['billing_company'] = $applepay_request_data_object->billing_address()['company'] ?? '';
$data['billing_country'] = $applepay_request_data_object->billing_address()['country'] ?? '';
$data['billing_address_1'] = $applepay_request_data_object->billing_address()['address_1'] ?? '';
$data['billing_address_2'] = $applepay_request_data_object->billing_address()['address_2'] ?? '';
$data['billing_city'] = $applepay_request_data_object->billing_address()['city'] ?? '';
$data['billing_state'] = $applepay_request_data_object->billing_address()['state'] ?? '';
$data['billing_postcode'] = $applepay_request_data_object->billing_address()['postcode'] ?? '';
if ( $applepay_request_data_object->shipping_method() !== null ) {
$data['billing_email'] = $applepay_request_data_object->shipping_address()['email'] ?? '';
$data['billing_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
if ( !empty($applepay_request_data_object->shipping_method()) ) {
$data['billing_email'] = $applepay_request_data_object->shipping_address()['email'] ?? '';
$data['billing_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
$data['shipping_first_name'] = $applepay_request_data_object->shipping_address()['first_name'] ?? '';
$data['shipping_last_name'] = $applepay_request_data_object->shipping_address()['last_name'] ?? '';
$data['shipping_company'] = $applepay_request_data_object->shipping_address()['company'] ?? '';
$data['shipping_country'] = $applepay_request_data_object->shipping_address()['country'] ?? '';
$data['shipping_address_1'] = $applepay_request_data_object->shipping_address()['address_1'] ?? '';
$data['shipping_address_2'] = $applepay_request_data_object->shipping_address()['address_2'] ?? '';
$data['shipping_city'] = $applepay_request_data_object->shipping_address()['city'] ?? '';
$data['shipping_state'] = $applepay_request_data_object->shipping_address()['state'] ?? '';
$data['shipping_postcode'] = $applepay_request_data_object->shipping_address()['postcode'] ?? '';
$data['shipping_email'] = $applepay_request_data_object->shipping_address()['email'] ?? '';
$data['shipping_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
$data['shipping_last_name'] = $applepay_request_data_object->shipping_address()['last_name'] ?? '';
$data['shipping_company'] = $applepay_request_data_object->shipping_address()['company'] ?? '';
$data['shipping_country'] = $applepay_request_data_object->shipping_address()['country'] ?? '';
$data['shipping_address_1'] = $applepay_request_data_object->shipping_address()['address_1'] ?? '';
$data['shipping_address_2'] = $applepay_request_data_object->shipping_address()['address_2'] ?? '';
$data['shipping_city'] = $applepay_request_data_object->shipping_address()['city'] ?? '';
$data['shipping_state'] = $applepay_request_data_object->shipping_address()['state'] ?? '';
$data['shipping_postcode'] = $applepay_request_data_object->shipping_address()['postcode'] ?? '';
$data['shipping_email'] = $applepay_request_data_object->shipping_address()['email'] ?? '';
$data['shipping_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
}
return $data;
@ -843,8 +855,12 @@ class ApplePayButton implements ButtonInterface {
);
}
public function render(): bool
{
/**
* Renders the Apple Pay button on the page
*
* @return bool
*/
public function render(): bool {
$is_applepay_button_enabled = $this->settings->has( 'applepay_button_enabled' ) ? $this->settings->get( 'applepay_button_enabled' ) : false;
$button_enabled_product = $is_applepay_button_enabled && $this->settings_status->is_smart_button_enabled_for_location( 'product' );
@ -855,7 +871,7 @@ class ApplePayButton implements ButtonInterface {
add_filter(
'woocommerce_paypal_payments_sdk_components_hook',
function( $components ) {
function( array $components ) {
$components[] = 'applepay';
return $components;
}
@ -908,7 +924,9 @@ class ApplePayButton implements ButtonInterface {
);
}
/*if ( $button_enabled_minicart ) {
/*
TODO
if ( $button_enabled_minicart ) {
$default_hook_name = 'woocommerce_paypal_payments_minicart_button_render';
$render_placeholder = apply_filters( 'woocommerce_paypal_payments_googlepay_minicart_button_render_hook', $default_hook_name );
$render_placeholder = is_string( $render_placeholder ) ? $render_placeholder : $default_hook_name;
@ -919,7 +937,9 @@ class ApplePayButton implements ButtonInterface {
},
21
);
}*/
}
*/
return true;
}
/**
@ -935,13 +955,21 @@ class ApplePayButton implements ButtonInterface {
<?php
}
public function should_load_script(): bool
{
/**
* Checks if the module should load the script.
*
* @return bool
*/
public function should_load_script(): bool {
return true;
}
public function enqueue(): void
{
/**
* Enqueues the scripts.
*
* @return void
*/
public function enqueue(): void {
wp_register_script(
'wc-ppcp-applepay',
untrailingslashit( $this->module_url ) . '/assets/js/boot.js',
@ -966,13 +994,22 @@ class ApplePayButton implements ButtonInterface {
);
}
public function script_data(): array
{
/**
* Returns the script data.
*
* @return array
*/
public function script_data(): array {
return $this->script_data->apple_pay_script_data();
}
public function is_enabled(): bool
{
/**
* Returns true if the module is enabled.
*
* @return bool
*/
public function is_enabled(): bool {
// TODO: Implement is_enabled() method.
return true;
}
}

View file

@ -20,6 +20,7 @@ class ApplePayDataObjectHttp {
* The nonce of the request.
*
* @var string
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $nonce;
@ -44,6 +45,13 @@ class ApplePayDataObjectHttp {
*/
protected $product_id;
/**
* The caller page.
*
* @var mixed
*/
protected $caller_page;
/**
* The product quantity.
*
@ -104,7 +112,7 @@ class ApplePayDataObjectHttp {
/**
* Resets the errors array
*/
protected function reset_errors() {
protected function reset_errors(): void {
$this->errors = array();
}
@ -113,7 +121,8 @@ class ApplePayDataObjectHttp {
*
* @return bool
*/
public function has_errors() {
public function has_errors(): bool
{
return ! empty( $this->errors );
}
/**
@ -125,7 +134,12 @@ class ApplePayDataObjectHttp {
return $this->errors;
}
public function validation_data() {
/**
* Assigns the validation flag
*
* @return void
*/
public function validation_data(): void {
$data = filter_input( INPUT_POST, 'validation', FILTER_VALIDATE_BOOL );
if ( ! $data ) {
return;
@ -137,8 +151,11 @@ class ApplePayDataObjectHttp {
* Set the object with the data relevant to ApplePay on update shipping contact
* Required data depends on callerPage
*/
public function update_contact_data() {
$nonce = filter_input( INPUT_POST, 'woocommerce-process-checkout-nonce', FILTER_SANITIZE_SPECIAL_CHARS );
public function update_contact_data(): void {
$nonce = filter_input( INPUT_POST, 'woocommerce-process-checkout-nonce', FILTER_SANITIZE_SPECIAL_CHARS );
if ( ! $nonce ) {
return;
}
$is_nonce_valid = wp_verify_nonce(
$nonce,
'woocommerce-process_checkout'
@ -147,7 +164,9 @@ class ApplePayDataObjectHttp {
return;
}
$data = $this->get_filtered_request_data();
if ( ! $data ) {
return;
}
$result = $this->update_required_data(
$data,
PropertiesDictionary::UPDATE_CONTACT_SINGLE_PROD_REQUIRED_FIELDS,
@ -163,8 +182,11 @@ class ApplePayDataObjectHttp {
* Set the object with the data relevant to ApplePay on update shipping method
* Required data depends on callerPage
*/
public function update_method_data() {
public function update_method_data(): void {
$nonce = filter_input( INPUT_POST, 'woocommerce-process-checkout-nonce', FILTER_SANITIZE_SPECIAL_CHARS );
if ( ! $nonce ) {
return;
}
$is_nonce_valid = wp_verify_nonce(
$nonce,
'woocommerce-process_checkout'
@ -174,6 +196,9 @@ class ApplePayDataObjectHttp {
}
$data = $this->get_filtered_request_data();
if ( ! $data ) {
return;
}
$result = $this->update_required_data(
$data,
PropertiesDictionary::UPDATE_METHOD_SINGLE_PROD_REQUIRED_FIELDS,
@ -192,8 +217,11 @@ class ApplePayDataObjectHttp {
*
* @param string $caller_page The caller page.
*/
public function order_data( string $caller_page ) {
public function order_data( string $caller_page ): void {
$nonce = filter_input( INPUT_POST, 'woocommerce-process-checkout-nonce', FILTER_SANITIZE_SPECIAL_CHARS );
if ( ! $nonce ) {
return;
}
$is_nonce_valid = wp_verify_nonce(
$nonce,
'woocommerce-process_checkout'
@ -202,6 +230,9 @@ class ApplePayDataObjectHttp {
return;
}
$data = filter_var_array( $_POST, FILTER_SANITIZE_SPECIAL_CHARS );
if ( ! $data ) {
return;
}
$data[ PropertiesDictionary::CALLER_PAGE ] = $caller_page;
$result = $this->update_required_data(
$data,
@ -273,7 +304,7 @@ class ApplePayDataObjectHttp {
*
* @param array $data The data.
*/
protected function assign_data_object_values( array $data ) {
protected function assign_data_object_values( array $data ): void {
foreach ( $data as $key => $value ) {
if ( $key === 'woocommerce-process-checkout-nonce' ) {
$key = 'nonce';
@ -285,7 +316,7 @@ class ApplePayDataObjectHttp {
/**
* Returns the address details used in pre-authorization steps.
*
* @param array $contact_info
* @param array $contact_info The contact info.
*
* @return string[]
*/
@ -316,9 +347,9 @@ class ApplePayDataObjectHttp {
* are not empty.
* If not it adds a contacField error to the object's error list.
*
* @param array $post The address to check
* @param array $required The required fields for the given address
* @param string $error_code Either shipping or billing kind
* @param array $post The address to check.
* @param array $required The required fields for the given address.
* @param string $error_code Either shipping or billing kind.
*
* @return bool
*/
@ -355,8 +386,8 @@ class ApplePayDataObjectHttp {
/**
* Returns the address details for after authorization steps.
*
* @param array $data The data.
* @param string $error_code differentiates between billing and shipping information
* @param array $data The data.
* @param string $error_code differentiates between billing and shipping information.
*
* @return string[]
*/
@ -397,8 +428,8 @@ class ApplePayDataObjectHttp {
* Updates the object with the required data.
*
* @param array $data The data.
* @param array $required_product_fields
* @param array $required_cart_fields
* @param array $required_product_fields The required product fields.
* @param array $required_cart_fields The required cart fields.
* @return bool
*/
protected function update_required_data( array $data, array $required_product_fields, array $required_cart_fields ) {
@ -499,6 +530,15 @@ class ApplePayDataObjectHttp {
return $this->product_id;
}
/**
* Returns the product id.
*
* @return string
*/
public function caller_page(): string {
return $this->caller_page;
}
/**
* Returns the product quantity.
*
@ -511,9 +551,10 @@ class ApplePayDataObjectHttp {
/**
* Returns the nonce.
*
* @return mixed
* @return string
*/
public function nonce() {
public function nonce(): string
{
return $this->nonce;
}
@ -531,8 +572,7 @@ class ApplePayDataObjectHttp {
*
* @return bool
*/
public function validated_flag()
{
public function validated_flag() {
return $this->validation_flag;
}

View file

@ -9,6 +9,9 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay\Assets;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
* Class DataToAppleButtonScripts
*/
@ -23,12 +26,18 @@ class DataToAppleButtonScripts {
/**
* The settings.
*
* @var array
* @var Settings
*/
private $settings;
public function __construct($sdk_url, $settings) {
$this->sdk_url = $sdk_url;
/**
* DataToAppleButtonScripts constructor.
*
* @param string $sdk_url The URL to the SDK.
* @param Settings $settings The settings.
*/
public function __construct(string $sdk_url, Settings $settings ) {
$this->sdk_url = $sdk_url;
$this->settings = $settings;
}
@ -36,14 +45,15 @@ class DataToAppleButtonScripts {
* Sets the appropriate data to send to ApplePay script
* Data differs between product page and cart page
*
* @param bool $is_block
* @param bool $is_block Whether the button is in a block or not.
* @return array
* @throws NotFoundException When the setting is not found.
*/
public function apple_pay_script_data( bool $is_block = false ): array {
$base_location = wc_get_base_location();
$base_location = wc_get_base_location();
$shop_country_code = $base_location['country'];
$currency_code = get_woocommerce_currency();
$total_label = get_bloginfo( 'name' );
$currency_code = get_woocommerce_currency();
$total_label = get_bloginfo( 'name' );
if ( is_product() ) {
return $this->data_for_product_page(
$shop_country_code,
@ -62,11 +72,11 @@ class DataToAppleButtonScripts {
/**
* Check if the product needs shipping
*
* @param $product
* @param \WC_Product $product The product.
*
* @return bool
*/
protected function check_if_need_shipping($product ) {
protected function check_if_need_shipping( $product ) {
if (
! wc_shipping_enabled()
|| 0 === wc_get_shipping_method_count(
@ -85,11 +95,14 @@ class DataToAppleButtonScripts {
}
/**
* @param $shop_country_code
* @param $currency_code
* @param $tota_label
* Prepares the data for the product page.
*
* @param string $shop_country_code The shop country code.
* @param string $currency_code The currency code.
* @param string $total_label The label for the total amount.
*
* @return array
* @throws NotFoundException When the setting is not found.
*/
protected function data_for_product_page(
$shop_country_code,
@ -105,30 +118,30 @@ class DataToAppleButtonScripts {
$is_variation = true;
}
$product_need_shipping = $this->check_if_need_shipping( $product );
$product_id = get_the_id();
$product_price = $product->get_price();
$product_stock = $product->get_stock_status();
$type = $this->settings->get('applepay_button_type');
$color = $this->settings->get('applepay_button_color');
$lang = $this->settings->get('applepay_button_language');
$product_id = get_the_id();
$product_price = $product->get_price();
$product_stock = $product->get_stock_status();
$type = $this->settings->get( 'applepay_button_type' );
$color = $this->settings->get( 'applepay_button_color' );
$lang = $this->settings->get( 'applepay_button_language' );
return array(
'sdk_url' => $this->sdk_url,
'button' => array(
'sdk_url' => $this->sdk_url,
'button' => array(
'wrapper' => '#applepay-container',
'mini_cart_wrapper' => '#applepay-container',
'type' => $type,
'color' => $color,
'lang' => $lang,
'type' => $type,
'color' => $color,
'lang' => $lang,
),
'product' => array(
'product' => array(
'needShipping' => $product_need_shipping,
'id' => $product_id,
'price' => $product_price,
'isVariation' => $is_variation,
'stock' => $product_stock,
),
'shop' => array(
'shop' => array(
'countryCode' => $shop_country_code,
'currencyCode' => $currency_code,
'totalLabel' => $total_label,
@ -138,9 +151,11 @@ class DataToAppleButtonScripts {
}
/**
* @param $shop_country_code
* @param $currency_code
* @param $total_label
* Prepares the data for the cart page.
*
* @param string $shop_country_code The shop country code.
* @param string $currency_code The currency code.
* @param string $total_label The label for the total amount.
*
* @return array
*/
@ -150,15 +165,15 @@ class DataToAppleButtonScripts {
$total_label
) {
$cart = WC()->cart;
$nonce = wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' );
$cart = WC()->cart;
$nonce = wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' );
$button_markup =
'<div id="applepay-container">'
. $nonce
. '</div>';
return array(
'sdk_url' => $this->sdk_url,
'button' => array(
'sdk_url' => $this->sdk_url,
'button' => array(
'wrapper' => '#applepay-container',
'mini_cart_wrapper' => '#applepay-container',
),
@ -171,7 +186,7 @@ class DataToAppleButtonScripts {
'currencyCode' => $currency_code,
'totalLabel' => $total_label,
),
'ajax_url' => admin_url( 'admin-ajax.php' ),
'ajax_url' => admin_url( 'admin-ajax.php' ),
'buttonMarkup' => $button_markup,
);
}

View file

@ -12,8 +12,8 @@ namespace WooCommerce\PayPalCommerce\Applepay\Assets;
/**
* Class PropertiesDictionary
*/
class PropertiesDictionary
{
class PropertiesDictionary {
public const BILLING_CONTACT_INVALID = 'billing Contact Invalid';
@ -108,12 +108,11 @@ class PropertiesDictionary
*
* @return array
*/
public static function button_colors(): array
{
public static function button_colors(): array {
return array(
'white' => __('White', 'woocommerce-paypal-payments'),
'black' => __('Black', 'woocommerce-paypal-payments'),
'white-outline' => __('White with outline', 'woocommerce-paypal-payments'),
'white' => __( 'White', 'woocommerce-paypal-payments' ),
'black' => __( 'Black', 'woocommerce-paypal-payments' ),
'white-outline' => __( 'White with outline', 'woocommerce-paypal-payments' ),
);
}
@ -122,17 +121,16 @@ class PropertiesDictionary
*
* @return array
*/
public static function button_types(): array
{
public static function button_types(): array {
return array(
'book' => __('Book', 'woocommerce-paypal-payments'),
'buy' => __('Buy', 'woocommerce-paypal-payments'),
'check-out' => __('Checkout', 'woocommerce-paypal-payments'),
'donate' => __('Donate', 'woocommerce-paypal-payments'),
'order' => __('Order', 'woocommerce-paypal-payments'),
'pay' => __('Pay', 'woocommerce-paypal-payments'),
'plain' => __('Plain', 'woocommerce-paypal-payments'),
'subscribe' => __('Book', 'woocommerce-paypal-payments'),
'book' => __( 'Book', 'woocommerce-paypal-payments' ),
'buy' => __( 'Buy', 'woocommerce-paypal-payments' ),
'check-out' => __( 'Checkout', 'woocommerce-paypal-payments' ),
'donate' => __( 'Donate', 'woocommerce-paypal-payments' ),
'order' => __( 'Order', 'woocommerce-paypal-payments' ),
'pay' => __( 'Pay', 'woocommerce-paypal-payments' ),
'plain' => __( 'Plain', 'woocommerce-paypal-payments' ),
'subscribe' => __( 'Book', 'woocommerce-paypal-payments' ),
);
}
@ -141,48 +139,47 @@ class PropertiesDictionary
*
* @return array
*/
public static function button_languages(): array
{
public static function button_languages(): array {
return array(
"ar-AB" => __('Arabic', 'woocommerce-paypal-payments'),
"ca-ES" => __('Catalan', 'woocommerce-paypal-payments'),
"cs-CZ" => __('Czech', 'woocommerce-paypal-payments'),
"da-DK" => __('Danish', 'woocommerce-paypal-payments'),
"de-DE" => __('German', 'woocommerce-paypal-payments'),
"el-GR" => __('Greek', 'woocommerce-paypal-payments'),
"en-AU" => __('English (Australia)', 'woocommerce-paypal-payments'),
"en-GB" => __('English (United Kingdom)', 'woocommerce-paypal-payments'),
"en-US" => __('English (United States)', 'woocommerce-paypal-payments'),
"es-ES" => __('Spanish (Spain)', 'woocommerce-paypal-payments'),
"es-MX" => __('Spanish (Mexico)', 'woocommerce-paypal-payments'),
"fi-FI" => __('Finnish', 'woocommerce-paypal-payments'),
"fr-CA" => __('French (Canada)', 'woocommerce-paypal-payments'),
"fr-FR" => __('French (France)', 'woocommerce-paypal-payments'),
"he-IL" => __('Hebrew', 'woocommerce-paypal-payments'),
"hi-IN" => __('Hindi', 'woocommerce-paypal-payments'),
"hr-HR" => __('Croatian', 'woocommerce-paypal-payments'),
"hu-HU" => __('Hungarian', 'woocommerce-paypal-payments'),
"id-ID" => __('Indonesian', 'woocommerce-paypal-payments'),
"it-IT" => __('Italian', 'woocommerce-paypal-payments'),
"ja-JP" => __('Japanese', 'woocommerce-paypal-payments'),
"ko-KR" => __('Korean', 'woocommerce-paypal-payments'),
"ms-MY" => __('Malay', 'woocommerce-paypal-payments'),
"nb-NO" => __('Norwegian', 'woocommerce-paypal-payments'),
"nl-NL" => __('Dutch', 'woocommerce-paypal-payments'),
"pl-PL" => __('Polish', 'woocommerce-paypal-payments'),
"pt-BR" => __('Portuguese (Brazil)', 'woocommerce-paypal-payments'),
"pt-PT" => __('Portuguese (Portugal)', 'woocommerce-paypal-payments'),
"ro-RO" => __('Romanian', 'woocommerce-paypal-payments'),
"ru-RU" => __('Russian', 'woocommerce-paypal-payments'),
"sk-SK" => __('Slovak', 'woocommerce-paypal-payments'),
"sv-SE" => __('Swedish', 'woocommerce-paypal-payments'),
"th-TH" => __('Thai', 'woocommerce-paypal-payments'),
"tr-TR" => __('Turkish', 'woocommerce-paypal-payments'),
"uk-UA" => __('Ukrainian', 'woocommerce-paypal-payments'),
"vi-VN" => __('Vietnamese', 'woocommerce-paypal-payments'),
"zh-CN" => __('Chinese (Simplified)', 'woocommerce-paypal-payments'),
"zh-HK" => __('Chinese (Hong Kong)', 'woocommerce-paypal-payments'),
"zh-TW" => __('Chinese (Traditional)', 'woocommerce-paypal-payments'),
'ar-AB' => __( 'Arabic', 'woocommerce-paypal-payments' ),
'ca-ES' => __( 'Catalan', 'woocommerce-paypal-payments' ),
'cs-CZ' => __( 'Czech', 'woocommerce-paypal-payments' ),
'da-DK' => __( 'Danish', 'woocommerce-paypal-payments' ),
'de-DE' => __( 'German', 'woocommerce-paypal-payments' ),
'el-GR' => __( 'Greek', 'woocommerce-paypal-payments' ),
'en-AU' => __( 'English (Australia)', 'woocommerce-paypal-payments' ),
'en-GB' => __( 'English (United Kingdom)', 'woocommerce-paypal-payments' ),
'en-US' => __( 'English (United States)', 'woocommerce-paypal-payments' ),
'es-ES' => __( 'Spanish (Spain)', 'woocommerce-paypal-payments' ),
'es-MX' => __( 'Spanish (Mexico)', 'woocommerce-paypal-payments' ),
'fi-FI' => __( 'Finnish', 'woocommerce-paypal-payments' ),
'fr-CA' => __( 'French (Canada)', 'woocommerce-paypal-payments' ),
'fr-FR' => __( 'French (France)', 'woocommerce-paypal-payments' ),
'he-IL' => __( 'Hebrew', 'woocommerce-paypal-payments' ),
'hi-IN' => __( 'Hindi', 'woocommerce-paypal-payments' ),
'hr-HR' => __( 'Croatian', 'woocommerce-paypal-payments' ),
'hu-HU' => __( 'Hungarian', 'woocommerce-paypal-payments' ),
'id-ID' => __( 'Indonesian', 'woocommerce-paypal-payments' ),
'it-IT' => __( 'Italian', 'woocommerce-paypal-payments' ),
'ja-JP' => __( 'Japanese', 'woocommerce-paypal-payments' ),
'ko-KR' => __( 'Korean', 'woocommerce-paypal-payments' ),
'ms-MY' => __( 'Malay', 'woocommerce-paypal-payments' ),
'nb-NO' => __( 'Norwegian', 'woocommerce-paypal-payments' ),
'nl-NL' => __( 'Dutch', 'woocommerce-paypal-payments' ),
'pl-PL' => __( 'Polish', 'woocommerce-paypal-payments' ),
'pt-BR' => __( 'Portuguese (Brazil)', 'woocommerce-paypal-payments' ),
'pt-PT' => __( 'Portuguese (Portugal)', 'woocommerce-paypal-payments' ),
'ro-RO' => __( 'Romanian', 'woocommerce-paypal-payments' ),
'ru-RU' => __( 'Russian', 'woocommerce-paypal-payments' ),
'sk-SK' => __( 'Slovak', 'woocommerce-paypal-payments' ),
'sv-SE' => __( 'Swedish', 'woocommerce-paypal-payments' ),
'th-TH' => __( 'Thai', 'woocommerce-paypal-payments' ),
'tr-TR' => __( 'Turkish', 'woocommerce-paypal-payments' ),
'uk-UA' => __( 'Ukrainian', 'woocommerce-paypal-payments' ),
'vi-VN' => __( 'Vietnamese', 'woocommerce-paypal-payments' ),
'zh-CN' => __( 'Chinese (Simplified)', 'woocommerce-paypal-payments' ),
'zh-HK' => __( 'Chinese (Hong Kong)', 'woocommerce-paypal-payments' ),
'zh-TW' => __( 'Chinese (Traditional)', 'woocommerce-paypal-payments' ),
);
}
}

View file

@ -1,12 +1,17 @@
<?php
/**
* Prepares the data for the response to Apple.
*
* @package WooCommerce\PayPalCommerce\Applepay
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay\Assets;
use Psr\Log\LoggerInterface;
use WC_Payment_Gateway;
/**
* Class ResponsesToApple
*/
class ResponsesToApple {
/**
@ -14,9 +19,10 @@ class ResponsesToApple {
* Adds the error list if provided to be handled by the script
* On success it adds the redirection url
*
* @param $status 0 => success, 1 => error
* @param string $order_id
* @param array $error_list
* @param int $status 0 => success, 1 => error.
* @param string $order_id The order ID.
* @param array $error_list [['errorCode'=>required, 'contactField'=>'']].
* @param string $return_url The URL to redirect to.
*
* @return array
*/
@ -44,13 +50,12 @@ class ResponsesToApple {
/**
* Returns an error response to be handled by the script
*
* @param array $errorList [['errorCode'=>required, 'contactField'=>'']]
*
* @param array $error_list [['errorCode'=>required, 'contactField'=>'']].
* @return void
*/
public function response_with_data_errors( $errorList ) {
public function response_with_data_errors( $error_list ) {
$response = array();
$response['errors'] = $this->apple_pay_error( $errorList );
$response['errors'] = $this->apple_pay_error( $error_list );
$response['newTotal'] = $this->apple_new_total_response(
0,
'pending'
@ -61,6 +66,7 @@ class ResponsesToApple {
/**
* Creates a response formatted for ApplePay
*
* @param array $payment_details Payment details.
* @return array
*/
public function apple_formatted_response( array $payment_details ) {
@ -81,16 +87,18 @@ class ResponsesToApple {
/**
* Returns a success response to be handled by the script
*
* @param array $response Response to send.
*/
public function response_success( array $response ) {
public function response_success( array $response ): void {
wp_send_json_success( $response );
}
/**
* Creates an array of errors formatted
*
* @param array $error_list
* @param array $errors
* @param array $error_list List of errors.
* @param array $errors (optional).
*
* @return array
*/
@ -110,9 +118,8 @@ class ResponsesToApple {
/**
* Creates NewTotals line
*
* @param $total
*
* @param string $type
* @param float $total Total amount.
* @param string $type final or pending.
*
* @return array
*/
@ -127,9 +134,9 @@ class ResponsesToApple {
/**
* Creates item line
*
* @param $subtotal_label
* @param $subtotal
* @param $type
* @param string $subtotal_label Subtotal label.
* @param float $subtotal Subtotal amount.
* @param string $type final or pending.
*
* @return array
*/
@ -144,6 +151,7 @@ class ResponsesToApple {
/**
* Creates NewLineItems line
*
* @param array $payment_details Payment details.
* @return array[]
*/
protected function apple_new_line_items_response( array $payment_details ): array {