2020-04-02 08:38:00 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Inpsyde\PayPalCommerce\Button\Assets;
|
|
|
|
|
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-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-09 14:34:45 +03:00
|
|
|
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;
|
2020-04-09 09:33:57 +03:00
|
|
|
$this->sessionHandler = $sessionHandler;
|
2020-04-09 18:15:44 +03:00
|
|
|
$this->settings = $settings;
|
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-06 11:16:18 +03:00
|
|
|
$renderer = function () {
|
2020-04-02 08:38:00 +03:00
|
|
|
echo '<div id="ppc-button"></div>';
|
|
|
|
};
|
2020-04-09 18:15:44 +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(
|
2020-04-09 18:58:07 +03:00
|
|
|
'woocommerce_proceed_to_checkout',
|
2020-04-08 12:33:34 +03:00
|
|
|
$renderer,
|
|
|
|
20
|
|
|
|
);
|
|
|
|
}
|
2020-04-09 18:15:44 +03:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
2020-04-09 18:15:44 +03:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
2020-04-08 15:31:05 +03:00
|
|
|
add_action(
|
|
|
|
'woocommerce_review_order_after_submit',
|
2020-04-08 17:50:10 +03:00
|
|
|
$renderer,
|
2020-04-08 15:31:05 +03:00
|
|
|
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()),
|
|
|
|
],
|
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
|
|
|
],
|
|
|
|
'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-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
|
|
|
}
|