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

131 lines
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;
2020-04-02 08:38:00 +03:00
class SmartButton
{
private $moduleUrl;
private $sessionHandler;
2020-04-02 08:38:00 +03:00
private $isSandbox;
public function __construct(
string $moduleUrl,
SessionHandler $sessionHandler,
2020-04-02 08:38:00 +03:00
bool $isSandbox
) {
2020-04-06 11:16:18 +03:00
2020-04-02 08:38:00 +03:00
$this->moduleUrl = $moduleUrl;
$this->sessionHandler = $sessionHandler;
2020-04-02 08:38:00 +03:00
$this->isSandbox = $isSandbox;
}
public function renderWrapper() : bool
{
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-08 12:33:34 +03:00
if (is_cart()) {
add_action(
'woocommerce_after_cart_totals',
$renderer,
20
);
}
2020-04-02 08:38:00 +03:00
if (is_product()) {
add_action(
'woocommerce_single_product_summary',
$renderer,
31
);
}
add_action(
'woocommerce_review_order_after_submit',
$renderer,
10
);
2020-04-08 12:33:34 +03:00
add_action(
'woocommerce_widget_shopping_cart_buttons',
function () {
2020-04-08 12:33:34 +03:00
echo '<span id="ppc-button-minicart"></span>';
},
30
);
return true;
2020-04-02 08:38:00 +03:00
}
public function enqueue() : bool
{
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-02 08:38:00 +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-06 11:16:18 +03:00
private function context() : string
{
2020-04-02 08:38:00 +03:00
$context = 'mini-cart';
if (is_product()) {
$context = 'product';
}
if (is_cart()) {
$context = 'cart';
}
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
}