mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
move module.local to module
This commit is contained in:
parent
c443e4053c
commit
f8e82bdfaf
217 changed files with 8 additions and 2 deletions
44
modules/ppcp-button/src/Assets/class-disabledsmartbutton.php
Normal file
44
modules/ppcp-button/src/Assets/class-disabledsmartbutton.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
/**
|
||||
* If we can't render our buttons, this Null object will be used.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Assets
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Assets;
|
||||
|
||||
/**
|
||||
* Class DisabledSmartButton
|
||||
*/
|
||||
class DisabledSmartButton implements SmartButtonInterface {
|
||||
|
||||
/**
|
||||
* Renders the necessary HTML.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function render_wrapper(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues necessary scripts.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function enqueue(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether tokens can be stored or not.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function can_save_vault_token(): bool {
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
816
modules/ppcp-button/src/Assets/class-smartbutton.php
Normal file
816
modules/ppcp-button/src/Assets/class-smartbutton.php
Normal file
|
@ -0,0 +1,816 @@
|
|||
<?php
|
||||
/**
|
||||
* Registers and configures the necessary Javascript for the button, credit messaging and DCC fields.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Assets
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Assets;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Helper\DccApplies;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\DataClientIdEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
|
||||
use Inpsyde\PayPalCommerce\Button\Helper\MessagesApply;
|
||||
use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
||||
use Inpsyde\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
/**
|
||||
* Class SmartButton
|
||||
*/
|
||||
class SmartButton implements SmartButtonInterface {
|
||||
|
||||
/**
|
||||
* The URL to the module.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $module_url;
|
||||
|
||||
/**
|
||||
* The Session Handler.
|
||||
*
|
||||
* @var SessionHandler
|
||||
*/
|
||||
private $session_handler;
|
||||
|
||||
/**
|
||||
* The settings.
|
||||
*
|
||||
* @var Settings
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* The Payee Repository.
|
||||
*
|
||||
* @var PayeeRepository
|
||||
*/
|
||||
private $payee_repository;
|
||||
|
||||
/**
|
||||
* The Identity Token.
|
||||
*
|
||||
* @var IdentityToken
|
||||
*/
|
||||
private $identity_token;
|
||||
|
||||
/**
|
||||
* The Payer Factory.
|
||||
*
|
||||
* @var PayerFactory
|
||||
*/
|
||||
private $payer_factory;
|
||||
|
||||
/**
|
||||
* The client ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $client_id;
|
||||
|
||||
/**
|
||||
* The Request Data.
|
||||
*
|
||||
* @var RequestData
|
||||
*/
|
||||
private $request_data;
|
||||
|
||||
/**
|
||||
* The DCC Applies helper.
|
||||
*
|
||||
* @var DccApplies
|
||||
*/
|
||||
private $dcc_applies;
|
||||
|
||||
/**
|
||||
* The Subscription Helper.
|
||||
*
|
||||
* @var SubscriptionHelper
|
||||
*/
|
||||
private $subscription_helper;
|
||||
|
||||
/**
|
||||
* The Messages apply helper.
|
||||
*
|
||||
* @var MessagesApply
|
||||
*/
|
||||
private $messages_apply;
|
||||
|
||||
/**
|
||||
* SmartButton constructor.
|
||||
*
|
||||
* @param string $module_url The URL to the module.
|
||||
* @param SessionHandler $session_handler The Session Handler.
|
||||
* @param Settings $settings The Settings.
|
||||
* @param PayeeRepository $payee_repository The Payee Repository.
|
||||
* @param IdentityToken $identity_token The Identity Token.
|
||||
* @param PayerFactory $payer_factory The Payer factory.
|
||||
* @param string $client_id The client ID.
|
||||
* @param RequestData $request_data The Request Data helper.
|
||||
* @param DccApplies $dcc_applies The DCC applies helper.
|
||||
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
||||
* @param MessagesApply $messages_apply The Messages apply helper.
|
||||
*/
|
||||
public function __construct(
|
||||
string $module_url,
|
||||
SessionHandler $session_handler,
|
||||
Settings $settings,
|
||||
PayeeRepository $payee_repository,
|
||||
IdentityToken $identity_token,
|
||||
PayerFactory $payer_factory,
|
||||
string $client_id,
|
||||
RequestData $request_data,
|
||||
DccApplies $dcc_applies,
|
||||
SubscriptionHelper $subscription_helper,
|
||||
MessagesApply $messages_apply
|
||||
) {
|
||||
|
||||
$this->module_url = $module_url;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->settings = $settings;
|
||||
$this->payee_repository = $payee_repository;
|
||||
$this->identity_token = $identity_token;
|
||||
$this->payer_factory = $payer_factory;
|
||||
$this->client_id = $client_id;
|
||||
$this->request_data = $request_data;
|
||||
$this->dcc_applies = $dcc_applies;
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
$this->messages_apply = $messages_apply;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the necessary action hooks to render the HTML depending on the settings.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
|
||||
*/
|
||||
public function render_wrapper(): bool {
|
||||
|
||||
if ( ! $this->can_save_vault_token() && $this->has_subscriptions() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' ) ) {
|
||||
$this->render_button_wrapper_registrar();
|
||||
$this->render_message_wrapper_registrar();
|
||||
}
|
||||
|
||||
if (
|
||||
$this->settings->has( 'dcc_gateway_enabled' )
|
||||
&& $this->settings->get( 'dcc_gateway_enabled' )
|
||||
&& ! $this->session_handler->order()
|
||||
) {
|
||||
add_action(
|
||||
'woocommerce_review_order_after_submit',
|
||||
array(
|
||||
$this,
|
||||
'dcc_renderer',
|
||||
),
|
||||
11
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the hooks to render the credit messaging HTML depending on the settings.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
|
||||
*/
|
||||
private function render_message_wrapper_registrar(): bool {
|
||||
|
||||
$not_enabled_on_cart = $this->settings->has( 'message_cart_enabled' ) &&
|
||||
! $this->settings->get( 'message_cart_enabled' );
|
||||
if (
|
||||
is_cart()
|
||||
&& ! $not_enabled_on_cart
|
||||
) {
|
||||
add_action(
|
||||
'woocommerce_proceed_to_checkout',
|
||||
array(
|
||||
$this,
|
||||
'message_renderer',
|
||||
),
|
||||
19
|
||||
);
|
||||
}
|
||||
|
||||
$not_enabled_on_product_page = $this->settings->has( 'message_product_enabled' ) &&
|
||||
! $this->settings->get( 'message_product_enabled' );
|
||||
if (
|
||||
( is_product() || wc_post_content_has_shortcode( 'product_page' ) )
|
||||
&& ! $not_enabled_on_product_page
|
||||
) {
|
||||
add_action(
|
||||
'woocommerce_single_product_summary',
|
||||
array(
|
||||
$this,
|
||||
'message_renderer',
|
||||
),
|
||||
30
|
||||
);
|
||||
}
|
||||
|
||||
$not_enabled_on_checkout = $this->settings->has( 'message_enabled' ) &&
|
||||
! $this->settings->get( 'message_enabled' );
|
||||
if ( ! $not_enabled_on_checkout ) {
|
||||
add_action(
|
||||
'woocommerce_review_order_after_submit',
|
||||
array(
|
||||
$this,
|
||||
'message_renderer',
|
||||
),
|
||||
11
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the hooks where to render the button HTML according to the settings.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
|
||||
*/
|
||||
private function render_button_wrapper_registrar(): bool {
|
||||
|
||||
$not_enabled_on_cart = $this->settings->has( 'button_cart_enabled' ) &&
|
||||
! $this->settings->get( 'button_cart_enabled' );
|
||||
if (
|
||||
is_cart()
|
||||
&& ! $not_enabled_on_cart
|
||||
) {
|
||||
add_action(
|
||||
'woocommerce_proceed_to_checkout',
|
||||
array(
|
||||
$this,
|
||||
'button_renderer',
|
||||
),
|
||||
20
|
||||
);
|
||||
}
|
||||
|
||||
$not_enabled_on_product_page = $this->settings->has( 'button_single_product_enabled' ) &&
|
||||
! $this->settings->get( 'button_single_product_enabled' );
|
||||
if (
|
||||
( is_product() || wc_post_content_has_shortcode( 'product_page' ) )
|
||||
&& ! $not_enabled_on_product_page
|
||||
) {
|
||||
add_action(
|
||||
'woocommerce_single_product_summary',
|
||||
array(
|
||||
$this,
|
||||
'button_renderer',
|
||||
),
|
||||
31
|
||||
);
|
||||
}
|
||||
|
||||
$not_enabled_on_minicart = $this->settings->has( 'button_mini_cart_enabled' ) &&
|
||||
! $this->settings->get( 'button_mini_cart_enabled' );
|
||||
if (
|
||||
! $not_enabled_on_minicart
|
||||
) {
|
||||
add_action(
|
||||
'woocommerce_widget_shopping_cart_after_buttons',
|
||||
static function () {
|
||||
echo '<p
|
||||
id="ppc-button-minicart"
|
||||
class="woocommerce-mini-cart__buttons buttons"
|
||||
></p>';
|
||||
},
|
||||
30
|
||||
);
|
||||
}
|
||||
|
||||
add_action( 'woocommerce_review_order_after_submit', array( $this, 'button_renderer' ), 10 );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues the script.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
|
||||
*/
|
||||
public function enqueue(): bool {
|
||||
$buttons_enabled = $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' );
|
||||
if ( ! is_checkout() && ! $buttons_enabled ) {
|
||||
return false;
|
||||
}
|
||||
if ( ! $this->can_save_vault_token() && $this->has_subscriptions() ) {
|
||||
return false;
|
||||
}
|
||||
wp_enqueue_style(
|
||||
'ppcp-hosted-fields',
|
||||
$this->module_url . '/assets/css/hosted-fields.css',
|
||||
array(),
|
||||
1
|
||||
);
|
||||
wp_enqueue_script(
|
||||
'ppcp-smart-button',
|
||||
$this->module_url . '/assets/js/button.js',
|
||||
array( 'jquery' ),
|
||||
1,
|
||||
true
|
||||
);
|
||||
|
||||
wp_localize_script(
|
||||
'ppcp-smart-button',
|
||||
'PayPalCommerceGateway',
|
||||
$this->localize_script()
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the HTML for the buttons.
|
||||
*/
|
||||
public function button_renderer() {
|
||||
$product = wc_get_product();
|
||||
if (
|
||||
! is_checkout() && is_a( $product, \WC_Product::class )
|
||||
&& (
|
||||
$product->is_type( array( 'external', 'grouped' ) )
|
||||
|| ! $product->is_in_stock()
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo '<div id="ppc-button"></div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the HTML for the credit messaging.
|
||||
*/
|
||||
public function message_renderer() {
|
||||
|
||||
echo '<div id="ppcp-messages"></div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* The values for the credit messaging.
|
||||
*
|
||||
* @return array
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
|
||||
*/
|
||||
private function message_values(): array {
|
||||
|
||||
if (
|
||||
$this->settings->has( 'disable_funding' )
|
||||
&& in_array( 'credit', (array) $this->settings->get( 'disable_funding' ), true )
|
||||
) {
|
||||
return array();
|
||||
}
|
||||
$placement = 'product';
|
||||
$product = wc_get_product();
|
||||
$amount = ( is_a( $product, \WC_Product::class ) ) ? wc_get_price_including_tax( $product ) : 0;
|
||||
$layout = $this->settings->has( 'message_product_layout' ) ?
|
||||
$this->settings->get( 'message_product_layout' ) : 'text';
|
||||
$logo_type = $this->settings->has( 'message_product_logo' ) ?
|
||||
$this->settings->get( 'message_product_logo' ) : 'primary';
|
||||
$logo_position = $this->settings->has( 'message_product_position' ) ?
|
||||
$this->settings->get( 'message_product_position' ) : 'left';
|
||||
$text_color = $this->settings->has( 'message_product_color' ) ?
|
||||
$this->settings->get( 'message_product_color' ) : 'black';
|
||||
$style_color = $this->settings->has( 'message_product_flex_color' ) ?
|
||||
$this->settings->get( 'message_product_flex_color' ) : 'blue';
|
||||
$ratio = $this->settings->has( 'message_product_flex_ratio' ) ?
|
||||
$this->settings->get( 'message_product_flex_ratio' ) : '1x1';
|
||||
$should_show = $this->settings->has( 'message_product_enabled' )
|
||||
&& $this->settings->get( 'message_product_enabled' );
|
||||
if ( is_checkout() ) {
|
||||
$placement = 'payment';
|
||||
$amount = WC()->cart->get_total( 'raw' );
|
||||
$layout = $this->settings->has( 'message_layout' ) ?
|
||||
$this->settings->get( 'message_layout' ) : 'text';
|
||||
$logo_type = $this->settings->has( 'message_logo' ) ?
|
||||
$this->settings->get( 'message_logo' ) : 'primary';
|
||||
$logo_position = $this->settings->has( 'message_position' ) ?
|
||||
$this->settings->get( 'message_position' ) : 'left';
|
||||
$text_color = $this->settings->has( 'message_color' ) ?
|
||||
$this->settings->get( 'message_color' ) : 'black';
|
||||
$style_color = $this->settings->has( 'message_flex_color' ) ?
|
||||
$this->settings->get( 'message_flex_color' ) : 'blue';
|
||||
$ratio = $this->settings->has( 'message_flex_ratio' ) ?
|
||||
$this->settings->get( 'message_flex_ratio' ) : '1x1';
|
||||
$should_show = $this->settings->has( 'message_enabled' )
|
||||
&& $this->settings->get( 'message_enabled' );
|
||||
}
|
||||
if ( is_cart() ) {
|
||||
$placement = 'cart';
|
||||
$amount = WC()->cart->get_total( 'raw' );
|
||||
$layout = $this->settings->has( 'message_cart_layout' ) ?
|
||||
$this->settings->get( 'message_cart_layout' ) : 'text';
|
||||
$logo_type = $this->settings->has( 'message_cart_logo' ) ?
|
||||
$this->settings->get( 'message_cart_logo' ) : 'primary';
|
||||
$logo_position = $this->settings->has( 'message_cart_position' ) ?
|
||||
$this->settings->get( 'message_cart_position' ) : 'left';
|
||||
$text_color = $this->settings->has( 'message_cart_color' ) ?
|
||||
$this->settings->get( 'message_cart_color' ) : 'black';
|
||||
$style_color = $this->settings->has( 'message_cart_flex_color' ) ?
|
||||
$this->settings->get( 'message_cart_flex_color' ) : 'blue';
|
||||
$ratio = $this->settings->has( 'message_cart_flex_ratio' ) ?
|
||||
$this->settings->get( 'message_cart_flex_ratio' ) : '1x1';
|
||||
$should_show = $this->settings->has( 'message_cart_enabled' )
|
||||
&& $this->settings->get( 'message_cart_enabled' );
|
||||
}
|
||||
|
||||
if ( ! $should_show ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$values = array(
|
||||
'wrapper' => '#ppcp-messages',
|
||||
'amount' => $amount,
|
||||
'placement' => $placement,
|
||||
'style' => array(
|
||||
'layout' => $layout,
|
||||
'logo' => array(
|
||||
'type' => $logo_type,
|
||||
'position' => $logo_position,
|
||||
),
|
||||
'text' => array(
|
||||
'color' => $text_color,
|
||||
),
|
||||
'color' => $style_color,
|
||||
'ratio' => $ratio,
|
||||
),
|
||||
);
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the HTML for the DCC fields.
|
||||
*
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting hasnt been found.
|
||||
*/
|
||||
public function dcc_renderer() {
|
||||
|
||||
$id = 'ppcp-hosted-fields';
|
||||
$can_render_dcc = $this->dcc_applies->for_country_currency()
|
||||
&& $this->settings->has( 'client_id' )
|
||||
&& $this->settings->get( 'client_id' );
|
||||
if ( ! $can_render_dcc ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$save_card = $this->can_save_vault_token() ? sprintf(
|
||||
'<div>
|
||||
|
||||
<label for="ppcp-vault-%1$s">%2$s</label>
|
||||
<input
|
||||
type="checkbox"
|
||||
id="ppcp-vault-%1$s"
|
||||
class="ppcp-credit-card-vault"
|
||||
name="vault"
|
||||
>
|
||||
</div>',
|
||||
esc_attr( $id ),
|
||||
esc_html__( 'Save your card', 'paypal-for-woocommerce' )
|
||||
) : '';
|
||||
|
||||
printf(
|
||||
'<form id="%1$s">
|
||||
<div class="ppcp-dcc-credit-card-wrapper" style="display: none">
|
||||
<label for="ppcp-credit-card-%1$s">%2$s</label>
|
||||
<span id="ppcp-credit-card-%1$s" class="ppcp-credit-card"></span>
|
||||
<label for="ppcp-expiration-date-%1$s">%3$s</label>
|
||||
<span
|
||||
id="ppcp-expiration-date-%1$s"
|
||||
class="ppcp-expiration-date"
|
||||
></span>
|
||||
<label for="ppcp-cvv-%1$s">%4$s</label>
|
||||
<span id="ppcp-cvv-%1$s" class="ppcp-cvv"></span>
|
||||
%5$s
|
||||
<button class="button alt">%6$s</button>
|
||||
</div>
|
||||
</form><div id="payments-sdk__contingency-lightbox"></div>',
|
||||
esc_attr( $id ),
|
||||
esc_html__( 'Credit Card number', 'paypal-for-woocommerce' ),
|
||||
esc_html__( 'Expiration', 'paypal-for-woocommerce' ),
|
||||
esc_html__( 'CVV', 'paypal-for-woocommerce' ),
|
||||
//phpcs:ignore
|
||||
$save_card,
|
||||
esc_html__( 'Place order', 'paypal-for-woocommerce' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we can store vault tokens or not.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting hasnt been found.
|
||||
*/
|
||||
public function can_save_vault_token(): bool {
|
||||
|
||||
if ( ! $this->settings->has( 'client_id' ) || ! $this->settings->get( 'client_id' ) ) {
|
||||
return false;
|
||||
}
|
||||
if ( ! $this->settings->has( 'vault_enabled' ) || ! $this->settings->get( 'vault_enabled' ) ) {
|
||||
return false;
|
||||
}
|
||||
return is_user_logged_in();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we need to initialize the script to enable tokenization for subscriptions or not.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function has_subscriptions(): bool {
|
||||
if ( ! $this->subscription_helper->accept_only_automatic_payment_gateways() ) {
|
||||
return false;
|
||||
}
|
||||
if ( is_product() ) {
|
||||
return $this->subscription_helper->current_product_is_subscription();
|
||||
}
|
||||
return $this->subscription_helper->cart_contains_subscription();
|
||||
}
|
||||
|
||||
/**
|
||||
* The localized data for the smart button.
|
||||
*
|
||||
* @return array
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting hasn't been found.
|
||||
*/
|
||||
private function localize_script(): array {
|
||||
$this->request_data->enqueue_nonce_fix();
|
||||
$localize = array(
|
||||
'script_attributes' => $this->attributes(),
|
||||
'data_client_id' => array(
|
||||
'set_attribute' => ( is_checkout() && $this->dcc_is_enabled() )
|
||||
|| $this->can_save_vault_token(),
|
||||
'endpoint' => home_url( \WC_AJAX::get_endpoint( DataClientIdEndpoint::ENDPOINT ) ),
|
||||
'nonce' => wp_create_nonce( DataClientIdEndpoint::nonce() ),
|
||||
'user' => get_current_user_id(),
|
||||
),
|
||||
'redirect' => wc_get_checkout_url(),
|
||||
'context' => $this->context(),
|
||||
'ajax' => array(
|
||||
'change_cart' => array(
|
||||
'endpoint' => home_url( \WC_AJAX::get_endpoint( ChangeCartEndpoint::ENDPOINT ) ),
|
||||
'nonce' => wp_create_nonce( ChangeCartEndpoint::nonce() ),
|
||||
),
|
||||
'create_order' => array(
|
||||
'endpoint' => home_url( \WC_AJAX::get_endpoint( CreateOrderEndpoint::ENDPOINT ) ),
|
||||
'nonce' => wp_create_nonce( CreateOrderEndpoint::nonce() ),
|
||||
),
|
||||
'approve_order' => array(
|
||||
'endpoint' => home_url( \WC_AJAX::get_endpoint( ApproveOrderEndpoint::ENDPOINT ) ),
|
||||
'nonce' => wp_create_nonce( ApproveOrderEndpoint::nonce() ),
|
||||
),
|
||||
),
|
||||
'enforce_vault' => $this->has_subscriptions(),
|
||||
'bn_codes' => $this->bn_codes(),
|
||||
'payer' => $this->payerData(),
|
||||
'button' => array(
|
||||
'wrapper' => '#ppc-button',
|
||||
'mini_cart_wrapper' => '#ppc-button-minicart',
|
||||
'cancel_wrapper' => '#ppcp-cancel',
|
||||
'url' => $this->url(),
|
||||
'mini_cart_style' => array(
|
||||
'layout' => $this->style_for_context( 'layout', 'mini-cart' ),
|
||||
'color' => $this->style_for_context( 'color', 'mini-cart' ),
|
||||
'shape' => $this->style_for_context( 'shape', 'mini-cart' ),
|
||||
'label' => $this->style_for_context( 'label', 'mini-cart' ),
|
||||
'tagline' => $this->style_for_context( 'tagline', 'mini-cart' ),
|
||||
),
|
||||
'style' => array(
|
||||
'layout' => $this->style_for_context( 'layout', $this->context() ),
|
||||
'color' => $this->style_for_context( 'color', $this->context() ),
|
||||
'shape' => $this->style_for_context( 'shape', $this->context() ),
|
||||
'label' => $this->style_for_context( 'label', $this->context() ),
|
||||
'tagline' => $this->style_for_context( 'tagline', $this->context() ),
|
||||
),
|
||||
),
|
||||
'hosted_fields' => array(
|
||||
'wrapper' => '#ppcp-hosted-fields',
|
||||
'mini_cart_wrapper' => '#ppcp-hosted-fields-mini-cart',
|
||||
'labels' => array(
|
||||
'credit_card_number' => '',
|
||||
'cvv' => '',
|
||||
'mm_yyyy' => __( 'MM/YYYY', 'paypal-for-woocommerce' ),
|
||||
'fields_not_valid' => __(
|
||||
'Unfortunatly, your credit card details are not valid.',
|
||||
'paypal-for-woocommerce'
|
||||
),
|
||||
),
|
||||
),
|
||||
'messages' => $this->message_values(),
|
||||
'labels' => array(
|
||||
'error' => array(
|
||||
'generic' => __(
|
||||
'Something went wrong. Please try again or choose another payment source.',
|
||||
'paypal-for-woocommerce'
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if ( $this->style_for_context( 'layout', 'mini-cart' ) !== 'horizontal' ) {
|
||||
unset( $localize['button']['mini_cart_style']['tagline'] );
|
||||
}
|
||||
if ( $this->style_for_context( 'layout', $this->context() ) !== 'horizontal' ) {
|
||||
unset( $localize['button']['style']['tagline'] );
|
||||
}
|
||||
|
||||
$this->request_data->dequeue_nonce_fix();
|
||||
return $localize;
|
||||
}
|
||||
|
||||
/**
|
||||
* If we can find the payer data for a current customer, we will return it.
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
private function payerData(): ?array {
|
||||
|
||||
$customer = WC()->customer;
|
||||
if ( ! is_user_logged_in() || ! is_a( $customer, \WC_Customer::class ) ) {
|
||||
return null;
|
||||
}
|
||||
return $this->payer_factory->from_customer( $customer )->to_array();
|
||||
}
|
||||
|
||||
/**
|
||||
* The JavaScript SDK url to load.
|
||||
*
|
||||
* @return string
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting was not found.
|
||||
*/
|
||||
private function url(): string {
|
||||
$params = array(
|
||||
'client-id' => $this->client_id,
|
||||
'currency' => get_woocommerce_currency(),
|
||||
'locale' => get_user_locale(),
|
||||
// ToDo: Update date on releases.
|
||||
'integration-date' => gmdate( 'Y-m-d' ),
|
||||
'components' => implode( ',', $this->components() ),
|
||||
'vault' => ( is_checkout() && $this->dcc_is_enabled() ) || $this->can_save_vault_token() ?
|
||||
'true' : 'false',
|
||||
'commit' => is_checkout() ? 'true' : 'false',
|
||||
'intent' => ( $this->settings->has( 'intent' ) ) ?
|
||||
$this->settings->get( 'intent' ) : 'capture',
|
||||
);
|
||||
if (
|
||||
defined( 'WP_DEBUG' ) && \WP_DEBUG
|
||||
&& WC()->customer && WC()->customer->get_billing_country()
|
||||
) {
|
||||
$params['buyer-country'] = WC()->customer->get_billing_country();
|
||||
}
|
||||
$payee = $this->payee_repository->payee();
|
||||
if ( $payee->merchant_id() ) {
|
||||
$params['merchant-id'] = $payee->merchant_id();
|
||||
}
|
||||
$disable_funding = $this->settings->has( 'disable_funding' ) ?
|
||||
$this->settings->get( 'disable_funding' ) : array();
|
||||
$disable_funding[] = 'venmo';
|
||||
if ( ! is_checkout() ) {
|
||||
$disable_funding[] = 'card';
|
||||
}
|
||||
$params['disable-funding'] = implode( ',', array_unique( $disable_funding ) );
|
||||
$smart_button_url = add_query_arg( $params, 'https://www.paypal.com/sdk/js' );
|
||||
return $smart_button_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* The attributes we need to load for the JS SDK.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function attributes(): array {
|
||||
return array(
|
||||
'data-partner-attribution-id' => $this->bn_code_for_context( $this->context() ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* What BN Code to use in a given context.
|
||||
*
|
||||
* @param string $context The context.
|
||||
* @return string
|
||||
*/
|
||||
private function bn_code_for_context( string $context ): string {
|
||||
|
||||
$codes = $this->bn_codes();
|
||||
return ( isset( $codes[ $context ] ) ) ? $codes[ $context ] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* BN Codes to use.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function bn_codes(): array {
|
||||
|
||||
return array(
|
||||
'checkout' => 'Woo_PPCP',
|
||||
'cart' => 'Woo_PPCP',
|
||||
'mini-cart' => 'Woo_PPCP',
|
||||
'product' => 'Woo_PPCP',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The JS SKD components we need to load.
|
||||
*
|
||||
* @return array
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting was not found.
|
||||
*/
|
||||
private function components(): array {
|
||||
$components = array( 'buttons' );
|
||||
if ( $this->messages_apply->for_country() ) {
|
||||
$components[] = 'messages';
|
||||
}
|
||||
if ( $this->dcc_is_enabled() ) {
|
||||
$components[] = 'hosted-fields';
|
||||
}
|
||||
return $components;
|
||||
}
|
||||
|
||||
/**
|
||||
* The current context.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function context(): string {
|
||||
$context = 'mini-cart';
|
||||
if ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) {
|
||||
$context = 'product';
|
||||
}
|
||||
if ( is_cart() ) {
|
||||
$context = 'cart';
|
||||
}
|
||||
if ( is_checkout() && ! $this->session_handler->order() ) {
|
||||
$context = 'checkout';
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether DCC is enabled or not.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting has not been found.
|
||||
*/
|
||||
private function dcc_is_enabled(): bool {
|
||||
if ( ! $this->dcc_applies->for_country_currency() ) {
|
||||
return false;
|
||||
}
|
||||
$keys = array(
|
||||
'dcc_gateway_enabled' => 'is_checkout',
|
||||
);
|
||||
foreach ( $keys as $key => $callback ) {
|
||||
if ( $this->settings->has( $key ) && $this->settings->get( $key ) && $callback() ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the style for a given indicator in a given context.
|
||||
*
|
||||
* @param string $style The style.
|
||||
* @param string $context The context.
|
||||
*
|
||||
* @return string
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting hasn't been found.
|
||||
*/
|
||||
private function style_for_context( string $style, string $context ): string {
|
||||
$defaults = array(
|
||||
'layout' => 'vertical',
|
||||
'size' => 'responsive',
|
||||
'color' => 'gold',
|
||||
'shape' => 'pill',
|
||||
'label' => 'paypal',
|
||||
'tagline' => true,
|
||||
);
|
||||
|
||||
$value = isset( $defaults[ $style ] ) ?
|
||||
$defaults[ $style ] : '';
|
||||
$value = $this->settings->has( 'button_' . $style ) ?
|
||||
$this->settings->get( 'button_' . $style ) : $value;
|
||||
$value = $this->settings->has( 'button_' . $context . '_' . $style ) ?
|
||||
$this->settings->get( 'button_' . $context . '_' . $style ) : $value;
|
||||
|
||||
if ( is_bool( $value ) ) {
|
||||
$value = $value ? 'true' : 'false';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
/**
|
||||
* The interface for the smart button asset renderer.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Assets
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Assets;
|
||||
|
||||
/**
|
||||
* Interface SmartButtonInterface
|
||||
*/
|
||||
interface SmartButtonInterface {
|
||||
|
||||
/**
|
||||
* Renders the necessary HTML.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function render_wrapper(): bool;
|
||||
|
||||
/**
|
||||
* Enqueues the necessary scripts.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function enqueue(): bool;
|
||||
|
||||
/**
|
||||
* Whether the running installation could save vault tokens or not.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function can_save_vault_token(): bool;
|
||||
}
|
159
modules/ppcp-button/src/Endpoint/class-approveorderendpoint.php
Normal file
159
modules/ppcp-button/src/Endpoint/class-approveorderendpoint.php
Normal file
|
@ -0,0 +1,159 @@
|
|||
<?php
|
||||
/**
|
||||
* Endpoint to verify if an order has been approved. An approved order
|
||||
* will be stored in the current session.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Endpoint
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
|
||||
use Inpsyde\PayPalCommerce\Button\Helper\ThreeDSecure;
|
||||
use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
||||
|
||||
/**
|
||||
* Class ApproveOrderEndpoint
|
||||
*/
|
||||
class ApproveOrderEndpoint implements EndpointInterface {
|
||||
|
||||
|
||||
public const ENDPOINT = 'ppc-approve-order';
|
||||
|
||||
/**
|
||||
* The request data helper.
|
||||
*
|
||||
* @var RequestData
|
||||
*/
|
||||
private $request_data;
|
||||
|
||||
/**
|
||||
* The session handler.
|
||||
*
|
||||
* @var SessionHandler
|
||||
*/
|
||||
private $session_handler;
|
||||
|
||||
/**
|
||||
* The order endpoint.
|
||||
*
|
||||
* @var OrderEndpoint
|
||||
*/
|
||||
private $api_endpoint;
|
||||
|
||||
/**
|
||||
* The 3d secure helper object.
|
||||
*
|
||||
* @var ThreeDSecure
|
||||
*/
|
||||
private $threed_secure;
|
||||
|
||||
/**
|
||||
* ApproveOrderEndpoint constructor.
|
||||
*
|
||||
* @param RequestData $request_data The request data helper.
|
||||
* @param OrderEndpoint $order_endpoint The order endpoint.
|
||||
* @param SessionHandler $session_handler The session handler.
|
||||
* @param ThreeDSecure $three_d_secure The 3d secure helper object.
|
||||
*/
|
||||
public function __construct(
|
||||
RequestData $request_data,
|
||||
OrderEndpoint $order_endpoint,
|
||||
SessionHandler $session_handler,
|
||||
ThreeDSecure $three_d_secure
|
||||
) {
|
||||
|
||||
$this->request_data = $request_data;
|
||||
$this->api_endpoint = $order_endpoint;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->threed_secure = $three_d_secure;
|
||||
}
|
||||
|
||||
/**
|
||||
* The nonce.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function nonce(): string {
|
||||
return self::ENDPOINT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the request.
|
||||
*
|
||||
* @return bool
|
||||
* @throws RuntimeException When no order was found.
|
||||
*/
|
||||
public function handle_request(): bool {
|
||||
try {
|
||||
$data = $this->request_data->read_request( $this->nonce() );
|
||||
if ( ! isset( $data['order_id'] ) ) {
|
||||
throw new RuntimeException(
|
||||
__( 'No order id given', 'paypal-for-woocommerce' )
|
||||
);
|
||||
}
|
||||
|
||||
$order = $this->api_endpoint->order( $data['order_id'] );
|
||||
if ( ! $order ) {
|
||||
throw new RuntimeException(
|
||||
sprintf(
|
||||
// translators: %s is the id of the order.
|
||||
__( 'Order %s not found.', 'paypal-for-woocommerce' ),
|
||||
$data['order_id']
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( $order->payment_source() && $order->payment_source()->card() ) {
|
||||
$proceed = $this->threed_secure->proceed_with_order( $order );
|
||||
if ( ThreeDSecure::RETRY === $proceed ) {
|
||||
throw new RuntimeException(
|
||||
__(
|
||||
'Something went wrong. Please try again.',
|
||||
'paypal-for-woocommerce'
|
||||
)
|
||||
);
|
||||
}
|
||||
if ( ThreeDSecure::REJECT === $proceed ) {
|
||||
throw new RuntimeException(
|
||||
__(
|
||||
'Unfortunatly, we can\'t accept your card. Please choose a different payment method.',
|
||||
'paypal-for-woocommerce'
|
||||
)
|
||||
);
|
||||
}
|
||||
$this->session_handler->replace_order( $order );
|
||||
wp_send_json_success( $order );
|
||||
}
|
||||
|
||||
if ( ! $order->status()->is( OrderStatus::APPROVED ) ) {
|
||||
throw new RuntimeException(
|
||||
sprintf(
|
||||
// translators: %s is the id of the order.
|
||||
__( 'Order %s is not approved yet.', 'paypal-for-woocommerce' ),
|
||||
$data['order_id']
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$this->session_handler->replace_order( $order );
|
||||
wp_send_json_success( $order );
|
||||
return true;
|
||||
} catch ( \RuntimeException $error ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',
|
||||
'message' => $error->getMessage(),
|
||||
'code' => $error->getCode(),
|
||||
'details' => is_a( $error, PayPalApiException::class ) ? $error->details() : array(),
|
||||
)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
289
modules/ppcp-button/src/Endpoint/class-changecartendpoint.php
Normal file
289
modules/ppcp-button/src/Endpoint/class-changecartendpoint.php
Normal file
|
@ -0,0 +1,289 @@
|
|||
<?php
|
||||
/**
|
||||
* Endpoint to update the cart.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Endpoint
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
|
||||
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Class ChangeCartEndpoint
|
||||
*/
|
||||
class ChangeCartEndpoint implements EndpointInterface {
|
||||
|
||||
|
||||
public const ENDPOINT = 'ppc-change-cart';
|
||||
|
||||
/**
|
||||
* The current cart object.
|
||||
*
|
||||
* @var \WC_Cart
|
||||
*/
|
||||
private $cart;
|
||||
|
||||
/**
|
||||
* The current shipping object.
|
||||
*
|
||||
* @var \WC_Shipping
|
||||
*/
|
||||
private $shipping;
|
||||
|
||||
/**
|
||||
* The request data helper.
|
||||
*
|
||||
* @var RequestData
|
||||
*/
|
||||
private $request_data;
|
||||
|
||||
/**
|
||||
* Contains purchase units based off the current WC cart.
|
||||
*
|
||||
* @var CartRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* The product data store.
|
||||
*
|
||||
* @var \WC_Data_Store
|
||||
*/
|
||||
private $product_data_store;
|
||||
|
||||
/**
|
||||
* ChangeCartEndpoint constructor.
|
||||
*
|
||||
* @param \WC_Cart $cart The current WC cart object.
|
||||
* @param \WC_Shipping $shipping The current WC shipping object.
|
||||
* @param RequestData $request_data The request data helper.
|
||||
* @param CartRepository $repository The repository for the current purchase items.
|
||||
* @param \WC_Data_Store $product_data_store The data store for products.
|
||||
*/
|
||||
public function __construct(
|
||||
\WC_Cart $cart,
|
||||
\WC_Shipping $shipping,
|
||||
RequestData $request_data,
|
||||
CartRepository $repository,
|
||||
\WC_Data_Store $product_data_store
|
||||
) {
|
||||
|
||||
$this->cart = $cart;
|
||||
$this->shipping = $shipping;
|
||||
$this->request_data = $request_data;
|
||||
$this->repository = $repository;
|
||||
$this->product_data_store = $product_data_store;
|
||||
}
|
||||
|
||||
/**
|
||||
* The nonce.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function nonce(): string {
|
||||
return self::ENDPOINT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the request.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Exception On error.
|
||||
*/
|
||||
public function handle_request(): bool {
|
||||
try {
|
||||
return $this->handle_data();
|
||||
} catch ( RuntimeException $error ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',
|
||||
'message' => $error->getMessage(),
|
||||
'code' => $error->getCode(),
|
||||
'details' => is_a( $error, PayPalApiException::class ) ? $error->details() : array(),
|
||||
)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the request data.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Exception On error.
|
||||
*/
|
||||
private function handle_data(): bool {
|
||||
$data = $this->request_data->read_request( $this->nonce() );
|
||||
$products = $this->products_from_data( $data );
|
||||
if ( ! $products ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'name' => '',
|
||||
'message' => __(
|
||||
'Necessary fields not defined. Action aborted.',
|
||||
'paypal-for-woocommerce'
|
||||
),
|
||||
'code' => 0,
|
||||
'details' => array(),
|
||||
)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->shipping->reset_shipping();
|
||||
$this->cart->empty_cart( false );
|
||||
$success = true;
|
||||
foreach ( $products as $product ) {
|
||||
$success = $success && ( ! $product['product']->is_type( 'variable' ) ) ?
|
||||
$this->add_product( $product['product'], $product['quantity'] )
|
||||
: $this->add_variable_product(
|
||||
$product['product'],
|
||||
$product['quantity'],
|
||||
$product['variations']
|
||||
);
|
||||
}
|
||||
if ( ! $success ) {
|
||||
$this->handle_error();
|
||||
return $success;
|
||||
}
|
||||
|
||||
wp_send_json_success( $this->generate_purchase_units() );
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles errors.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function handle_error(): bool {
|
||||
|
||||
$message = __(
|
||||
'Something went wrong. Action aborted',
|
||||
'paypal-for-woocommerce'
|
||||
);
|
||||
$errors = wc_get_notices( 'error' );
|
||||
if ( count( $errors ) ) {
|
||||
$message = array_reduce(
|
||||
$errors,
|
||||
static function ( string $add, array $error ): string {
|
||||
return $add . $error['notice'] . ' ';
|
||||
},
|
||||
''
|
||||
);
|
||||
wc_clear_notices();
|
||||
}
|
||||
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'name' => '',
|
||||
'message' => $message,
|
||||
'code' => 0,
|
||||
'details' => array(),
|
||||
)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns product information from an data array.
|
||||
*
|
||||
* @param array $data The data array.
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
private function products_from_data( array $data ): ?array {
|
||||
|
||||
$products = array();
|
||||
|
||||
if (
|
||||
! isset( $data['products'] )
|
||||
|| ! is_array( $data['products'] )
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
foreach ( $data['products'] as $product ) {
|
||||
if ( ! isset( $product['quantity'] ) || ! isset( $product['id'] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$wc_product = wc_get_product( (int) $product['id'] );
|
||||
|
||||
if ( ! $wc_product ) {
|
||||
return null;
|
||||
}
|
||||
$products[] = array(
|
||||
'product' => $wc_product,
|
||||
'quantity' => (int) $product['quantity'],
|
||||
'variations' => isset( $product['variations'] ) ? $product['variations'] : null,
|
||||
);
|
||||
}
|
||||
return $products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a product to the cart.
|
||||
*
|
||||
* @param \WC_Product $product The Product.
|
||||
* @param int $quantity The Quantity.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Exception When product could not be added.
|
||||
*/
|
||||
private function add_product( \WC_Product $product, int $quantity ): bool {
|
||||
return false !== $this->cart->add_to_cart( $product->get_id(), $quantity );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds variations to the cart.
|
||||
*
|
||||
* @param \WC_Product $product The Product.
|
||||
* @param int $quantity The Quantity.
|
||||
* @param array $post_variations The variations.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Exception When product could not be added.
|
||||
*/
|
||||
private function add_variable_product(
|
||||
\WC_Product $product,
|
||||
int $quantity,
|
||||
array $post_variations
|
||||
): bool {
|
||||
|
||||
$variations = array();
|
||||
foreach ( $post_variations as $key => $value ) {
|
||||
$variations[ $value['name'] ] = $value['value'];
|
||||
}
|
||||
|
||||
$variation_id = $this->product_data_store->find_matching_product_variation( $product, $variations );
|
||||
|
||||
// ToDo: Check stock status for variation.
|
||||
return false !== $this->cart->add_to_cart(
|
||||
$product->get_id(),
|
||||
$quantity,
|
||||
$variation_id,
|
||||
$variations
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Based on the cart contents, the purchase units are created.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function generate_purchase_units(): array {
|
||||
return array_map(
|
||||
static function ( PurchaseUnit $line_item ): array {
|
||||
return $line_item->to_array();
|
||||
},
|
||||
$this->repository->all()
|
||||
);
|
||||
}
|
||||
}
|
243
modules/ppcp-button/src/Endpoint/class-createorderendpoint.php
Normal file
243
modules/ppcp-button/src/Endpoint/class-createorderendpoint.php
Normal file
|
@ -0,0 +1,243 @@
|
|||
<?php
|
||||
/**
|
||||
* The endpoint to create an PayPal order.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Endpoint
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\PaymentMethod;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Button\Helper\EarlyOrderHandler;
|
||||
use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
/**
|
||||
* Class CreateOrderEndpoint
|
||||
*/
|
||||
class CreateOrderEndpoint implements EndpointInterface {
|
||||
|
||||
|
||||
public const ENDPOINT = 'ppc-create-order';
|
||||
|
||||
/**
|
||||
* The request data helper.
|
||||
*
|
||||
* @var RequestData
|
||||
*/
|
||||
private $request_data;
|
||||
|
||||
/**
|
||||
* The cart repository.
|
||||
*
|
||||
* @var CartRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* The order endpoint.
|
||||
*
|
||||
* @var OrderEndpoint
|
||||
*/
|
||||
private $api_endpoint;
|
||||
|
||||
/**
|
||||
* The payer factory.
|
||||
*
|
||||
* @var PayerFactory
|
||||
*/
|
||||
private $payer_factory;
|
||||
|
||||
/**
|
||||
* The session handler.
|
||||
*
|
||||
* @var SessionHandler
|
||||
*/
|
||||
private $session_handler;
|
||||
|
||||
/**
|
||||
* The settings.
|
||||
*
|
||||
* @var Settings
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* The early order handler.
|
||||
*
|
||||
* @var EarlyOrderHandler
|
||||
*/
|
||||
private $early_order_handler;
|
||||
|
||||
/**
|
||||
* The current PayPal order in a process.
|
||||
*
|
||||
* @var Order|null
|
||||
*/
|
||||
private $order;
|
||||
|
||||
/**
|
||||
* CreateOrderEndpoint constructor.
|
||||
*
|
||||
* @param RequestData $request_data The RequestData object.
|
||||
* @param CartRepository $repository The CartRepository object.
|
||||
* @param OrderEndpoint $order_endpoint The OrderEndpoint object.
|
||||
* @param PayerFactory $payer_factory The PayerFactory object.
|
||||
* @param SessionHandler $session_handler The SessionHandler object.
|
||||
* @param Settings $settings The Settings object.
|
||||
* @param EarlyOrderHandler $early_order_handler The EarlyOrderHandler object.
|
||||
*/
|
||||
public function __construct(
|
||||
RequestData $request_data,
|
||||
CartRepository $repository,
|
||||
OrderEndpoint $order_endpoint,
|
||||
PayerFactory $payer_factory,
|
||||
SessionHandler $session_handler,
|
||||
Settings $settings,
|
||||
EarlyOrderHandler $early_order_handler
|
||||
) {
|
||||
|
||||
$this->request_data = $request_data;
|
||||
$this->repository = $repository;
|
||||
$this->api_endpoint = $order_endpoint;
|
||||
$this->payer_factory = $payer_factory;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->settings = $settings;
|
||||
$this->early_order_handler = $early_order_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the nonce.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function nonce(): string {
|
||||
return self::ENDPOINT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the request.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException In case a setting was not found.
|
||||
*/
|
||||
public function handle_request(): bool {
|
||||
try {
|
||||
$data = $this->request_data->read_request( $this->nonce() );
|
||||
$purchase_units = $this->repository->all();
|
||||
$payer = null;
|
||||
if ( isset( $data['payer'] ) && $data['payer'] ) {
|
||||
if ( isset( $data['payer']['phone']['phone_number']['national_number'] ) ) {
|
||||
// make sure the phone number contains only numbers and is max 14. chars long.
|
||||
$number = $data['payer']['phone']['phone_number']['national_number'];
|
||||
$number = preg_replace( '/[^0-9]/', '', $number );
|
||||
$number = substr( $number, 0, 14 );
|
||||
$data['payer']['phone']['phone_number']['national_number'] = $number;
|
||||
}
|
||||
$payer = $this->payer_factory->from_paypal_response( json_decode( wp_json_encode( $data['payer'] ) ) );
|
||||
}
|
||||
$bn_code = isset( $data['bn_code'] ) ? (string) $data['bn_code'] : '';
|
||||
if ( $bn_code ) {
|
||||
$this->session_handler->replace_bn_code( $bn_code );
|
||||
$this->api_endpoint->with_bn_code( $bn_code );
|
||||
}
|
||||
$payee_preferred = $this->settings->has( 'payee_preferred' )
|
||||
&& $this->settings->get( 'payee_preferred' ) ?
|
||||
PaymentMethod::PAYEE_PREFERRED_IMMEDIATE_PAYMENT_REQUIRED
|
||||
: PaymentMethod::PAYEE_PREFERRED_UNRESTRICTED;
|
||||
$payment_method = new PaymentMethod( $payee_preferred );
|
||||
$order = $this->api_endpoint->create(
|
||||
$purchase_units,
|
||||
$payer,
|
||||
null,
|
||||
$payment_method
|
||||
);
|
||||
if ( 'checkout' === $data['context'] ) {
|
||||
$this->validateForm( $data['form'], $order );
|
||||
}
|
||||
wp_send_json_success( $order->to_array() );
|
||||
return true;
|
||||
} catch ( \RuntimeException $error ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',
|
||||
'message' => $error->getMessage(),
|
||||
'code' => $error->getCode(),
|
||||
'details' => is_a( $error, PayPalApiException::class ) ? $error->details() : array(),
|
||||
)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the Request parameter and process the checkout form and validate it.
|
||||
*
|
||||
* @param string $form_values The values of the form.
|
||||
* @param Order $order The Order.
|
||||
*
|
||||
* @throws \Exception On Error.
|
||||
*/
|
||||
private function validateForm( string $form_values, Order $order ) {
|
||||
$this->order = $order;
|
||||
$parsed_values = wp_parse_args( $form_values );
|
||||
$_POST = $parsed_values;
|
||||
$_REQUEST = $parsed_values;
|
||||
|
||||
add_filter(
|
||||
'woocommerce_after_checkout_validation',
|
||||
array(
|
||||
$this,
|
||||
'after_checkout_validation',
|
||||
),
|
||||
10,
|
||||
2
|
||||
);
|
||||
$checkout = \WC()->checkout();
|
||||
$checkout->process_checkout();
|
||||
}
|
||||
|
||||
/**
|
||||
* Once the checkout has been validated we execute this method.
|
||||
*
|
||||
* @param array $data The data.
|
||||
* @param \WP_Error $errors The errors, which occurred.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function after_checkout_validation( array $data, \WP_Error $errors ): array {
|
||||
|
||||
$order = $this->order;
|
||||
if ( ! $errors->errors ) {
|
||||
|
||||
/**
|
||||
* In case we are onboarded and everything is fine with the \WC_Order
|
||||
* we want this order to be created. We will intercept it and leave it
|
||||
* in the "Pending payment" status though, which than later will change
|
||||
* during the "onApprove"-JS callback or the webhook listener.
|
||||
*/
|
||||
if ( ! $this->early_order_handler->should_create_early_order() ) {
|
||||
wp_send_json_success( $order->to_array() );
|
||||
}
|
||||
$this->early_order_handler->register_for_order( $order );
|
||||
return $data;
|
||||
}
|
||||
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'name' => '',
|
||||
'message' => $errors->get_error_message(),
|
||||
'code' => (int) $errors->get_error_code(),
|
||||
'details' => array(),
|
||||
)
|
||||
);
|
||||
return $data;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
/**
|
||||
* The Data Client ID endpoint.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Endpoint
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Class DataClientIdEndpoint
|
||||
*/
|
||||
class DataClientIdEndpoint implements EndpointInterface {
|
||||
|
||||
|
||||
public const ENDPOINT = 'ppc-data-client-id';
|
||||
|
||||
/**
|
||||
* The Request Data Helper.
|
||||
*
|
||||
* @var RequestData
|
||||
*/
|
||||
private $request_data;
|
||||
|
||||
/**
|
||||
* The Identity Token.
|
||||
*
|
||||
* @var IdentityToken
|
||||
*/
|
||||
private $identity_token;
|
||||
|
||||
/**
|
||||
* DataClientIdEndpoint constructor.
|
||||
*
|
||||
* @param RequestData $request_data The Request Data Helper.
|
||||
* @param IdentityToken $identity_token The Identity Token.
|
||||
*/
|
||||
public function __construct(
|
||||
RequestData $request_data,
|
||||
IdentityToken $identity_token
|
||||
) {
|
||||
|
||||
$this->request_data = $request_data;
|
||||
$this->identity_token = $identity_token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the nonce.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function nonce(): string {
|
||||
return self::ENDPOINT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function handle_request(): bool {
|
||||
try {
|
||||
$this->request_data->read_request( $this->nonce() );
|
||||
$user_id = get_current_user_id();
|
||||
$token = $this->identity_token->generate_for_customer( $user_id );
|
||||
wp_send_json(
|
||||
array(
|
||||
'token' => $token->token(),
|
||||
'expiration' => $token->expiration_timestamp(),
|
||||
'user' => get_current_user_id(),
|
||||
)
|
||||
);
|
||||
return true;
|
||||
} catch ( RuntimeException $error ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',
|
||||
'message' => $error->getMessage(),
|
||||
'code' => $error->getCode(),
|
||||
'details' => is_a( $error, PayPalApiException::class ) ? $error->details() : array(),
|
||||
)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
30
modules/ppcp-button/src/Endpoint/class-endpointinterface.php
Normal file
30
modules/ppcp-button/src/Endpoint/class-endpointinterface.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
/**
|
||||
* The Endpoint interface.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Endpoint
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
|
||||
|
||||
/**
|
||||
* Interface EndpointInterface
|
||||
*/
|
||||
interface EndpointInterface {
|
||||
|
||||
/**
|
||||
* Returns the nonce for an endpoint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function nonce(): string;
|
||||
|
||||
/**
|
||||
* Handles the request for an endpoint.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function handle_request(): bool;
|
||||
}
|
91
modules/ppcp-button/src/Endpoint/class-requestdata.php
Normal file
91
modules/ppcp-button/src/Endpoint/class-requestdata.php
Normal file
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
/**
|
||||
* Helper to read request data for the endpoints.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Endpoint
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
|
||||
|
||||
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Class RequestData
|
||||
*/
|
||||
class RequestData {
|
||||
|
||||
/**
|
||||
* Enqueues the nonce fix hook.
|
||||
*/
|
||||
public function enqueue_nonce_fix() {
|
||||
add_filter( 'nonce_user_logged_out', array( $this, 'nonce_fix' ), 100 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Dequeues the nonce fix hook.
|
||||
*/
|
||||
public function dequeue_nonce_fix() {
|
||||
remove_filter( 'nonce_user_logged_out', array( $this, 'nonce_fix' ), 100 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the current request.
|
||||
*
|
||||
* @param string $nonce The nonce.
|
||||
*
|
||||
* @return array
|
||||
* @throws RuntimeException When nonce validation fails.
|
||||
*/
|
||||
public function read_request( string $nonce ): array {
|
||||
$stream = file_get_contents( 'php://input' );
|
||||
$json = json_decode( $stream, true );
|
||||
$this->enqueue_nonce_fix();
|
||||
if (
|
||||
! isset( $json['nonce'] )
|
||||
|| ! wp_verify_nonce( $json['nonce'], $nonce )
|
||||
) {
|
||||
remove_filter( 'nonce_user_logged_out', array( $this, 'nonce_fix' ), 100 );
|
||||
throw new RuntimeException(
|
||||
__( 'Could not validate nonce.', 'paypal-for-woocommerce' )
|
||||
);
|
||||
}
|
||||
$this->dequeue_nonce_fix();
|
||||
|
||||
$sanitized = $this->sanitize( $json );
|
||||
return $sanitized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Woocommerce will give you a customer object on your 2nd request. the first page
|
||||
* load will not yet have this customer object, but the ajax request will. Therefore
|
||||
* the nonce validation will fail. this fixes this problem:
|
||||
*
|
||||
* @wp-hook nonce_user_logged_out
|
||||
* @see https://github.com/woocommerce/woocommerce/blob/69e3835041113bee80379c1037e97e26815a699b/includes/class-wc-session-handler.php#L288-L296
|
||||
* @return int
|
||||
*/
|
||||
public function nonce_fix(): int {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes the data.
|
||||
*
|
||||
* @param array $assoc_array The data array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function sanitize( array $assoc_array ): array {
|
||||
$data = array();
|
||||
foreach ( (array) $assoc_array as $raw_key => $raw_value ) {
|
||||
if ( ! is_array( $raw_value ) ) {
|
||||
$data[ sanitize_text_field( urldecode( (string) $raw_key ) ) ] = sanitize_text_field( urldecode( (string) $raw_value ) );
|
||||
continue;
|
||||
}
|
||||
$data[ sanitize_text_field( urldecode( (string) $raw_key ) ) ] = $this->sanitize( $raw_value );
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
18
modules/ppcp-button/src/Exception/class-runtimeexception.php
Normal file
18
modules/ppcp-button/src/Exception/class-runtimeexception.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
/**
|
||||
* The modules Runtime Exception.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Exception
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Exception;
|
||||
|
||||
/**
|
||||
* Class RuntimeException
|
||||
*/
|
||||
class RuntimeException extends \RuntimeException {
|
||||
|
||||
|
||||
}
|
174
modules/ppcp-button/src/Helper/class-earlyorderhandler.php
Normal file
174
modules/ppcp-button/src/Helper/class-earlyorderhandler.php
Normal file
|
@ -0,0 +1,174 @@
|
|||
<?php
|
||||
/**
|
||||
* Handles the Early Order logic, when we need to create the WC_Order by ourselfs.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Helper
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Helper;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use Inpsyde\PayPalCommerce\Onboarding\State;
|
||||
use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor;
|
||||
use Inpsyde\PayPalCommerce\Webhooks\Handler\PrefixTrait;
|
||||
|
||||
/**
|
||||
* Class EarlyOrderHandler
|
||||
*/
|
||||
class EarlyOrderHandler {
|
||||
|
||||
use PrefixTrait;
|
||||
|
||||
/**
|
||||
* The State.
|
||||
*
|
||||
* @var State
|
||||
*/
|
||||
private $state;
|
||||
|
||||
/**
|
||||
* The Order Processor.
|
||||
*
|
||||
* @var OrderProcessor
|
||||
*/
|
||||
private $order_processor;
|
||||
|
||||
/**
|
||||
* The Session Handler.
|
||||
*
|
||||
* @var SessionHandler
|
||||
*/
|
||||
private $session_handler;
|
||||
|
||||
/**
|
||||
* EarlyOrderHandler constructor.
|
||||
*
|
||||
* @param State $state The State.
|
||||
* @param OrderProcessor $order_processor The Order Processor.
|
||||
* @param SessionHandler $session_handler The Session Handler.
|
||||
* @param string $prefix The Prefix.
|
||||
*/
|
||||
public function __construct(
|
||||
State $state,
|
||||
OrderProcessor $order_processor,
|
||||
SessionHandler $session_handler,
|
||||
string $prefix
|
||||
) {
|
||||
|
||||
$this->state = $state;
|
||||
$this->order_processor = $order_processor;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether early orders should be created at all.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function should_create_early_order(): bool {
|
||||
return $this->state->current_state() === State::STATE_ONBOARDED;
|
||||
}
|
||||
|
||||
//phpcs:disable WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
/**
|
||||
* Tries to determine the current WC Order Id based on the PayPal order
|
||||
* and the current order in session.
|
||||
*
|
||||
* @param int|null $value The initial value.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function determine_wc_order_id( int $value = null ): ?int {
|
||||
|
||||
if ( ! isset( $_REQUEST['ppcp-resume-order'] ) ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$resume_order_id = (int) WC()->session->get( 'order_awaiting_payment' );
|
||||
|
||||
$order = $this->session_handler->order();
|
||||
if ( ! $order ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$order_id = false;
|
||||
foreach ( $order->purchase_units() as $purchase_unit ) {
|
||||
if ( $purchase_unit->custom_id() === sanitize_text_field( wp_unslash( $_REQUEST['ppcp-resume-order'] ) ) ) {
|
||||
$order_id = (int) $this->sanitize_custom_id( $purchase_unit->custom_id() );
|
||||
}
|
||||
}
|
||||
if ( $order_id === $resume_order_id ) {
|
||||
$value = $order_id;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
//phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
/**
|
||||
* Registers the necessary checkout actions for a given order.
|
||||
*
|
||||
* @param Order $order The PayPal order.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function register_for_order( Order $order ): bool {
|
||||
|
||||
$success = (bool) add_action(
|
||||
'woocommerce_checkout_order_processed',
|
||||
function ( $order_id ) use ( $order ) {
|
||||
try {
|
||||
$order = $this->configure_session_and_order( (int) $order_id, $order );
|
||||
wp_send_json_success( $order->to_array() );
|
||||
} catch ( \RuntimeException $error ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'name' => is_a( $error, PayPalApiException::class ) ?
|
||||
$error->name() : '',
|
||||
'message' => $error->getMessage(),
|
||||
'code' => $error->getCode(),
|
||||
'details' => is_a( $error, PayPalApiException::class ) ?
|
||||
$error->details() : array(),
|
||||
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the session, so we can pick up the order, once we pass through the checkout.
|
||||
*
|
||||
* @param int $order_id The Woocommerce order id.
|
||||
* @param Order $order The PayPal order.
|
||||
*
|
||||
* @return Order
|
||||
*/
|
||||
public function configure_session_and_order( int $order_id, Order $order ): Order {
|
||||
|
||||
/**
|
||||
* Set the order id in our session in order for
|
||||
* us to resume this order in checkout.
|
||||
*/
|
||||
WC()->session->set( 'order_awaiting_payment', $order_id );
|
||||
|
||||
$wc_order = wc_get_order( $order_id );
|
||||
$wc_order->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $order->id() );
|
||||
$wc_order->update_meta_data( PayPalGateway::INTENT_META_KEY, $order->intent() );
|
||||
$wc_order->save_meta_data();
|
||||
|
||||
/**
|
||||
* Patch Order so we have the \WC_Order id added.
|
||||
*/
|
||||
return $this->order_processor->patch_order( $wc_order, $order );
|
||||
}
|
||||
}
|
37
modules/ppcp-button/src/Helper/class-messagesapply.php
Normal file
37
modules/ppcp-button/src/Helper/class-messagesapply.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
/**
|
||||
* Helper class to determine if credit messaging should be displayed.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Helper
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Helper;
|
||||
|
||||
/**
|
||||
* Class MessagesApply
|
||||
*/
|
||||
class MessagesApply {
|
||||
|
||||
|
||||
/**
|
||||
* In which countries credit messaging is available.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $countries = array(
|
||||
'US',
|
||||
);
|
||||
|
||||
/**
|
||||
* Determines whether a credit messaging is enabled for the shops location country.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function for_country(): bool {
|
||||
$region = wc_get_base_location();
|
||||
$country = $region['country'];
|
||||
return in_array( $country, $this->countries, true );
|
||||
}
|
||||
}
|
104
modules/ppcp-button/src/Helper/class-threedsecure.php
Normal file
104
modules/ppcp-button/src/Helper/class-threedsecure.php
Normal file
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
/**
|
||||
* Helper class to determine how to proceed with an order depending on the 3d secure feedback.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button\Helper
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Helper;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\CardAuthenticationResult as AuthResult;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
|
||||
|
||||
/**
|
||||
* Class ThreeDSecure
|
||||
*/
|
||||
class ThreeDSecure {
|
||||
|
||||
|
||||
public const NO_DECISION = 0;
|
||||
public const PROCCEED = 1;
|
||||
public const REJECT = 2;
|
||||
public const RETRY = 3;
|
||||
|
||||
/**
|
||||
* Determine, how we proceed with a given order.
|
||||
*
|
||||
* @link https://developer.paypal.com/docs/business/checkout/add-capabilities/3d-secure/#authenticationresult
|
||||
*
|
||||
* @param Order $order The order for which the decission is needed.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function proceed_with_order( Order $order ): int {
|
||||
if ( ! $order->payment_source() ) {
|
||||
return self::NO_DECISION;
|
||||
}
|
||||
if ( ! $order->payment_source()->card() ) {
|
||||
return self::NO_DECISION;
|
||||
}
|
||||
if ( ! $order->payment_source()->card()->authentication_result() ) {
|
||||
return self::NO_DECISION;
|
||||
}
|
||||
$result = $order->payment_source()->card()->authentication_result();
|
||||
if ( $result->liability_shift() === AuthResult::LIABILITY_SHIFT_POSSIBLE ) {
|
||||
return self::PROCCEED;
|
||||
}
|
||||
|
||||
if ( $result->liability_shift() === AuthResult::LIABILITY_SHIFT_UNKNOWN ) {
|
||||
return self::RETRY;
|
||||
}
|
||||
if ( $result->liability_shift() === AuthResult::LIABILITY_SHIFT_NO ) {
|
||||
return $this->no_liability_shift( $result );
|
||||
}
|
||||
return self::NO_DECISION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines how to proceed depending on the Liability Shift.
|
||||
*
|
||||
* @param AuthResult $result The AuthResult object based on which we make the decision.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function no_liability_shift( AuthResult $result ): int {
|
||||
|
||||
if (
|
||||
$result->enrollment_status() === AuthResult::ENROLLMENT_STATUS_BYPASS
|
||||
&& ! $result->authentication_result()
|
||||
) {
|
||||
return self::PROCCEED;
|
||||
}
|
||||
if (
|
||||
$result->enrollment_status() === AuthResult::ENROLLMENT_STATUS_UNAVAILABLE
|
||||
&& ! $result->authentication_result()
|
||||
) {
|
||||
return self::PROCCEED;
|
||||
}
|
||||
if (
|
||||
$result->enrollment_status() === AuthResult::ENROLLMENT_STATUS_NO
|
||||
&& ! $result->authentication_result()
|
||||
) {
|
||||
return self::PROCCEED;
|
||||
}
|
||||
|
||||
if ( $result->authentication_result() === AuthResult::AUTHENTICATION_RESULT_REJECTED ) {
|
||||
return self::REJECT;
|
||||
}
|
||||
|
||||
if ( $result->authentication_result() === AuthResult::AUTHENTICATION_RESULT_NO ) {
|
||||
return self::REJECT;
|
||||
}
|
||||
|
||||
if ( $result->authentication_result() === AuthResult::AUTHENTICATION_RESULT_UNABLE ) {
|
||||
return self::RETRY;
|
||||
}
|
||||
|
||||
if ( ! $result->authentication_result() ) {
|
||||
return self::RETRY;
|
||||
}
|
||||
return self::NO_DECISION;
|
||||
}
|
||||
}
|
154
modules/ppcp-button/src/class-buttonmodule.php
Normal file
154
modules/ppcp-button/src/class-buttonmodule.php
Normal file
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
/**
|
||||
* The button module.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Button
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button;
|
||||
|
||||
use Dhii\Container\ServiceProvider;
|
||||
use Dhii\Modular\Module\ModuleInterface;
|
||||
use Inpsyde\PayPalCommerce\Button\Assets\SmartButtonInterface;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\DataClientIdEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Button\Helper\EarlyOrderHandler;
|
||||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Class ButtonModule
|
||||
*/
|
||||
class ButtonModule implements ModuleInterface {
|
||||
|
||||
|
||||
/**
|
||||
* Sets up the module.
|
||||
*
|
||||
* @return ServiceProviderInterface
|
||||
*/
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider(
|
||||
require __DIR__ . '/../services.php',
|
||||
require __DIR__ . '/../extensions.php'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the module.
|
||||
*
|
||||
* @param ContainerInterface $container The Container.
|
||||
*/
|
||||
public function run( ContainerInterface $container ) {
|
||||
|
||||
add_action(
|
||||
'wp',
|
||||
static function () use ( $container ) {
|
||||
if ( is_admin() ) {
|
||||
return;
|
||||
}
|
||||
$smart_button = $container->get( 'button.smart-button' );
|
||||
/**
|
||||
* The Smart Button.
|
||||
*
|
||||
* @var SmartButtonInterface $smart_button
|
||||
*/
|
||||
$smart_button->render_wrapper();
|
||||
}
|
||||
);
|
||||
add_action(
|
||||
'wp_enqueue_scripts',
|
||||
static function () use ( $container ) {
|
||||
|
||||
$smart_button = $container->get( 'button.smart-button' );
|
||||
/**
|
||||
* The Smart Button.
|
||||
*
|
||||
* @var SmartButtonInterface $smart_button
|
||||
*/
|
||||
$smart_button->enqueue();
|
||||
}
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'woocommerce_create_order',
|
||||
static function ( $value ) use ( $container ) {
|
||||
$early_order_handler = $container->get( 'button.helper.early-order-handler' );
|
||||
if ( ! is_null( $value ) ) {
|
||||
$value = (int) $value;
|
||||
}
|
||||
/**
|
||||
* The Early Order Handler
|
||||
*
|
||||
* @var EarlyOrderHandler $early_order_handler
|
||||
*/
|
||||
return $early_order_handler->determine_wc_order_id( $value );
|
||||
}
|
||||
);
|
||||
|
||||
$this->register_ajax_endpoints( $container );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the Ajax Endpoints.
|
||||
*
|
||||
* @param ContainerInterface $container The Container.
|
||||
*/
|
||||
private function register_ajax_endpoints( ContainerInterface $container ) {
|
||||
add_action(
|
||||
'wc_ajax_' . DataClientIdEndpoint::ENDPOINT,
|
||||
static function () use ( $container ) {
|
||||
$endpoint = $container->get( 'button.endpoint.data-client-id' );
|
||||
/**
|
||||
* The Data Client ID Endpoint.
|
||||
*
|
||||
* @var DataClientIdEndpoint $endpoint
|
||||
*/
|
||||
$endpoint->handle_request();
|
||||
}
|
||||
);
|
||||
|
||||
add_action(
|
||||
'wc_ajax_' . ChangeCartEndpoint::ENDPOINT,
|
||||
static function () use ( $container ) {
|
||||
$endpoint = $container->get( 'button.endpoint.change-cart' );
|
||||
/**
|
||||
* The Change Cart Endpoint.
|
||||
*
|
||||
* @var ChangeCartEndpoint $endpoint
|
||||
*/
|
||||
$endpoint->handle_request();
|
||||
}
|
||||
);
|
||||
|
||||
add_action(
|
||||
'wc_ajax_' . ApproveOrderEndpoint::ENDPOINT,
|
||||
static function () use ( $container ) {
|
||||
$endpoint = $container->get( 'button.endpoint.approve-order' );
|
||||
/**
|
||||
* The Approve Order Endpoint.
|
||||
*
|
||||
* @var ApproveOrderEndpoint $endpoint
|
||||
*/
|
||||
$endpoint->handle_request();
|
||||
}
|
||||
);
|
||||
|
||||
add_action(
|
||||
'wc_ajax_' . CreateOrderEndpoint::ENDPOINT,
|
||||
static function () use ( $container ) {
|
||||
$endpoint = $container->get( 'button.endpoint.create-order' );
|
||||
/**
|
||||
* The Create Order Endpoint.
|
||||
*
|
||||
* @var CreateOrderEndpoint $endpoint
|
||||
*/
|
||||
$endpoint->handle_request();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue