| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Gateway\Blocks; |
| 4 |
|
| 5 |
use Payplug\PayplugWoocommerce\Gateway\Oney\OneyAccountMapper; |
| 6 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 7 |
|
| 8 |
class PayplugOney extends PayplugGenericBlock |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Payment method name/id/slug. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $name = 'oney'; |
| 16 |
|
| 17 |
protected $icon = ''; |
| 18 |
|
| 19 |
protected $cart; |
| 20 |
|
| 21 |
protected $total_price; |
| 22 |
|
| 23 |
/** |
| 24 |
* Returns an array of scripts/handles to be registered for this payment method. |
| 25 |
* |
| 26 |
* The bundled JS calls the official Oney widget (window.loadOneyWidget / |
| 27 |
* window.oneyMerchantApp), which is exposed by a separate external loader script - not |
| 28 |
* something webpack bundles. Register and prepend it here. Execution order between the |
| 29 |
* loader and the bundle doesn't matter: the bundle only calls loadOneyWidget() from inside |
| 30 |
* a useEffect, which fires after the checkout page has finished mounting - i.e. well after |
| 31 |
* every enqueued script has already executed. |
| 32 |
* |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
public function get_payment_method_script_handles() |
| 36 |
{ |
| 37 |
wp_register_script('payplug-oney-loader', PayplugWoocommerceHelper::get_oney_loader_url(), [], PAYPLUG_GATEWAY_VERSION, true); |
| 38 |
|
| 39 |
return array_merge(['payplug-oney-loader'], parent::get_payment_method_script_handles()); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns an associative array of data to be exposed for the payment method's client side. |
| 44 |
*/ |
| 45 |
public function get_payment_method_data() |
| 46 |
{ |
| 47 |
$data = parent::get_payment_method_data(); |
| 48 |
|
| 49 |
if (is_checkout()) { |
| 50 |
$this->cart = WC()->cart; |
| 51 |
$this->total_price = floatval(WC()->cart->total); |
| 52 |
} |
| 53 |
|
| 54 |
$account = PayplugWoocommerceHelper::get_account_data_from_options(); |
| 55 |
$oney_settings = $this->gateway->get_configuration()->get_option('payment_methods.configuration.oney'); |
| 56 |
$country = PayplugWoocommerceHelper::getISOCountryCode(); |
| 57 |
$mapper = OneyAccountMapper::map((array) $account, $oney_settings, $country); |
| 58 |
$fee_mode = !empty($oney_settings['with_fees']) ? 'with_fees' : 'without_fees'; |
| 59 |
|
| 60 |
if (PayplugWoocommerceHelper::is_cart_block() && is_cart() && !PayplugWoocommerceHelper::is_subscription()) { |
| 61 |
$data['oney_cart_label'] = __('OR PAY IN', 'payplug'); |
| 62 |
if ('with_fees' === $fee_mode) { |
| 63 |
$data['oney_cart_logo'] = esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/lg-3x4xoney.png'); |
| 64 |
} else { |
| 65 |
$data['oney_cart_logo'] = esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/Oneywithoutfees3x4x.png'); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
$data['icon'] = [ |
| 70 |
'src' => esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/' . $this->icon), |
| 71 |
'class' => 'payplug-payment-icon', |
| 72 |
'alt' => $this->gateway->title, |
| 73 |
]; |
| 74 |
$data['description'] = $this->gateway->description; |
| 75 |
$data['currency'] = get_woocommerce_currency_symbol(get_option('woocommerce_currency')); |
| 76 |
$data['allowed_country_codes'] = $this->gateway->allowed_country_codes; |
| 77 |
|
| 78 |
$data['oney_widget'] = [ |
| 79 |
'merchant_guid' => $mapper->get_merchant_guid(), |
| 80 |
'business_transaction_code' => $mapper->business_transaction_code_for_gateway($this->gateway->id), |
| 81 |
'business_transaction_codes' => $mapper->business_transaction_codes($fee_mode), |
| 82 |
'country' => $country, |
| 83 |
'language' => strtoupper(substr(get_locale(), 0, 2)), |
| 84 |
'payment_amount' => $this->total_price, |
| 85 |
]; |
| 86 |
|
| 87 |
$oney_amount = json_decode($oney_settings['custom_amounts'], true); |
| 88 |
$data['requirements'] = [ |
| 89 |
'max_quantity' => $this->gateway::ONEY_PRODUCT_QUANTITY_MAXIMUM, |
| 90 |
'min_threshold' => $oney_amount['min'], |
| 91 |
'max_threshold' => $oney_amount['max'], |
| 92 |
'allowed_country_codes' => $this->gateway->allowed_country_codes, |
| 93 |
]; |
| 94 |
|
| 95 |
return $data; |
| 96 |
} |
| 97 |
} |
| 98 |
|