Skip to content

Commit

Permalink
Merge pull request #94 from onpayio/task/performance_optimization
Browse files Browse the repository at this point in the history
Task/performance optimization
  • Loading branch information
ebbesmoeller authored Sep 6, 2023
2 parents 30c7874 + 6245673 commit 353b84d
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 284 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
- Added ability to set language of created payments according to frontoffice language
- Performance optimization of payment creation.
- Bugfix of subscriptions not being available in block layout.

## [1.0.34] - 2023-08-16
- Restructured the settings page into sevaral sections
Expand Down
5 changes: 4 additions & 1 deletion assets/js/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ function registerMethod(method) {
content: Object(element.createElement)(content, null),
edit: element.createElement('div', {}, method.description),
canMakePayment: () => true,
ariaLabel: method.id
ariaLabel: method.id,
supports: {
features: method.supports,
}
};

// Register method
Expand Down
71 changes: 30 additions & 41 deletions classes/abstract-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* MIT License
*
* Copyright (c) 2019 OnPay.io
* Copyright (c) 2023 OnPay.io
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,17 +24,13 @@
*/

abstract class wc_onpay_gateway_abstract extends WC_Payment_Gateway {
private $apiAuthorized;

public function admin_options() {
// Redirect to general plugin settings page
wp_redirect(wc_onpay_query_helper::generate_url(['page' => 'wc-settings','tab' => WC_OnPay::WC_ONPAY_ID, 'section' => 'methods']));
exit;
}

public function __construct() {
add_action('woocommerce_receipt_' . $this->id, [$this, 'checkout']);
}
public function __construct() {}

public function getMethodTitle() {
if (is_admin()) {
Expand All @@ -49,33 +45,42 @@ public function getMethodTitle() {
return $this->method_title;
}

public function process_payment($order_id) {
$order = new WC_Order($order_id);
$redirectUrl = $order->get_checkout_payment_url(true);
if (class_exists('WC_Subscriptions_Change_Payment_Gateway') && WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment) {
$redirectUrl = add_query_arg('update_method', true, $redirectUrl);
}
return [
'result' => 'success',
'redirect' => $redirectUrl
];
}

/**
* Gets payment link and redirects browser to newly created payment
*/
public function checkout($order_id) {
public function process_payment($order_id) {
$order = new WC_Order($order_id);
$updateMethod = wc_onpay_query_helper::get_query_value('update_method') !== null ? true : false;
$updateMethod = class_exists('WC_Subscriptions_Change_Payment_Gateway') && WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment;
$error = '';

try {
$paymentWindow = self::get_payment_window($order, $updateMethod);
wp_redirect(self::getPaymentLink($paymentWindow));
exit;
$redirect = self::getPaymentLink($paymentWindow);

if ($updateMethod) {
// If we're doing an update of method, do a manual redirect.
wp_redirect($redirect);
exit;
}

return [
'result' => 'success',
'redirect' => $redirect
];
} catch (InvalidArgumentException $e) {
echo 'Invalid data provided(' . $e->getMessage() . '). Unable to create OnPay payment.';
$error = __('Invalid data provided. Unable to create OnPay payment', 'wc-onpay') . ' (' . $e->getMessage() . ')';
} catch (WoocommerceOnpay\OnPay\API\Exception\TokenException $e) {
echo 'Authorized connection to OnPay failed.';
$error = __('Authorized connection to OnPay failed', 'wc-onpay');
}

if ($updateMethod) {
// If we're doing an update of method, manually echo error.
echo $error;
exit;
}

wc_add_notice($error, 'error');
return [];
}

/**
Expand Down Expand Up @@ -369,28 +374,12 @@ protected function getLanguage() {

// WooCommerce function indicating available state of method.
public function is_available() {
if ($this->enabled === 'yes' && $this->isApiAuthorized()) {
if ($this->enabled === 'yes') {
return true;
}
return false;
}

// Function that checks if connection to OnPay APi is authorized. Caches result by default.
private function isApiAuthorized($refresh = false) {
if(isset($this->isApiAuthorized) && !$refresh) {
return $this->isApiAuthorized;
}
$onpayApi = $this->getOnPayClient();
try {
$this->isApiAuthorized = $onpayApi->isAuthorized();
} catch (OnPay\API\Exception\ConnectionException $e) {
$this->isApiAuthorized = false;
} catch (OnPay\API\Exception\TokenException $e) {
$this->isApiAuthorized = false;
}
return $this->isApiAuthorized;
}

private function sanitizeFieldValue($value) {
$value = strval($value);
$value = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8');
Expand Down
3 changes: 2 additions & 1 deletion classes/blocks/abstract-gateway-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class wc_onpay_abstract_gateway_block extends AbstractPaymentMethodType
protected $hasRegisteredJs = false;

public function is_active() {
return $this->gateway->is_available();
return $this->gateway->enabled;
}

public function get_payment_method_script_handles() {
Expand All @@ -43,6 +43,7 @@ public function get_payment_method_script_handles() {
'description' => $this->gateway->description,
'icon' => $this->gateway->icon,
'logos' => $this->gateway->getMethodLogos(),
'supports' => $this->gateway->supports
];

if (false === $this->hasRegisteredJs) {
Expand Down
Binary file modified languages/wc-onpay-da_DK.mo
Binary file not shown.
Loading

0 comments on commit 353b84d

Please sign in to comment.