Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Server side payment method registration
Browse files Browse the repository at this point in the history
  • Loading branch information
nerrad committed Mar 5, 2020
1 parent c8dc538 commit d00ffa2
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/BlockTypes/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ public function render( $attributes = array(), $content = '' ) {
if ( ! $data_registry->exists( 'cartData' ) ) {
$data_registry->add( 'cartData', WC()->api->get_endpoint_data( '/wc/store/cart' ) );
}
do_action( 'woocommerce_blocks_enqueue_cart_block_scripts_before' );
\Automattic\WooCommerce\Blocks\Assets::register_block_script(
$this->block_name . '-frontend',
$this->block_name . '-block-frontend'
);
do_action( 'woocommerce_blocks_enqueue_cart_block_scripts_after' );
return $content . $this->get_skeleton();
}

Expand Down
3 changes: 3 additions & 0 deletions src/BlockTypes/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public function render( $attributes = array(), $content = '' ) {
if ( ! $data_registry->exists( 'defaultAddressFields' ) ) {
$data_registry->add( 'defaultAddressFields', WC()->countries->get_default_address_fields() );
}
do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_before' );
\Automattic\WooCommerce\Blocks\Assets::register_block_script( $this->block_name . '-frontend', $this->block_name . '-block-frontend' );
do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after' );

return $content;
}
}
32 changes: 32 additions & 0 deletions src/Domain/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Automattic\WooCommerce\Blocks\Library;
use Automattic\WooCommerce\Blocks\Registry\Container;
use Automattic\WooCommerce\Blocks\RestApi;
use Automattic\WooCommerce\Blocks\PaymentMethodIntegrations\Stripe;

/**
* Takes care of bootstrapping the plugin.
Expand Down Expand Up @@ -89,6 +90,10 @@ function( Container $container ) {
// load AssetDataRegistry.
$this->container->get( AssetDataRegistry::class );

// @todo this will eventually get moved into the relevant payment
// extensions
$this->load_payment_method_integrations();

Library::init();
OldAssets::init();
RestApi::init();
Expand Down Expand Up @@ -163,4 +168,31 @@ protected function define_feature_flag() {
}
define( 'WOOCOMMERCE_BLOCKS_PHASE', $flag );
}

/**
* This is a temporary method that is used for setting up payment method
* integrations with Cart and Checkout blocks. This logic should get moved
* to the payment gateway extensions.
*/
protected function load_payment_method_integrations() {
// stripe registration.
$this->container->register(
Stripe::class,
function( Container $container ) {
$asset_data_registry = $container->get( AssetDataRegistry::class );
return new Stripe( $asset_data_registry );
}
);
add_action(
'plugins_loaded',
function() {
if ( class_exists( 'WC_Stripe' ) ) {
// initialize hooking into blocks.
$stripe = $this->container->get( Stripe::class );
$stripe->register_assets();
}
},
15
);
}
}
157 changes: 157 additions & 0 deletions src/PaymentMethodIntegrations/Stripe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
/**
* Temporary integration of the stripe payment method for the new cart and
* checkout blocks. Once the api is demonstrated to be stable, this integration
* will be moved to the Stripe extension
*
* @package WooCommerce/Blocks
* @since $VID:$
*/

namespace Automattic\WooCommerce\Blocks\PaymentMethodIntegrations;

use Exception;
use WC_Stripe_Payment_Request;
use WC_Stripe_Helper;
use Automattic\WooCommerce\Blocks\Assets\Api;
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;

