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;
|
2020-05-07 12:43:46 +03:00
|
|
|
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
2020-04-13 09:07:20 +03:00
|
|
|
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
|
2020-04-08 16:23:33 +03:00
|
|
|
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-04-09 09:33:57 +03:00
|
|
|
use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
2020-04-09 14:34:45 +03:00
|
|
|
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
2020-04-02 08:38:00 +03:00
|
|
|
|
2020-04-09 14:34:45 +03:00
|
|
|
class SmartButton implements SmartButtonInterface
|
2020-04-02 08:38:00 +03:00
|
|
|
{
|
|
|
|
private $moduleUrl;
|
2020-04-09 09:33:57 +03:00
|
|
|
private $sessionHandler;
|
2020-04-09 18:15:44 +03:00
|
|
|
private $settings;
|
2020-04-13 09:07:20 +03:00
|
|
|
private $payeeRepository;
|
2020-04-30 15:28:48 +03:00
|
|
|
private $identityToken;
|
2020-05-07 12:43:46 +03:00
|
|
|
private $payerFactory;
|
2020-04-09 14:34:45 +03:00
|
|
|
|
2020-04-02 08:38:00 +03:00
|
|
|
public function __construct(
|
|
|
|
string $moduleUrl,
|
2020-04-09 09:33:57 +03:00
|
|
|
SessionHandler $sessionHandler,
|
2020-04-13 09:07:20 +03:00
|
|
|
Settings $settings,
|
2020-04-30 15:28:48 +03:00
|
|
|
PayeeRepository $payeeRepository,
|
2020-05-07 12:43:46 +03:00
|
|
|
IdentityToken $identityToken,
|
|
|
|
PayerFactory $payerFactory
|
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;
|
2020-04-09 09:33:57 +03:00
|
|
|
$this->sessionHandler = $sessionHandler;
|
2020-04-09 18:15:44 +03:00
|
|
|
$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;
|
2020-05-07 12:43:46 +03:00
|
|
|
$this->payerFactory = $payerFactory;
|
2020-04-02 08:38:00 +03:00
|
|
|
}
|
|
|
|
|
2020-04-09 18:15:44 +03:00
|
|
|
public function renderWrapper(): bool
|
2020-04-02 08:38:00 +03:00
|
|
|
{
|
2020-04-30 15:28:48 +03:00
|
|
|
|
2020-04-30 16:30:23 +03:00
|
|
|
$buttonRenderer = static function () {
|
2020-04-02 08:38:00 +03:00
|
|
|
echo '<div id="ppc-button"></div>';
|
2020-04-30 16:30:23 +03:00
|
|
|
};
|
|
|
|
|
2020-04-30 16:58:00 +03:00
|
|
|
$dccRenderer = static function ($id = null) {
|
2020-04-30 16:30:23 +03:00
|
|
|
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>
|
2020-04-30 16:30:23 +03:00
|
|
|
</form>',
|
|
|
|
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
|
|
|
};
|
2020-06-15 11:48:37 +03:00
|
|
|
if (
|
|
|
|
is_cart()
|
|
|
|
&& $this->settings->has('button_cart_enabled')
|
|
|
|
&& wc_string_to_bool($this->settings->get('button_cart_enabled'))
|
|
|
|
) {
|
2020-04-08 12:33:34 +03:00
|
|
|
add_action(
|
2020-04-09 18:58:07 +03:00
|
|
|
'woocommerce_proceed_to_checkout',
|
2020-04-30 16:30:23 +03:00
|
|
|
$buttonRenderer,
|
|
|
|
20
|
|
|
|
);
|
|
|
|
}
|
2020-06-15 11:48:37 +03:00
|
|
|
if (
|
|
|
|
is_cart()
|
|
|
|
&& $this->settings->has('dcc_cart_enabled')
|
|
|
|
&& wc_string_to_bool($this->settings->get('dcc_cart_enabled'))
|
|
|
|
) {
|
2020-04-30 16:30:23 +03:00
|
|
|
add_action(
|
|
|
|
'woocommerce_proceed_to_checkout',
|
|
|
|
$dccRenderer,
|
2020-04-08 12:33:34 +03:00
|
|
|
20
|
|
|
|
);
|
|
|
|
}
|
2020-06-15 11:48:37 +03:00
|
|
|
if (
|
|
|
|
is_product()
|
|
|
|
&& $this->settings->has('button_single_product_enabled')
|
|
|
|
&& wc_string_to_bool($this->settings->get('button_single_product_enabled'))
|
|
|
|
) {
|
2020-04-02 08:38:00 +03:00
|
|
|
add_action(
|
|
|
|
'woocommerce_single_product_summary',
|
2020-04-30 16:30:23 +03:00
|
|
|
$buttonRenderer,
|
|
|
|
31
|
|
|
|
);
|
|
|
|
}
|
2020-06-15 11:48:37 +03:00
|
|
|
if (
|
|
|
|
is_product()
|
|
|
|
&& $this->settings->has('dcc_single_product_enabled')
|
|
|
|
&& wc_string_to_bool($this->settings->get('dcc_single_product_enabled'))
|
|
|
|
) {
|
2020-04-30 16:30:23 +03:00
|
|
|
add_action(
|
|
|
|
'woocommerce_single_product_summary',
|
|
|
|
$dccRenderer,
|
2020-04-02 08:38:00 +03:00
|
|
|
31
|
|
|
|
);
|
|
|
|
}
|
2020-06-15 11:48:37 +03:00
|
|
|
if (
|
|
|
|
$this->settings->has('button_mini_cart_enabled')
|
|
|
|
&& wc_string_to_bool($this->settings->get('button_mini_cart_enabled'))
|
|
|
|
) {
|
2020-04-09 18:15:44 +03:00
|
|
|
add_action(
|
|
|
|
'woocommerce_widget_shopping_cart_after_buttons',
|
2020-04-28 12:31:12 +03:00
|
|
|
static function () {
|
2020-04-09 18:15:44 +03:00
|
|
|
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')
|
|
|
|
&& wc_string_to_bool($this->settings->get('dcc_mini_cart_enabled'))
|
|
|
|
) {
|
2020-04-30 16:30:23 +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');
|
2020-04-30 16:30:23 +03:00
|
|
|
},
|
|
|
|
31
|
|
|
|
);
|
|
|
|
}
|
2020-04-08 15:31:05 +03:00
|
|
|
add_action(
|
|
|
|
'woocommerce_review_order_after_submit',
|
2020-04-30 16:30:23 +03:00
|
|
|
$buttonRenderer,
|
2020-04-08 15:31:05 +03:00
|
|
|
10
|
|
|
|
);
|
2020-06-15 11:48:37 +03:00
|
|
|
if (
|
|
|
|
|
|
|
|
$this->settings->has('dcc_checkout_enabled')
|
|
|
|
&& wc_string_to_bool($this->settings->get('dcc_checkout_enabled'))
|
|
|
|
) {
|
2020-04-30 16:30:23 +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
|
|
|
}
|
|
|
|
|
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-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()),
|
|
|
|
],
|
2020-04-08 16:23:33 +03:00
|
|
|
'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-04-28 09:14:05 +03:00
|
|
|
'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',
|
2020-04-08 17:00:47 +03:00
|
|
|
'cancel_wrapper' => '#ppcp-cancel',
|
2020-04-13 09:07:20 +03:00
|
|
|
'url' => $this->url(),
|
2020-04-09 19:15:28 +03:00
|
|
|
'style' => [
|
|
|
|
'layout' => 'vertical',
|
2020-06-15 11:48:37 +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,
|
2020-04-09 19:15:28 +03:00
|
|
|
'label' => 'paypal',
|
|
|
|
],
|
2020-04-06 11:16:18 +03:00
|
|
|
],
|
2020-04-30 15:28:48 +03:00
|
|
|
'hosted_fields' => [
|
2020-04-30 16:30:23 +03:00
|
|
|
'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-04-13 09:07:20 +03:00
|
|
|
return $localize;
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:31:12 +03:00
|
|
|
private function payerData(): ?array
|
|
|
|
{
|
|
|
|
|
2020-04-28 09:14:05 +03:00
|
|
|
$customer = WC()->customer;
|
|
|
|
if (! is_user_logged_in() || ! is_a($customer, \WC_Customer::class)) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-07 12:43:46 +03:00
|
|
|
return $this->payerFactory->fromCustomer($customer)->toArray();
|
2020-04-28 09:14:05 +03:00
|
|
|
}
|
|
|
|
|
2020-04-28 12:31:12 +03:00
|
|
|
private function url(): string
|
2020-04-13 09:07:20 +03:00
|
|
|
{
|
|
|
|
$params = [
|
|
|
|
//ToDo: Add the correct client id, toggle when settings is set to sandbox
|
2020-04-30 15:28:48 +03:00
|
|
|
'client-id' => 'AQB97CzMsd58-It1vxbcDAGvMuXNCXRD9le_XUaMlHB_U7XsU9IiItBwGQOtZv9sEeD6xs2vlIrL4NiD',
|
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
|
2020-04-30 15:28:48 +03:00
|
|
|
'vault' => $this->dccIsEnabled() ? 'false' : 'false',
|
2020-04-13 09:07:20 +03:00
|
|
|
'commit' => is_checkout() ? 'true' : 'false',
|
2020-06-15 11:48:37 +03:00
|
|
|
'intent' => ($this->settings->has('intent')) ? $this->settings->get('intent') : 'commit',
|
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-15 11:48: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-04-30 15:28:48 +03:00
|
|
|
private function attributes() : array {
|
|
|
|
$attributes = [
|
|
|
|
//'data-partner-attribution-id' => '',
|
|
|
|
];
|
|
|
|
try {
|
|
|
|
$clientToken = $this->identityToken->generate();
|
|
|
|
$attributes['data-client-token'] = $clientToken->token();
|
|
|
|
return $attributes;
|
|
|
|
} catch (RuntimeException $exception) {
|
|
|
|
return $attributes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function components() : array
|
|
|
|
{
|
|
|
|
$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()) {
|
|
|
|
$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
|
|
|
|
|
|
|
private function dccIsEnabled() : bool
|
|
|
|
{
|
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) && wc_string_to_bool($this->settings->get($key))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2020-04-30 15:28:48 +03:00
|
|
|
}
|
2020-04-06 11:16:18 +03:00
|
|
|
}
|