Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove payment method selector from "KOMOJU" payment method #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 3 additions & 102 deletions class-wc-gateway-komoju.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class WC_Gateway_Komoju extends WC_Payment_Gateway
public function __construct()
{
$this->id = $this->id ? $this->id : 'komoju';
$this->has_fields = gettype($this->has_fields) == 'boolean' ? $this->has_fields : true;
$this->method_title = $this->method_title ? $this->method_title : __('Komoju', 'komoju-woocommerce');
$this->method_description = __('Allows payments by Komoju, dedicated to Japanese online and offline payment gateways.', 'komoju-woocommerce');
$this->debug = 'yes' === $this->get_option_compat('debug_log', 'debug');
Expand Down Expand Up @@ -127,10 +126,6 @@ public function process_payment($order_id, $payment_type = null)
$order = wc_get_order($order_id);
$return_url = WC()->api_request_url('WC_Gateway_Komoju');

if ($payment_type === null) {
$payment_type = sanitize_text_field($_POST['komoju-method']);
}

// construct line items
$line_items = [];
foreach ($order->get_items() as $item) {
Expand Down Expand Up @@ -183,7 +178,6 @@ public function process_payment($order_id, $payment_type = null)
'return_url' => $return_url,
'default_locale' => self::get_locale_or_fallback(),
'email' => $email,
'payment_types' => [$payment_type],
'payment_data' => [
'external_order_num' => $this->external_order_num($order),
'billing_address' => $billing_address,
Expand All @@ -192,6 +186,9 @@ public function process_payment($order_id, $payment_type = null)
],
'line_items' => $line_items,
];
if ($payment_type) {
$session_params['payment_types'] = [$payment_type];
}
$remove_nulls = function ($v) { return !is_null($v); };
$session_params['payment_data'] = array_filter(
$session_params['payment_data'],
Expand All @@ -206,14 +203,6 @@ public function process_payment($order_id, $payment_type = null)
];
}

/**
* Payment form on checkout page
*/
public function payment_fields()
{
$this->komoju_method_form();
}

/**
* set KOMOJU side reference for order
*
Expand All @@ -226,80 +215,6 @@ private function external_order_num($order)
return $this->invoice_prefix . $order->get_order_number() . '-' . $suffix;
}

/**
* Form to choose the payment method
*/
private function komoju_method_form($args = [], $fields = [])
{
$default_args = [
'fields_have_names' => true,
];

$args = wp_parse_args($args, apply_filters('woocommerce_komoju_method_form_args', $default_args, $this->id));

$data = $this->get_input_field_data();
$method_fields = ['method-field' => $data];
$fields = wp_parse_args($fields, apply_filters('woocommerce_komoju_method_form_fields', $method_fields, $this->id)); ?>
<fieldset id="<?php echo $this->id; ?>-cc-form">
<?php do_action('woocommerce_komoju_method_form_start', $this->id); ?>
<?php
foreach ($fields as $field) {
echo $field;
} ?>
<?php do_action('woocommerce_komoju_method_form_end', $this->id); ?>
<div class="clear"></div>
</fieldset>
<?php
}

private function get_input_field_data()
{
$komoju_client = $this->komoju_api;

try {
$methods = apply_filters('woocommerce_komoju_payment_methods', $komoju_client->paymentMethods());
$page_locale = $this->get_locale_or_fallback();
$name_property = "name_{$page_locale}";

$field_data = '
<p
class="
form-row
form-row-wide
validate-required
woocommerce-validated"
>
' . __('Method of payment:', 'komoju-woocommerce') . '
<abbr
class="required"
title="required"
>*
</abbr>';
foreach ($methods as $method) {
$field_data .= '
<label>
<input
id="' . esc_attr($this->id) . '-method"
class="input-radio"
type="radio"
value="' . esc_attr($method['type_slug']) . '"
name="' . esc_attr($this->id) . '-method"
/>
' . ($method[$name_property]) . '
<br/>
</label>';
}
$field_data .= '</p>';
} catch (KomojuExceptionBadServer | KomojuExceptionBadJson $e) {
$message = $e->getMessage();
$this->log($message);

$field_data = '<p>' . __('Encountered an issue communicating with KOMOJU. Please wait a moment and try again.', 'komoju-woocommerce') . '</p>';
}

return $field_data;
}

public static function get_locale_or_fallback()
{
$fallback_locale = 'en';
Expand All @@ -313,20 +228,6 @@ public static function get_locale_or_fallback()
}
}

/**
* Validate the payment form (for custom fields added)
*/
public function validate_fields()
{
if (!isset($_POST['komoju-method'])) {
wc_add_notice(__('Please select a payment method (how you want to pay)', 'komoju-woocommerce'), 'error');

return false;
}

return true;
}

/**
* We moved a lot of gateway settings into global options.
*
Expand Down
10 changes: 1 addition & 9 deletions tests/cypress/integration/checkout.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ describe('KOMOJU for WooCommerce: Checkout', () => {
cy.contains('Komoju').click();
cy.get('.blockUI,.blockOverlay').should('not.exist');

// This waits for the "expanding box" animation to finish
cy.get('.payment_method_komoju[style]').should('not.exist');

cy.get('#komoju-cc-form').contains('Pay Easy').click();
cy.get('.blockUI,.blockOverlay').should('not.exist');

cy.get('#place_order').click();
cy.contains('Enter Account Holder Details').should('exist');
cy.location('pathname').should('include', '/sessions/');
cy.contains('Select Payment Method').should('exist');
})

it('lets me make a payment using the specialized konbini gateway', () => {
Expand All @@ -41,8 +35,6 @@ describe('KOMOJU for WooCommerce: Checkout', () => {
cy.fillInAddress();

cy.get('#payment_method_komoju_konbini').click();
cy.get('.payment_method_komoju[style="display: none;"]').should('exist');
cy.get('.blockUI,.blockOverlay').should('not.exist');
cy.get('#place_order').click();

cy.contains('Choose a Convenience Store').should('exist');
Expand Down