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

299 lines
11 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;
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;
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;
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
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;
2020-04-02 08:38:00 +03:00
}
public function renderWrapper(): bool
2020-04-02 08:38:00 +03:00
{
2020-04-30 15:28:48 +03:00
$buttonRenderer = static function () {
2020-04-02 08:38:00 +03:00
echo '<div id="ppc-button"></div>';
};
2020-04-30 16:58:00 +03:00
$dccRenderer = static function ($id = null) {
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>',
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
};
if (is_cart() && wc_string_to_bool($this->settings->get('button_cart_enabled'))) {
2020-04-08 12:33:34 +03:00
add_action(
'woocommerce_proceed_to_checkout',
$buttonRenderer,
20
);
}
if (is_cart() && wc_string_to_bool($this->settings->get('dcc_cart_enabled'))) {
add_action(
'woocommerce_proceed_to_checkout',
$dccRenderer,
2020-04-08 12:33:34 +03:00
20
);
}
if (is_product() && 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',
$buttonRenderer,
31
);
}
if (is_product() && wc_string_to_bool($this->settings->get('dcc_single_product_enabled'))) {
add_action(
'woocommerce_single_product_summary',
$dccRenderer,
2020-04-02 08:38:00 +03:00
31
);
}
if (wc_string_to_bool($this->settings->get('button_mini_cart_enabled'))) {
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
);
}
if (wc_string_to_bool($this->settings->get('dcc_mini_cart_enabled'))) {
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
);
if (wc_string_to_bool($this->settings->get('dcc_checkout_enabled'))) {
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(
'paypal-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(
'paypal-smart-button',
'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()),
],
'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
],
'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',
'color' => $this->settings->get('button_color'),
'shape' => $this->settings->get('button_shape'),
'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-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 [
'email_address' => $customer->get_billing_email(),
'name' => [
'surname' => $customer->get_billing_last_name(),
'given_name' => $customer->get_billing_last_name(),
],
'address' => [
'country_code' => $customer->get_billing_country(),
'address_line_1' => $customer->get_billing_address_1(),
'address_line_2' => $customer->get_billing_address_2(),
'admin_area_1' => $customer->get_billing_city(),
'admin_area_2' => $customer->get_billing_state(),
'postal_code' => $customer->get_billing_postcode(),
],
'phone' => [
'phone_type' => 'HOME',
'phone_number' => [
'national_number' => $customer->get_billing_phone(),
],
],
];
}
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',
'intent' => $this->settings->get('intent'),
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-04-16 09:29:02 +03:00
$disableFunding = $this->settings->get('disable_funding');
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
{
return
wc_string_to_bool($this->settings->get('dcc_cart_enabled'))
|| wc_string_to_bool($this->settings->get('dcc_mini_cart_enabled'))
|| wc_string_to_bool($this->settings->get('dcc_checkout_enabled'))
|| wc_string_to_bool($this->settings->get('dcc_single_product_enabled'));
2020-04-30 15:28:48 +03:00
}
2020-04-06 11:16:18 +03:00
}