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

@ -63,7 +63,7 @@ return array(
$container->get( 'applepay.url' ),
$container->get( 'ppcp.asset-version' ),
$container->get( 'applepay.data_to_scripts' ),
$container->get( 'wcgateway.settings.status' ),
$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
@ -86,7 +88,7 @@ class ApplepayModule implements ModuleInterface {
$this->load_assets( $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',
@ -110,8 +112,9 @@ class ApplepayModule implements ModuleInterface {
* Loads the validation string.
*
* @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;
}
@ -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,11 +176,13 @@ 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 ) {
@ -210,11 +214,12 @@ class ApplepayModule implements ModuleInterface {
}
/**
* @param ContainerInterface $c
* Handles the validation file.
*
* @param ContainerInterface $c The container.
* @return void
*/
public function handle_validation_file(ContainerInterface $c): 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 );

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;
@ -98,6 +115,10 @@ class ApplePayButton implements ButtonInterface {
* @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,
@ -241,8 +262,7 @@ 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() ) {
return;
@ -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();
@ -332,7 +350,8 @@ 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();
@ -359,12 +378,15 @@ 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 {
// 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,7 +435,7 @@ 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
*/
@ -421,7 +443,7 @@ class ApplePayButton implements ButtonInterface {
$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' ) {
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 ) ) {
@ -436,7 +458,7 @@ class ApplePayButton implements ButtonInterface {
$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,10 +473,9 @@ 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,
@ -463,7 +484,7 @@ class ApplePayButton implements ButtonInterface {
): 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,11 +656,11 @@ 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,
array $customer_address,
$shipping_method = null
): 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,64 +768,40 @@ 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
{
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
{
/*
TODO
public function process_order_as_paid( $order_id ): void {
$order = wc_get_order( $order_id );
if ( ! assert( $order instanceof WC_Order ) ) {
return;
@ -802,14 +810,19 @@ class ApplePayButton implements ButtonInterface {
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['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'] ?? '';
@ -821,8 +834,7 @@ class ApplePayButton implements ButtonInterface {
$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 ) {
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'] ?? '';
@ -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() {
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
*/
@ -356,7 +387,7 @@ 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 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,11 +26,17 @@ class DataToAppleButtonScripts {
/**
* The settings.
*
* @var array
* @var Settings
*/
private $settings;
public function __construct($sdk_url, $settings) {
/**
* 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,8 +45,9 @@ 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();
@ -62,7 +72,7 @@ class DataToAppleButtonScripts {
/**
* Check if the product needs shipping
*
* @param $product
* @param \WC_Product $product The product.
*
* @return bool
*/
@ -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,
@ -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
*/

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,8 +108,7 @@ 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' ),
@ -122,8 +121,7 @@ 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' ),
@ -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 {