Skip to content

Commit

Permalink
Merge pull request #40 from pronamic/mollie-components
Browse files Browse the repository at this point in the history
Mollie components
  • Loading branch information
remcotolsma committed Dec 6, 2023
2 parents 2a1aca6 + 80ab156 commit 4c7fd4c
Show file tree
Hide file tree
Showing 17 changed files with 499 additions and 15 deletions.
1 change: 1 addition & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"https://downloads.wordpress.org/plugin/query-monitor.zip",
"https://downloads.wordpress.org/plugin/log-http-requests.zip",
"https://downloads.wordpress.org/plugin/one-time-login.zip",
"https://downloads.wordpress.org/plugin/woocommerce.zip",
"https://downloads.wordpress.org/plugin/wp-plugin-dependencies.zip"
],
"mappings": {
Expand Down
1 change: 1 addition & 0 deletions assets/dist/card-field.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array(), 'version' => 'da91ceff221d2b17ebc7');
1 change: 1 addition & 0 deletions assets/dist/card-field.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added assets/dist/card-field.js
Empty file.
1 change: 1 addition & 0 deletions assets/dist/wc-legacy-checkout.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array(), 'version' => 'f465b8f2332f2d0a9b79');
1 change: 1 addition & 0 deletions assets/dist/wc-legacy-checkout.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions assets/src/card-field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './card-field.scss';
38 changes: 38 additions & 0 deletions assets/src/card-field.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Card field style.
*
* @link https://github.com/mollie/components-examples
*/
.pronamic-pay-mollie-card-field {
display: flex;

flex-flow: row wrap;

gap: 1em;

.mollie-card-component {
flex: 2 1 100%;

&--expiryDate,
&--verificationCode {
flex: 1 2 auto;
}

&__error {
color: #a00;
}
}

.mollie-component {
background: #fff;

border: 1px solid #767676;
border-radius: 3px;

padding: .5em;

&.is-invalid {
border-color: #a00;
}
}
}
161 changes: 161 additions & 0 deletions assets/src/wc-legacy-checkout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/* global Mollie */
/* eslint-env jquery */

/**
* Pronamic Pay Mollie WooCommerce legacy checkout form controller class
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_classes
*/
class PronamicPayMollieWooCommerceLegacyCheckoutFormController {
/**
* Construct Pronamic Pay Mollie WooCommerce legacy checkout form controller.
*
* @param {jQuery} jQuery The jQuery library.
* @param {HTMLElement} body Body element.
* @param {HTMLElement} form WooCommerce legacy checkout form element.
*/
constructor( jQuery, body, form ) {
this.jQuery = jQuery;
this.body = body;
this.form = form;
}

/**
* Setup.
*/
setup() {
this.jQuery( this.body ).on( 'init_checkout', () =>
this.initCheckout()
);
}

/**
* Init checkout.
*
* @see https://github.com/woocommerce/woocommerce/blob/8.3.0/plugins/woocommerce/client/legacy/js/frontend/checkout.js#L56-L59
*/
initCheckout() {
const cardElement = this.form.querySelector(
'.pronamic-pay-mollie-card-field'
);

if ( null === cardElement ) {
return;
}

const mollieProfileId = cardElement.dataset.mollieProfileId;
const mollieOptions = JSON.parse( cardElement.dataset.mollieOptions );

this.mollie = Mollie( mollieProfileId, mollieOptions );

this.checkoutPlaceOrderListener = ( event, wcCheckoutForm ) =>
this.checkoutPlaceOrder( event, wcCheckoutForm );

this.jQuery( this.form ).on(
'checkout_place_order',
this.checkoutPlaceOrderListener
);

this.jQuery( this.body ).on( 'updated_checkout', () =>
this.updatedCheckout()
);

this.mollieCardComponent = this.mollie.createComponent( 'card' );
}

/**
* Updated checkout.
*
* @see https://github.com/woocommerce/woocommerce/blob/8.3.0/plugins/woocommerce/client/legacy/js/frontend/checkout.js#L428-L429
*/
updatedCheckout() {
if ( this.cardElement ) {
this.mollieCardComponent.unmount();
}

this.cardElement = this.form.querySelector(
'.pronamic-pay-mollie-card-field'
);

if ( null === this.cardElement ) {
return;
}

this.mollieCardComponent.mount( this.cardElement );
}

/**
* Checkout place order.
*
* @see https://github.com/woocommerce/woocommerce/blob/8.3.0/plugins/woocommerce/client/legacy/js/frontend/checkout.js#L478-L480
* @param {jQuery.Event} event A `jQuery.Event` object.
* @param {Object} wcCheckoutForm WooCommerce checkout form object.
*/
checkoutPlaceOrder( event, wcCheckoutForm ) {
if (
'pronamic_pay_credit_card' !== wcCheckoutForm.get_payment_method()
) {
return true;
}

this.mollie
.createToken()
.then( ( result ) => this.processTokenResponse( result ) );

return false;
}

/**
* Process token response.
*
* @param {Object} result Mollie create token repsonse object.
*/
processTokenResponse( result ) {
if ( result.error ) {
return;
}

const tokenElement = document.getElementById(
'pronamic_pay_mollie_card_token'
);

if ( tokenElement ) {
tokenElement.value = result.token;
}

this.jQuery( this.form ).off(
'checkout_place_order',
this.checkoutPlaceOrderListener
);

this.jQuery( this.form ).submit();

this.jQuery( this.form ).on(
'checkout_place_order',
this.checkoutPlaceOrderListener
);
}
}

