summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--class-wc-gnutaler-gateway.php225
-rw-r--r--readme.txt19
2 files changed, 165 insertions, 79 deletions
diff --git a/class-wc-gnutaler-gateway.php b/class-wc-gnutaler-gateway.php
index dfddfe0..fe86503 100644
--- a/class-wc-gnutaler-gateway.php
+++ b/class-wc-gnutaler-gateway.php
@@ -9,7 +9,7 @@
* Plugin Name: GNU Taler Payment for WooCommerce
* Plugin URI: https://git.taler.net/woocommerce-taler
* Description: This plugin enables payments via the GNU Taler payment system
- * Version: 0.8
+ * Version: 0.9.4
* Author: Dominique Hofmann, Jan StrĂ¼bin, Christian Grothoff
* Author URI: https://taler.net/
*
@@ -37,7 +37,7 @@
* Which version of the Taler merchant protocol is implemented
* by this implementation? Used to determine compatibility.
*/
-define( 'GNU_TALER_MERCHANT_PROTOCOL_CURRENT', 1 );
+define( 'GNU_TALER_MERCHANT_PROTOCOL_CURRENT', 3 );
/**
* How many merchant protocol versions are we backwards compatible with?
@@ -118,7 +118,7 @@ function gnutaler_init_gateway_class() {
*
* @var Plugin loggger.
*/
- private static $logger = false;
+ private static $log = false;
/**
* True if logging is enabled in our configuration.
@@ -131,14 +131,14 @@ function gnutaler_init_gateway_class() {
* Class constructor
*/
public function __construct() {
- $this->id = 'gnutaler'; // Payment gateway plugin ID.
- $this->logger = new WC_logger( $this->id ); // Setup logging.
- $this->icon = plugins_url( '/assets/images/taler.png', __FILE__ );
- // We cannot use custom fields to show the QR code / do the wallet integration as WC doesn't give us the order_id at that time. Bummer.
- $this->has_fields = false;
- // The following texts will be displayed on the payment plugins settings page.
- $this->method_title = 'GNU Taler';
- $this->method_description = __( 'This plugin enables payments via the GNU Taler payment system', 'gnutaler' );
+
+ $this->setup_properties();
+ $this->init_form_fields();
+ $this->init_settings();
+
+ // Setup logging.
+ $this->debug = 'yes' === $this->get_option( 'debug', 'no' );
+ self::$log_enabled = $this->debug;
// This gateway can support refunds, saved payment methods.
$this->supports = array(
@@ -146,11 +146,63 @@ function gnutaler_init_gateway_class() {
'refunds',
);
- // Setup logging.
- $this->debug = 'yes' === $this->get_option( 'debug', 'no' );
- self::$log_enabled = $this->debug;
+ $this->title = $this->get_option( 'title' );
+ $this->description = $this->get_option( 'description' );
+ $this->instructions = $this->get_option( 'instructions' );
+ $this->enable_for_virtual = true;
+
+ $this->enabled = $this->get_option( 'enabled' );
+ $this->gnu_taler_backend_url = $this->get_option( 'gnu_taler_backend_url' );
+ // Remove trailing '/', we add one always ourselves...
+ if ( substr( $this->gnu_taler_backend_url, -1 ) === '/' ) {
+ $this->gnu_taler_backend_url = substr( $this->gnu_taler_backend_url, 0, -1 );
+ }
+
+ // Make transaction ID a link. We use the public version
+ // here, as a user clicking on the link could not supply
+ // the authorization header.
+ // See also: https://woocommerce.wordpress.com/2014/08/05/wc-2-2-payment-gateways-adding-refund-support-and-transaction-ids/.
+ $this->view_transaction_url = $this->gnu_taler_backend_url . '/orders/%s';
+
+ // Register handler for the fulfillment URL.
+ add_action(
+ 'woocommerce_api_' . strtolower( get_class( $this ) ),
+ array( &$this, 'fulfillment_url_handler' )
+ );
+
+ // This action hook saves the settings.
+ add_action(
+ 'woocommerce_update_options_payment_gateways_' . $this->id,
+ array( $this, 'process_admin_options' )
+ );
+
+ // Modify WC canonical refund e-mail notifications to add link to order status page.
+ // (according to https://www.businessbloomer.com/woocommerce-add-extra-content-order-email/).
+ add_action(
+ 'woocommerce_email_before_order_table',
+ array( $this, 'add_content_refund_email' ),
+ 20,
+ 4
+ );
+ }
+
+
+ /**
+ * Setup general properties for the gateway.
+ */
+ protected function setup_properties() {
+ $this->id = 'gnutaler'; // Payment gateway plugin ID.
+ $this->icon = plugins_url( '/assets/images/taler.png', __FILE__ );
+ $this->method_title = 'GNU Taler';
+ $this->method_description = __( 'This plugin enables payments via the GNU Taler payment system', 'gnutaler' );
+ // We cannot use custom fields to show the QR code / do the wallet integration as WC doesn't give us the order_id at that time. Bummer.
+ $this->has_fields = false;
+ }
- // Setup 'form_fields'.
+ /**
+ * Initialise Gateway Settings Form Fields.
+ */
+ public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'gnutaler' ),
@@ -169,8 +221,16 @@ function gnutaler_init_gateway_class() {
'description' => array(
'title' => __( 'Description', 'gnutaler' ),
'type' => 'textarea',
- 'description' => __( 'This controls the description for the payment option which the customer sees during checkout.', 'gnutaler' ),
- 'default' => __( 'Pay with GNU Taler', 'gnutaler' ),
+ 'description' => __( 'Payment method description which the customer sees during checkout.', 'gnutaler' ),
+ 'default' => __( 'Pay digitally with GNU Taler', 'gnutaler' ),
+ 'desc_tip' => true,
+ ),
+ 'instructions' => array(
+ 'title' => __( 'Instructions', 'gnutaler' ),
+ 'type' => 'textarea',
+ 'description' => __( 'Instructions that will be added to the thank you page.', 'gnutaler' ),
+ 'default' => __( 'Thank you for paying with GNU Taler', 'gnutaler' ),
+ 'desc_tip' => true,
),
'gnu_taler_backend_url' => array(
'title' => __( 'Taler backend URL', 'gnutaler' ),
@@ -181,8 +241,8 @@ function gnutaler_init_gateway_class() {
'GNU_Taler_Backend_API_Key' => array(
'title' => __( 'Taler Backend API Key', 'gnutaler' ),
'type' => 'text',
- 'description' => __( 'Enter your API key to authenticate with the Taler backend.', 'gnutaler' ),
- 'default' => 'Sandbox ApiKey',
+ 'description' => __( 'Enter your API key to authenticate with the Taler backend. Will be sent as a "Bearer" token using the HTTP "Authorization" header. Typically should be prefixed with "secret-token:" (RFC 8959).', 'gnutaler' ),
+ 'default' => 'secret-token:Sandbox ApiKey',
),
'Order_text' => array(
'title' => __( 'Summary Text of the Order', 'gnutaler' ),
@@ -208,45 +268,6 @@ function gnutaler_init_gateway_class() {
'default' => 'no',
),
);
-
- // Load the settings.
- $this->init_settings();
- $this->title = $this->get_option( 'title' );
- $this->description = $this->get_option( 'description' );
- $this->enabled = $this->get_option( 'enabled' );
- $this->gnu_taler_backend_url = $this->get_option( 'gnu_taler_backend_url' );
- // Remove trailing '/', we add one always ourselves...
- if ( substr( $this->gnu_taler_backend_url, -1 ) === '/' ) {
- $this->gnu_taler_backend_url = substr( $this->gnu_taler_backend_url, 0, -1 );
- }
-
- // Make transaction ID a link. We use the public version
- // here, as a user clicking on the link could not supply
- // the authorization header.
- // See also: https://woocommerce.wordpress.com/2014/08/05/wc-2-2-payment-gateways-adding-refund-support-and-transaction-ids/.
- $this->view_transaction_url = $this->gnu_taler_backend_url . '/orders/%s';
-
- // Register handler for the fulfillment URL.
- $hname = 'woocommerce_api_' . strtolower( get_class( $this ) );
- add_action(
- $hname,
- array( &$this, 'fulfillment_url_handler' )
- );
-
- // This action hook saves the settings.
- add_action(
- 'woocommerce_update_options_payment_gateways_' . $this->id,
- array( $this, 'process_admin_options' )
- );
-
- // Modify WC canonical refund e-mail notifications to add link to order status page.
- // (according to https://www.businessbloomer.com/woocommerce-add-extra-content-order-email/).
- add_action(
- 'woocommerce_email_before_order_table',
- array( $this, 'add_content_refund_email' ),
- 20,
- 4
- );
}
/**
@@ -284,10 +305,10 @@ function gnutaler_init_gateway_class() {
// Maybe clear logs.
if ( 'yes' !== $this->get_option( 'debug', 'no' ) ) {
- if ( empty( self::$logger ) ) {
- self::$logger = wc_get_logger();
+ if ( empty( self::$log ) ) {
+ self::$log = wc_get_logger();
}
- self::$logger->clear( 'gnutaler' );
+ self::$log->clear( 'gnutaler' );
}
return $saved;
@@ -412,14 +433,14 @@ function gnutaler_init_gateway_class() {
'user-agent' => '', // Minimize information leakage.
'blocking' => true, // We do nothing without it.
'headers' => array(
- 'Authorization: ' . $apikey,
+ 'Authorization' => 'Bearer ' . $apikey,
),
'decompress' => true,
'limit_response_size' => 1024 * 1024, // More than enough.
);
if ( $body ) {
$args['body'] = wp_json_encode( $body, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES, 16 );
- $args['headers'][] = 'Content-type: application/json';
+ $args['headers']['Content-type'] = 'application/json';
$args['compress'] = true;
}
$this->debug( 'Issuing HTTP ' . $method . ' request to ' . $url . ' with options ' . wp_json_encode( $args ) . ' and body ' . $body );
@@ -490,6 +511,8 @@ function gnutaler_init_gateway_class() {
* @return bool - Returns if valid or not.
*/
private function verify_backend_url( $url, $ecurrency ): bool {
+ $this->warning( "Verifying backend URL " . $url );
+
$config = $this->call_api( 'GET', $url . '/config', false );
$config_http_status = $config['http_code'];
$config_body = $config['message'];
@@ -523,7 +546,8 @@ function gnutaler_init_gateway_class() {
$age = $ver[2];
if ( ( ! is_numeric( $current ) )
|| ( ! is_numeric( $revision ) )
- || ( ! is_numeric( $age ) )
+ || ( ! is_numeric( $age )
+ || ( $age > $current ) )
) {
$this->error(
sprintf(
@@ -557,7 +581,8 @@ function gnutaler_init_gateway_class() {
return false;
}
$currency = $info['currency'];
- if ( ( ! is_null( $ecurrency ) ) && ( 0 !== strcasecmp( $currency, $ecurrency ) ) ) {
+ if ( ( ! is_null( $ecurrency ) ) &&
+ ( 0 !== strcasecmp( $currency, $ecurrency ) ) ) {
$this->error(
sprintf(
/* translators: first placeholder is the Taler backend currency, second the expected currency from WooCommerce */
@@ -723,7 +748,7 @@ function gnutaler_init_gateway_class() {
);
if ( isset( $refund_delay ) ) {
$order_json['refund_delay'] = array(
- 'd_ms' => 1000 * 60 * 60 * 24 * intval( $refund_delay ),
+ 'd_us' => 1000 * 1000 * 60 * 60 * 24 * intval( $refund_delay ),
);
}
return $order_json;
@@ -765,11 +790,51 @@ function gnutaler_init_gateway_class() {
$shipping_address_street = '';
$shipping_address_street_nr = '';
- $store_address = $wc_order->get_shipping_address_1();
+ $store_address = $wc_order->get_shipping_address_1( $context = 'view' );
+ if ( is_null( $store_address ) || empty( $store_address ) )
+ $store_address = $wc_order->get_billing_address_1( $context = 'view' );
$store_address_inverted = strrev( $store_address );
$store_address_array = str_split( $store_address_inverted );
-
- // Split the address into street and street number.
+ $country = $wc_order->get_shipping_country ($context = 'view');
+ if ( is_null( $country ) || empty( $country ) )
+ $country = $wc_order->get_billing_country ($context = 'view');
+ $state = $wc_order->get_shipping_state ($context = 'view');
+ if ( is_null( $state ) || empty( $state ) )
+ $state = $wc_order->get_billing_state ($context = 'view');
+ $city = $wc_order->get_shipping_city ($context = 'view');
+ if ( is_null( $city ) || empty( $city ) )
+ $city = $wc_order->get_billing_city ($context = 'view');
+ $postcode = $wc_order->get_shipping_postcode ($context = 'view');
+ if ( is_null( $postcode ) || empty( $postcode ) )
+ $postcode = $wc_order->get_billing_postcode ($context = 'view');
+
+ $this->info (
+ sprintf(
+ 'Shipping address is %s - %s - %s - %s - %s',
+ $store_address,
+ $wc_order->get_shipping_country ($context = 'view'),
+ $wc_order->get_shipping_state ($context = 'view'),
+ $wc_order->get_shipping_city ($context = 'view'),
+ $wc_order->get_shipping_postcode ($context = 'view')) );
+
+ $this->info (
+ sprintf(
+ 'Billing address is %s - %s - %s - %s - %s',
+ $store_address,
+ $wc_order->get_billing_country ($context = 'view'),
+ $wc_order->get_billing_state ($context = 'view'),
+ $wc_order->get_billing_city ($context = 'view'),
+ $wc_order->get_billing_postcode ($context = 'view')) );
+ $this->info (
+ sprintf(
+ 'Using address is %s - %s - %s - %s - %s',
+ $store_address,
+ $country,
+ $state,
+ $city,
+ $postcode));
+
+ // Split the address into street and street number.
foreach ( $store_address_array as $char ) {
if ( ! $whitechar_encounter ) {
$shipping_address_street .= $char;
@@ -780,10 +845,10 @@ function gnutaler_init_gateway_class() {
}
}
$ret = array(
- 'country' => $wc_order->get_shipping_country(),
- 'country_subdivision' => $wc_order->get_shipping_state(),
- 'town' => $wc_order->get_shipping_city(),
- 'post_code' => $wc_order->get_shipping_postcode(),
+ 'country' => $country,
+ 'country_subdivision' => $state,
+ 'town' => $city,
+ 'post_code' => $postcode,
'street' => $shipping_address_street,
'building_number' => $shipping_address_street_nr,
);
@@ -1021,12 +1086,18 @@ function gnutaler_init_gateway_class() {
// We intentionally do NOT verify the nonce here, as logging
// should always work.
// phpcs:disable WordPress.Security.NonceVerification
- $order_id = sanitize_text_field( wp_unslash( $_GET['order_id'] ) );
+ if ( isset ($_GET['order_id'] ) ) {
+ $order_id = sanitize_text_field( wp_unslash( $_GET['order_id'] ) );
+ }
+ else
+ {
+ $order_id = 'NONE';
+ }
// phpcs:enable
- if ( empty( self::$logger ) ) {
- self::$logger = wc_get_logger();
+ if ( empty( self::$log ) ) {
+ self::$log = wc_get_logger();
}
- self::$logger->log( $level, $user_id . '-' . $order_id . ': ' . $msg, array( 'source' => 'gnutaler' ) );
+ self::$log->log( $level, $user_id . '-' . $order_id . ': ' . $msg, array( 'source' => 'gnutaler' ) );
}
}
diff --git a/readme.txt b/readme.txt
index bb5978a..d43d1aa 100644
--- a/readme.txt
+++ b/readme.txt
@@ -2,8 +2,8 @@
Contributors: gnutaler
Tags: WooCommerce, e-commerce, Taler, Payment Gateway
Requires at least: 5.1
-Tested up to: 5.5.1
-Stable tag: 5.5
+Tested up to: 7.4.0
+Stable tag: 7.4
Requires PHP: 7.2
License: GNU General Public License v2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -45,6 +45,21 @@ A: For the plugin to work correctly you need to have the WooCommerce plugin inst
== Changelog ==
+= 0.9.4 =
+
+* Use billing address in contract if shipping address is not given
+
+= 0.9.3 =
+
+* Fix log logic
+* Fix header transmission
+
+= 0.9.2 =
+
+* Improve comments about settings, especially explain "secret-token:" prefix.
+* Automatically add "Bearer" to "Authorization" header.
+* Minor refactoring to match contemporary plugins better.
+
= 0.8.0 =
* Adaptations to GNU Taler Merchant API 1:0:0