/**
* Stripe payment method integration
*
* @since $VID:$
*/
class Stripe {

/**
* An instance of the AssetDataRegistry
*
* @var AssetDataRegistry
*/
private $asset_registry;

/**
* Stripe settings from the WP options table
*
* @var array
*/
private $stripe_settings;

/**
* Constructor for the class
*
* @param AssetDataRegistry $asset_registry Used for registering data
* to pass along to the request.
*/
public function __construct( AssetDataRegistry $asset_registry ) {
$this->asset_registry = $asset_registry;
$this->stripe_settings = get_option( 'woocommerce_stripe_settings', [] );
}

/**
* When called registers the stripe handle for enqueueing with cart and
* checkout blocks.
* Note: this assumes the stripe extension has registered this script.
* This will also ensure stripe data is loaded with the blocks.
*/
public function register_assets() {
add_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_before', [ $this, 'enqueue_stripe_and_data' ] );
add_action( 'woocommerce_blocks_enqueue_cart_block_scripts_before', [ $this, 'enqueue_stripe_and_data' ] );
}

/**
* When called, registers a stripe data object (with 'stripe_data' as the
* key) for including everywhere stripe integration happens.
*/
public function enqueue_data() {

This comment has been minimized.

Copy link
@haszari

haszari Mar 8, 2020

Member

Minor nitpick - data is a bit vague, would you consider this the stripe config for the store (some of it is, e.g. the API key)? Similarly on the front end, stripe_data could be clearer.

This comment has been minimized.

Copy link
@nerrad

nerrad Mar 8, 2020

Author Contributor

You mean enqueue_stripe_data as opposed to enqueue_data here? Keep in mind this method is already namespaced by being in the Automattic\WooCommerce\Blocks\PaymentMethodIntegrations\Stripe class. So it's fully qualified name is Automattic\WooCommerce\Blocks\PaymentMethodIntegrations\Stripe::enqueue_data. So if you are suggesting expanding on the method name I think that's redundant.

This comment has been minimized.

Copy link
@haszari

haszari Mar 8, 2020

Member

It's more just the word "data" - if this can be made more descriptive it might be helpful. E.g. if this is all config, then enqueue_config would be clearer. What kind of data is it?

This comment has been minimized.

Copy link
@nerrad

nerrad Mar 9, 2020

Author Contributor

Does it matter in this case if the function declares what kind of data this is? I read it and would describe it as enqueueing Stripe data from the server, hence the enqueue_data and the phpdoc "registers a stripe data object". There's nothing special about this data other than it is data being transferred from the server to the client. If we had multiple different "types" of data then maybe it would make sense to name it something different and split things up, but I'm wary of overthinking this.

There's value in being extra specific in function names when it gives more semantic meaning for a purpose (eg. This particular function enqueues data of type a, but this other function enqueues data of type b) , but the extra specificity in this case doesn't really seem to be needed?

$data = [
'stripeTotalLabel' => $this->get_total_label(),
'publicKey' => $this->get_publishable_key(),
'allowPrepaidCard' => $this->get_allow_prepaid_card(),
'button' => [
'type' => $this->get_button_type(),
'theme' => $this->get_button_theme(),
'height' => $this->get_button_height(),
'locale' => $this->get_button_locale(),
],
];
if ( ! $this->asset_registry->exists( 'stripe_data' ) ) {
$this->asset_registry->add( 'stripe_data', $data );

This comment has been minimized.

Copy link
@nerrad

nerrad Mar 8, 2020

Author Contributor

Based on your previous comment Rua, maybe you missed that the data is being registered for the frontend on a stripe_data key?

}
}

/**
* Callback hooked into cart and checkout block script enqueue action.
*/
public function enqueue_stripe_and_data() {
wp_enqueue_script( 'stripe', 'https://js.stripe.com/v3/', '', '3.0', true );
$this->enqueue_data();
}

/**
* Returns the label to use accompanying the total in the stripe statement.
*
* @return string Statement descriptor
*/
private function get_total_label() {
return ! empty( $this->stripe_settings['statement_descriptor'] ) ? WC_Stripe_Helper::clean_statement_descriptor( $this->stripe_settings['statement_descriptor'] ) : '';
}

/**
* Returns the publishable api key for the Stripe service.
*
* @return string Public api key.
*/
private function get_publishable_key() {
$test_mode = ( ! empty( $this->stripe_settings['testmode'] ) && 'yes' === $this->stripe_settings['testmode'] );
if ( $test_mode ) {
return ! empty( $this->stripe_settings['test_publishable_key'] ) ? $this->stripe_settings['test_publishable_key'] : '';
}
return ! empty( $this->stripe_settings['publishable_key'] ) ? $this->stripe_settings['publishable_key'] : '';
}

/**
* Returns whether to allow prepaid cards for payments.
*
* @return bool True means to allow prepaid card (default)
*/
private function get_allow_prepaid_card() {
return apply_filters( 'wc_stripe_allow_prepaid_card', true );
}

/**
* Return the button type for the payment button.
*
* @return string Defaults to 'default'
*/
private function get_button_type() {
return isset( $this->stripe_settings['payment_request_button_type'] ) ? $this->stripe_settings['payment_request_button_type'] : 'default';
}

/**
* Return the theme to use for the payment button.
*
* @return string Defaults to 'dark'.
*/
private function get_button_theme() {
return isset( $this->stripe_settings['payment_request_button_theme'] ) ? $this->stripe_settings['payment_request_button_theme'] : 'dark';
}

/**
* Return the height for the payment button.
*
* @return string A pixel value for the hight (defaults to '64')
*/
private function get_button_height() {
return isset( $this->stripe_settings['payment_request_button_height'] ) ? str_replace( 'px', '', $this->stripe_settings['payment_request_button_height'] ) : '64';
}

/**
* Return the locale for the payment button.
*
* @return string Defaults to en_US.
*/
private function get_button_locale() {
return apply_filters( 'wc_stripe_payment_request_button_locale', substr( get_locale(), 0, 2 ) );
}
}

0 comments on commit d00ffa2

Please sign in to comment.