/**
* Initialization.
*/
( function () {
if ( ! jQuery ) {
return;
}

if ( ! document.forms.checkout ) {
return;
}

const controller =
new PronamicPayMollieWooCommerceLegacyCheckoutFormController(
jQuery,
document.body,
document.forms.checkout
);

controller.setup();
} )();
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
}
},
"config": {
"platform": {
"php": "8.0"
},
"platform-check": false,
"sort-packages": true,
"allow-plugins": {
"composer/installers": true,
Expand Down
39 changes: 24 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@
"name": "mollie",
"version": "4.8.1",
"description": "Mollie driver for the WordPress payment processing library.",
"repository": {
"type": "git",
"url": "https://github.com/wp-pay-gateways/mollie"
},
"keywords": [
"wordpress",
"wp",
"pay",
"mollie",
"gateway",
"pronamic"
],
"author": {
"name": "Pronamic",
"email": "info@pronamic.nl",
Expand All @@ -26,16 +14,37 @@
"url": "http://www.remcotolsma.nl/"
}
],
"license": "GPL-3.0",
"license": "GPL-2.0-or-later",
"keywords": [
"wordpress",
"wp",
"pay",
"mollie",
"gateway",
"pronamic"
],
"homepage": "http://www.wp-pay.org/gateways/mollie/",
"repository": {
"type": "git",
"url": "https://github.com/wp-pay-gateways/mollie"
},
"bugs": {
"url": "https://github.com/wp-pay-gateways/mollie/issues"
},
"homepage": "http://www.wp-pay.org/gateways/mollie/",
"devDependencies": {
"@wordpress/env": "^8.13.0",
"npm-run-all": "^4.1.5"
"@wordpress/prettier-config": "^3.4.0",
"@wordpress/scripts": "^26.18.0",
"npm-run-all": "^4.1.5",
"prettier": "npm:wp-prettier@^3.0.3"
},
"prettier": "@wordpress/prettier-config",
"scripts": {
"build": "wp-scripts build assets/src/card-field.js assets/src/wc-legacy-checkout.js --output-path=assets/dist",
"start": "wp-scripts start assets/src/card-field.js assets/src/wc-legacy-checkout.js --output-path=assets/dist",
"lint-js": "wp-scripts lint-js assets/src",
"lint:pkg-json": "wp-scripts lint-pkg-json",
"lint:style": "wp-scripts lint-style 'assets/src/**/*.scss'",
"wp-env-setup": "npm-run-all wp-env-setup-*",
"wp-env-setup-mollie": "wp-env run cli wp config set MOLLIE_API_KEY $MOLLIE_API_KEY",
"wp-env-setup-buckaroo-website-key": "wp-env run cli wp config set BUCKAROO_WEBSITE_KEY $BUCKAROO_WEBSITE_KEY",
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<ruleset name="WordPress Pay Mollie rules">
<file>.</file>

<exclude-pattern>assets/dist/*.asset.php</exclude-pattern>
<exclude-pattern>tests/bootstrap.php</exclude-pattern>
<exclude-pattern>tests/wp-config.php</exclude-pattern>

Expand Down
97 changes: 97 additions & 0 deletions src/CardField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Mollie card field
*
* @author Pronamic <info@pronamic.eu>
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay\Mollie
*/

namespace Pronamic\WordPress\Pay\Gateways\Mollie;

use Pronamic\WordPress\Html\Element;
use Pronamic\WordPress\Pay\Fields\Field;

/**
* Mollie card field class
*/
class CardField extends Field {
/**
* Gateway.
*
* @var Gateway
*/
private $gateway;

/**
* Construct card field.
*
* @param string $id ID.
* @param Gateway $gateway Gateway.
*/
public function __construct( $id, Gateway $gateway ) {
parent::__construct( $id );

$this->gateway = $gateway;
}

/**
* Get element.
*
* @return Element|null
*/
protected function get_element() {
try {
$profile_id = $this->gateway->get_profile_id();
} catch ( \Exception $e ) {
return null;
}

$locale_transformer = new LocaleTransformer();

\wp_enqueue_script( 'pronamic-pay-mollie' );

\wp_enqueue_style( 'pronamic-pay-mollie' );

$element = new Element( 'div' );

$element->children[] = new Element(
'div',
[

Check failure on line 61 in src/CardField.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Parameter #2 $attributes of class Pronamic\WordPress\Html\Element constructor expects array<string, bool|float|int|string>, array<string, string|false|null> given.
'class' => 'pronamic-pay-mollie-card-field',
'data-mollie-profile-id' => $profile_id,
'data-mollie-options' => \wp_json_encode(
[
'locale' => $locale_transformer->transform_wp_to_mollie( \get_locale() ),
'testmode' => ( 'test' === $this->gateway->get_mode() ),
]
),
]
);

$element->children[] = new Element(
'input',
[
'id' => $this->get_id(),
'name' => $this->get_id(),
'type' => 'hidden',
]
);

return $element;
}

/**
* Serialize to JSON.
*
* @return array
*/
public function jsonSerialize(): array {

Check failure on line 90 in src/CardField.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Method Pronamic\WordPress\Pay\Gateways\Mollie\CardField::jsonSerialize() return type has no value type specified in iterable type array.
$data = parent::jsonSerialize();

$data['type'] = 'html';

return $data;
}
}
Loading

0 comments on commit 4c7fd4c

Please sign in to comment.