woocommerce-paypal-payments/modules.local/ppcp-button/src/Assets/SmartButton.php

398 lines
14 KiB
PHP
Raw Normal View History

2020-04-02 08:38:00 +03:00
<?php
2020-04-28 12:31:12 +03:00
2020-04-02 08:38:00 +03:00
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Assets;
2020-04-30 15:28:48 +03:00
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
2020-04-13 09:07:20 +03:00
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
2020-04-02 08:38:00 +03:00
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
2020-07-10 12:33:13 +03:00
use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
2020-04-02 08:38:00 +03:00
class SmartButton implements SmartButtonInterface
2020-04-02 08:38:00 +03:00
{
private $moduleUrl;
private $sessionHandler;
private $settings;
2020-04-13 09:07:20 +03:00
private $payeeRepository;
2020-04-30 15:28:48 +03:00
private $identityToken;
private $payerFactory;
private $clientId;
2020-07-10 12:33:13 +03:00
private $requestData;
2020-04-02 08:38:00 +03:00
public function __construct(
string $moduleUrl,
SessionHandler $sessionHandler,
2020-04-13 09:07:20 +03:00
Settings $settings,
2020-04-30 15:28:48 +03:00
PayeeRepository $payeeRepository,
IdentityToken $identityToken,
PayerFactory $payerFactory,
2020-07-10 12:33:13 +03:00
string $clientId,
RequestData $requestData
2020-04-02 08:38:00 +03:00
) {
2020-04-06 11:16:18 +03:00
2020-04-02 08:38:00 +03:00
$this->moduleUrl = $moduleUrl;
$this->sessionHandler = $sessionHandler;
$this->settings = $settings;
2020-04-13 09:07:20 +03:00
$this->payeeRepository = $payeeRepository;
2020-04-30 15:28:48 +03:00
$this->identityToken = $identityToken;
$this->payerFactory = $payerFactory;
$this->clientId = $clientId;
2020-07-10 12:33:13 +03:00
$this->requestData = $requestData;
2020-04-02 08:38:00 +03:00
}
// phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
2020-07-10 12:56:01 +03:00
// phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
/**
* ToDo: Refactor
* @return bool
*/
public function renderWrapper(): bool
2020-04-02 08:38:00 +03:00
{
2020-04-30 15:28:48 +03:00
$buttonRenderer = static function () {
$product = wc_get_product();
if (
! is_checkout() && is_a($product, \WC_Product::class)
&& (
$product->is_type(['external', 'grouped'])
|| ! $product->is_in_stock()
)
) {
return;
}
2020-04-02 08:38:00 +03:00
echo '<div id="ppc-button"></div>';
};
$canRenderDcc = $this->settings->has('client_id') && $this->settings->get('client_id');
$dccRenderer = static function (string $id = null) use ($canRenderDcc) {
if (! $canRenderDcc) {
return;
}
$product = wc_get_product();
if (
! is_checkout() && is_a($product, \WC_Product::class)
&& (
$product->is_type(['external', 'grouped'])
|| ! $product->is_in_stock()
)
) {
return;
}
if (!$id) {
$id = 'ppcp-hosted-fields';
}
2020-04-30 15:28:48 +03:00
printf(
2020-04-30 16:58:00 +03:00
'<form id="%1$s">
<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>
<button>%5$s</button>
</form><div id="payments-sdk__contingency-lightbox"></div>',
esc_attr($id),
esc_html__('Card number', 'woocommerce-paypal-commerce-gateway'),
esc_html__('Expiration Date', 'woocommerce-paypal-commerce-gateway'),
esc_html__('CVV', 'woocommerce-paypal-commerce-gateway'),
esc_html__('Pay with Card', 'woocommerce-paypal-commerce-gateway')
2020-04-30 15:28:48 +03:00
);
2020-04-02 08:38:00 +03:00
};
$notEnabledOnCart = $this->settings->has('button_cart_enabled') &&
!$this->settings->get('button_cart_enabled');
2020-06-15 11:48:37 +03:00
if (
is_cart()
&& !$notEnabledOnCart
2020-06-15 11:48:37 +03:00
) {
2020-04-08 12:33:34 +03:00
add_action(
'woocommerce_proceed_to_checkout',
$buttonRenderer,
20
);
}
2020-06-15 11:48:37 +03:00
if (
is_cart()
&& $this->settings->has('dcc_cart_enabled')
&& $this->settings->get('dcc_cart_enabled')
2020-06-15 11:48:37 +03:00
) {
add_action(
'woocommerce_proceed_to_checkout',
$dccRenderer,
2020-04-08 12:33:34 +03:00
20
);
}
$notEnabledOnProductPage = $this->settings->has('button_single_product_enabled') &&
!$this->settings->get('button_single_product_enabled');
2020-06-15 11:48:37 +03:00
if (
(is_product() || wc_post_content_has_shortcode('product_page'))
&& !$notEnabledOnProductPage
2020-06-15 11:48:37 +03:00
) {
add_action(
'woocommerce_single_product_summary',
$buttonRenderer,
31
);
}
2020-06-15 11:48:37 +03:00
if (
(is_product() || wc_post_content_has_shortcode('product_page'))
2020-06-15 11:48:37 +03:00
&& $this->settings->has('dcc_single_product_enabled')
&& $this->settings->get('dcc_single_product_enabled')
2020-06-15 11:48:37 +03:00
) {
add_action(
'woocommerce_single_product_summary',
$dccRenderer,
31
);
2020-04-02 08:38:00 +03:00
}
$notEnabledOnMiniCart = $this->settings->has('button_mini_cart_enabled') &&
!$this->settings->get('button_mini_cart_enabled');
2020-06-15 11:48:37 +03:00
if (
! $notEnabledOnMiniCart
2020-06-15 11:48:37 +03:00
) {
add_action(
'woocommerce_widget_shopping_cart_after_buttons',
2020-04-28 12:31:12 +03:00
static function () {
echo '<p id="ppc-button-minicart" class="woocommerce-mini-cart__buttons buttons"></p>';
},
30
);
}
2020-06-15 11:48:37 +03:00
if (
$this->settings->has('dcc_mini_cart_enabled')
&& $this->settings->get('dcc_mini_cart_enabled')
2020-06-15 11:48:37 +03:00
) {
add_action(
'woocommerce_widget_shopping_cart_after_buttons',
static function () use ($dccRenderer) {
2020-04-30 16:58:00 +03:00
$dccRenderer('ppcp-hosted-fields-mini-cart');
},
31
);
}
add_action(
'woocommerce_review_order_after_submit',
$buttonRenderer,
10
);
2020-06-15 11:48:37 +03:00
if (
$this->settings->has('dcc_checkout_enabled')
&& $this->settings->get('dcc_checkout_enabled')
&& ! $this->sessionHandler->order()
2020-06-15 11:48:37 +03:00
) {
add_action(
'woocommerce_review_order_after_submit',
$dccRenderer,
11
);
}
2020-04-08 12:33:34 +03:00
return true;
2020-04-02 08:38:00 +03:00
}
// phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong
2020-07-10 12:56:01 +03:00
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
2020-04-02 08:38:00 +03:00
2020-04-09 18:15:57 +03:00
public function enqueue(): bool
2020-04-02 08:38:00 +03:00
{
wp_enqueue_script(
2020-06-15 11:48:37 +03:00
'ppcp-smart-button',
2020-04-28 12:31:12 +03:00
$this->moduleUrl . '/assets/js/button.js',
['jquery'],
1,
true
2020-04-02 08:38:00 +03:00
);
2020-04-13 09:07:20 +03:00
wp_localize_script(
2020-06-15 11:48:37 +03:00
'ppcp-smart-button',
2020-04-13 09:07:20 +03:00
'PayPalCommerceGateway',
$this->localizeScript()
);
return true;
}
2020-04-02 08:38:00 +03:00
2020-04-28 12:31:12 +03:00
private function localizeScript(): array
2020-04-13 09:07:20 +03:00
{
2020-07-10 12:33:13 +03:00
$this->requestData->enqueueNonceFix();
2020-04-02 08:38:00 +03:00
$localize = [
2020-04-30 15:28:48 +03:00
'script_attributes' => $this->attributes(),
2020-04-02 08:38:00 +03:00
'redirect' => wc_get_checkout_url(),
'context' => $this->context(),
'ajax' => [
'change_cart' => [
2020-04-06 11:16:18 +03:00
'endpoint' => home_url(\WC_AJAX::get_endpoint(ChangeCartEndpoint::ENDPOINT)),
2020-04-02 08:38:00 +03:00
'nonce' => wp_create_nonce(ChangeCartEndpoint::nonce()),
],
'create_order' => [
2020-04-06 11:16:18 +03:00
'endpoint' => home_url(\WC_AJAX::get_endpoint(CreateOrderEndpoint::ENDPOINT)),
2020-04-02 08:38:00 +03:00
'nonce' => wp_create_nonce(CreateOrderEndpoint::nonce()),
],
'approve_order' => [
'endpoint' => home_url(\WC_AJAX::get_endpoint(ApproveOrderEndpoint::ENDPOINT)),
'nonce' => wp_create_nonce(ApproveOrderEndpoint::nonce()),
],
2020-04-02 08:38:00 +03:00
],
2020-07-15 19:49:32 +03:00
'bn_codes' => $this->bnCodes(),
'payer' => $this->payerData(),
2020-04-02 08:38:00 +03:00
'button' => [
'wrapper' => '#ppc-button',
2020-04-08 12:33:34 +03:00
'mini_cart_wrapper' => '#ppc-button-minicart',
'cancel_wrapper' => '#ppcp-cancel',
2020-04-13 09:07:20 +03:00
'url' => $this->url(),
'style' => [
'layout' => 'vertical',
2020-07-10 12:56:01 +03:00
'color' => ($this->settings->has('button_color')) ?
$this->settings->get('button_color') : null,
'shape' => ($this->settings->has('button_shape')) ?
$this->settings->get('button_shape') : null,
'label' => ($this->settings->has('button_label')) ?
$this->settings->get('button_label') : 'paypal',
],
2020-04-06 11:16:18 +03:00
],
2020-04-30 15:28:48 +03:00
'hosted_fields' => [
'wrapper' => '#ppcp-hosted-fields',
'mini_cart_wrapper' => '#ppcp-hosted-fields-mini-cart',
2020-04-30 15:28:48 +03:00
'labels' => [
'credit_card_number' => __('Credit Card Number', 'woocommerce-paypal-commerce-gateway'),
'cvv' => __('CVV', 'woocommerce-paypal-commerce-gateway'),
'mm_yyyy' => __('MM/YYYY', 'woocommerce-paypal-commerce-gateway'),
],
],
2020-04-02 08:38:00 +03:00
];
2020-07-10 12:33:13 +03:00
$this->requestData->dequeueNonceFix();
2020-04-13 09:07:20 +03:00
return $localize;
}
2020-04-28 12:31:12 +03:00
private function payerData(): ?array
{
$customer = WC()->customer;
if (! is_user_logged_in() || ! is_a($customer, \WC_Customer::class)) {
return null;
}
return $this->payerFactory->fromCustomer($customer)->toArray();
}
2020-04-28 12:31:12 +03:00
private function url(): string
2020-04-13 09:07:20 +03:00
{
$params = [
'client-id' => $this->clientId,
2020-04-13 09:07:20 +03:00
'currency' => get_woocommerce_currency(),
'locale' => get_user_locale(),
//'debug' => (defined('WP_DEBUG') && WP_DEBUG) ? 'true' : 'false',
//ToDo: Update date on releases.
'integration-date' => date('Y-m-d'),
2020-04-30 15:28:48 +03:00
'components' => implode(',', $this->components()),
2020-04-13 09:07:20 +03:00
//ToDo: Probably only needed, when DCC
'vault' => is_user_logged_in() ? 'true' : 'false',
2020-04-13 09:07:20 +03:00
'commit' => is_checkout() ? 'true' : 'false',
2020-06-29 12:16:06 +03:00
'intent' => ($this->settings->has('intent')) ? $this->settings->get('intent') : 'capture',
2020-04-13 09:07:20 +03:00
];
2020-04-30 15:28:48 +03:00
if (defined('WP_DEBUG') && \WP_DEBUG && WC()->customer) {
$params['buyer-country'] = WC()->customer->get_billing_country();
}
2020-04-13 09:07:20 +03:00
$payee = $this->payeeRepository->payee();
if ($payee->merchantId()) {
$params['merchant-id'] = $payee->merchantId();
}
2020-06-29 13:35:37 +03:00
$disableFunding = $this->settings->has('disable_funding') ?
$this->settings->get('disable_funding') : [];
2020-04-16 09:29:02 +03:00
if (is_array($disableFunding) && count($disableFunding)) {
$params['disable-funding'] = implode(',', $disableFunding);
}
2020-04-13 09:07:20 +03:00
$smartButtonUrl = add_query_arg($params, 'https://www.paypal.com/sdk/js');
return $smartButtonUrl;
2020-04-02 08:38:00 +03:00
}
2020-06-29 13:35:37 +03:00
private function attributes(): array
{
2020-07-15 19:49:32 +03:00
$attributes = [
'data-partner-attribution-id' => $this->bnCodeForContext($this->context()),
];
2020-04-30 15:28:48 +03:00
try {
if (! is_user_logged_in()) {
return $attributes;
}
$clientToken = $this->identityToken->generateForCustomer((int) get_current_user_id());
2020-04-30 15:28:48 +03:00
$attributes['data-client-token'] = $clientToken->token();
return $attributes;
} catch (RuntimeException $exception) {
return $attributes;
}
}
2020-07-15 19:49:32 +03:00
/**
* @param string $context
* @return string
*/
private function bnCodeForContext(string $context): string
{
$codes = $this->bnCodes();
return (isset($codes[$context])) ? $codes[$context] : '';
}
/**
* BN Codes
*
* @return array
*/
private function bnCodes(): array
{
return [
'checkout' => 'Woo_PPCP',
'cart' => 'Woo_PPCP',
'mini-cart' => 'Woo_PPCP',
'product' => 'Woo_PPCP',
2020-07-15 19:49:32 +03:00
];
}
2020-06-29 13:35:37 +03:00
private function components(): array
2020-04-30 15:28:48 +03:00
{
$components = ['buttons'];
if ($this->dccIsEnabled()) {
$components[] = 'hosted-fields';
}
return $components;
}
2020-04-09 18:15:57 +03:00
private function context(): string
2020-04-06 11:16:18 +03:00
{
2020-04-02 08:38:00 +03:00
$context = 'mini-cart';
if (is_product() || wc_post_content_has_shortcode('product_page')) {
2020-04-02 08:38:00 +03:00
$context = 'product';
}
if (is_cart()) {
$context = 'cart';
}
2020-04-09 18:15:57 +03:00
if (is_checkout() && !$this->sessionHandler->order()) {
2020-04-02 08:38:00 +03:00
$context = 'checkout';
}
return $context;
}
2020-04-30 15:28:48 +03:00
2020-06-29 13:35:37 +03:00
private function dccIsEnabled(): bool
2020-04-30 15:28:48 +03:00
{
2020-06-15 11:48:37 +03:00
$keys = [
'dcc_cart_enabled',
'dcc_mini_cart_enabled',
'dcc_checkout_enabled',
'dcc_single_product_enabled',
];
foreach ($keys as $key) {
if ($this->settings->has($key) && $this->settings->get($key)) {
2020-06-15 11:48:37 +03:00
return true;
}
}
return false;
2020-04-30 15:28:48 +03:00
}
2020-04-06 11:16:18 +03:00
}