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

134 lines
4.4 KiB
PHP
Raw Normal View History

2020-04-02 08:38:00 +03:00
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Assets;
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-02 08:38:00 +03:00
public function __construct(
string $moduleUrl,
SessionHandler $sessionHandler,
Settings $settings
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-02 08:38:00 +03:00
}
public function renderWrapper(): bool
2020-04-02 08:38:00 +03:00
{
2020-04-06 11:16:18 +03:00
$renderer = function () {
2020-04-02 08:38:00 +03:00
echo '<div id="ppc-button"></div>';
};
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',
2020-04-08 12:33:34 +03:00
$renderer,
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',
$renderer,
31
);
}
if (wc_string_to_bool($this->settings->get('button_mini_cart_enabled'))) {
add_action(
'woocommerce_widget_shopping_cart_after_buttons',
function () {
echo '<p id="ppc-button-minicart" class="woocommerce-mini-cart__buttons buttons"></p>';
},
30
);
}
add_action(
'woocommerce_review_order_after_submit',
$renderer,
10
);
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',
$this->moduleUrl . '/assets/js/button.js'
);
$params = [
2020-04-09 13:06:02 +03:00
//ToDo: Add the correct client id, toggle when settings is set to sandbox
2020-04-02 08:38:00 +03:00
'client-id' => 'AcVzowpNCpTxFzLG7onQI4JD0sVcA0BkZv-D42qRZPv_gZ8cNfX9zGL_8bXmSu7cbJ5B2DH7sot8vDpw',
2020-04-06 10:51:56 +03:00
'currency' => get_woocommerce_currency(),
2020-04-09 12:50:10 +03:00
'locale' => get_user_locale(),
2020-04-09 13:06:02 +03:00
//'debug' => (defined('WP_DEBUG') && WP_DEBUG) ? 'true' : 'false',
//ToDo: Update date on releases.
'integration-date' => date('Y-m-d'),
'components' => 'marks,buttons',
//ToDo: Probably only needed, when DCC
'vault' => 'true',
'commit' => is_checkout() ? 'true' : 'false',
2020-04-02 08:38:00 +03:00
];
$smartButtonUrl = add_query_arg($params, 'https://www.paypal.com/sdk/js');
$localize = [
'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
],
'button' => [
'wrapper' => '#ppc-button',
2020-04-08 12:33:34 +03:00
'mini_cart_wrapper' => '#ppc-button-minicart',
'cancel_wrapper' => '#ppcp-cancel',
2020-04-09 18:15:57 +03:00
'url' => $smartButtonUrl,
2020-04-06 11:16:18 +03:00
],
2020-04-02 08:38:00 +03:00
];
wp_localize_script(
'paypal-smart-button',
'PayPalCommerceGateway',
$localize
);
return true;
}
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-06 11:16:18 +03:00
}