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

Add tokenization support. #48

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
69 changes: 67 additions & 2 deletions includes/class-wc-gateway-dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class WC_Gateway_Dummy extends WC_Payment_Gateway {
* Constructor for the gateway.
*/
public function __construct() {

$this->icon = apply_filters( 'woocommerce_dummy_gateway_icon', '' );
$this->has_fields = false;
$this->supports = array(
Expand Down Expand Up @@ -121,10 +121,67 @@ public function init_form_fields() {
),
'default' => 'success',
'desc_tip' => true,
)
),
'tokenization' => array(
'title' => __( 'Enable tokenization', 'woocommerce-gateway-dummy' ),
'desc' => __( 'Allow customers to save payment methods for future use.', 'woocommerce-gateway-dummy' ),
'id' => 'woo_dummy_tokenization',
'type' => 'checkbox',
'default' => 'yes',
'desc_tip' => true,
),
);
}

/**
* Initialize the gateway settings from the form fields.
*
* At present this is used to enable tokenization if the setting is enabled
* in the dashboard.
*/
public function init_settings() {
parent::init_settings();

// Tokenization settings.
if ( 'yes' === $this->get_option( 'tokenization' ) ) {
$this->supports[] = 'tokenization';
}
}

/**
* Save the payment method to the database.
*
* @return bool Whether the payment method was saved successfully.
*/
public function add_payment_method() {
$token = new WC_Payment_Token_Dummy();
$token->set_token( 'dummy-' . $this->get_option( 'result' ) );
$token->set_gateway_id( $this->id );
$token->set_user_id( get_current_user_id() );
return $token->save();
}

/**
* Display the payment fields for the shortcode checkout page.
*
* Modifies the payment fields displayed on the checkout page to include
* the any saved payment methods and the option to save a new payment method.
*/
public function payment_fields() {
$description = $this->get_description();

if ( $description ) {
// KSES is ran within get_description, but not here since there may be custom HTML returned by extensions.
echo wpautop( wptexturize( $description ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

if ( $this->supports( 'tokenization' ) && is_checkout() ) {
$this->tokenization_script();
$this->saved_payment_methods();
$this->save_payment_method_checkbox();
}
}

/**
* Process the payment and return the result.
*
Expand Down Expand Up @@ -154,6 +211,14 @@ class_exists( 'WC_Pre_Orders_Order' )
// Remove cart
WC()->cart->empty_cart();

// Add payment method for tokenization.
if (
isset( $_POST['wc-dummy-new-payment-method'] )
&& $_POST['wc-dummy-new-payment-method']
) {
$this->add_payment_method();
}

// Return thankyou redirect
return array(
'result' => 'success',
Expand Down
61 changes: 61 additions & 0 deletions includes/class-wc-payment-token-dummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

class WC_Payment_Token_Dummy extends WC_Payment_Token {

/**
* Token Type.
*
* @var string
*/
protected $type = 'dummy';

/**
* Validate the token.
*
* @return bool True if token is valid.
*/
public function validate () {
if ( false === parent::validate() ) {
return false;
}

if ( ! is_user_logged_in() ) {
return false;
}

$gateways = WC()->payment_gateways->payment_gateways();
if ( ! isset( $gateways[ 'dummy' ] ) ) {
return false;
}

$gateway = $gateways[ 'dummy' ];

if ( ! $gateway->enabled === 'yes' ) {
return false;
}

if ( ! $gateway->supports( 'tokenization' ) ) {
return false;
}

if ( $gateway->get_option( 'result' ) !== 'success' ) {
return false;
}

return true;
}

/**
* Get display name for the token.
*
* @param string $deprecated
* @return string Display name.
*/
public function get_display_name( $deprecated = '' ) {
return sprintf(
/* translators: %s: Payment token ID */
__( 'Dummy Payment Token %s', 'woocommerce-gateway-dummy' ),
$this->get_id() ? '#' . $this->get_id() : ''
);
}
}
28 changes: 28 additions & 0 deletions resources/js/frontend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,37 @@ const defaultLabel = __(
);

const label = decodeEntities( settings.title ) || defaultLabel;

/**
* Content component
*/
const Content = () => {
return decodeEntities( settings.description || '' );
};

const SavedPaymentContent = ( props ) => {
const supportsTokenization = settings.supports.includes( 'tokenization' );

if ( ! supportsTokenization ) {
return null;
}

return (
<div style={
{
border: '1px solid #ccc',
borderTop: 'none',
padding: '1rem',
marginTop: '-16px',
}
}>
<p>
<small>{ __( 'For testing tokenization support', 'woocommerce-gateway-dummy' ) }</small>
</p>
</div>
);
};

/**
* Label component
*
Expand All @@ -35,11 +60,14 @@ const Dummy = {
name: "dummy",
label: <Label />,
content: <Content />,
savedTokenComponent: <SavedPaymentContent />,
edit: <Content />,
canMakePayment: () => true,
ariaLabel: label,
supports: {
features: settings.supports,
showSavedCards: true,
showSaveOption: true,
},
};

Expand Down
30 changes: 30 additions & 0 deletions woocommerce-gateway-dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public static function init() {
// Registers WooCommerce Blocks integration.
add_action( 'woocommerce_blocks_loaded', array( __CLASS__, 'woocommerce_gateway_dummy_woocommerce_block_support' ) );

// Prevent errors on My Account.
add_filter( 'woocommerce_saved_payment_methods_list', array( __CLASS__, 'saved_payment_methods_list' ), 20 );
}

/**
Expand Down Expand Up @@ -73,6 +75,11 @@ public static function add_gateway( $gateways ) {
*/
public static function includes() {

// Make the WC_Payment_Token_Dummy class available.
if ( class_exists( 'WC_Payment_Token' ) ) {
require_once 'includes/class-wc-payment-token-dummy.php';
}

// Make the WC_Gateway_Dummy class available.
if ( class_exists( 'WC_Payment_Gateway' ) ) {
require_once 'includes/class-wc-gateway-dummy.php';
Expand Down Expand Up @@ -112,6 +119,29 @@ function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_
);
}
}

/**
* Prevent errors on My Account.
*
* The My Account page will throw and error if the payment method does
* not include a brand. This method adds a brand to the Dummy Payment Token.
*
* Runs on `woocommerce_saved_payment_methods_list, 20` filter.
*
* @param array $tokens
* @return array
*/
public static function saved_payment_methods_list( $tokens ) {
if ( empty( $tokens['dummy'] ) ) {
return $tokens;
}

foreach ( $tokens['dummy'] as &$token ) {
$token['method']['brand'] = __( 'Dummy Payment Token', 'woocommerce-gateway-dummy' );
}

return $tokens;
}
}

WC_Dummy_Payments::init();
Loading