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

Mollie components #40

Merged
merged 35 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
be1f885
Add Mollie Components field to credit card payment method.
rvdsteege Nov 21, 2022
c94f899
Make Mollie Components work with WooCommerce and test meta box.
rvdsteege Nov 22, 2022
d704351
Only render components field for WooCommerce and test meta box.
rvdsteege Nov 28, 2022
59bd473
Update components field script and style.
rvdsteege Nov 28, 2022
4ffd4ab
Merge branch 'main' into mollie-components
rvdsteege Mar 29, 2023
79a1cc5
Merge branch 'main' into mollie-components
remcotolsma Nov 30, 2023
d854fc0
Merge branch 'main' into mollie-components
remcotolsma Nov 30, 2023
c7131ad
Merge branch 'main' into mollie-components
remcotolsma Nov 30, 2023
a4e499d
Merge branch 'main' into mollie-components
remcotolsma Nov 30, 2023
bf47a2e
Update ComponentsField.php
remcotolsma Nov 30, 2023
27e08e9
Script didn't work for me, fix it.
remcotolsma Nov 30, 2023
79861d5
Simplify Mollie card component.
remcotolsma Dec 1, 2023
506d8ed
wip
remcotolsma Dec 1, 2023
830bfc9
Update mollie.js
remcotolsma Dec 1, 2023
3e46792
Update mollie.js
remcotolsma Dec 1, 2023
c5d1fc5
Update mollie.js
remcotolsma Dec 1, 2023
31c29a4
Update mollie.js
remcotolsma Dec 1, 2023
b11b29a
Update .wp-env.json
remcotolsma Dec 4, 2023
e4f6c51
Simplify Mollie components, for now only WooCommerce support.
remcotolsma Dec 5, 2023
aa3e3d3
Remove styling for now.
remcotolsma Dec 5, 2023
eff6a41
Delete postcss.config.js
remcotolsma Dec 5, 2023
a7a16ea
PHPStan fixes.
remcotolsma Dec 5, 2023
803c797
Add styling support.
remcotolsma Dec 5, 2023
8c1fa3d
Improve base styling of card field.
remcotolsma Dec 6, 2023
3c9cbee
Improve error handling.
remcotolsma Dec 6, 2023
9f05c44
build
remcotolsma Dec 6, 2023
ce329ea
Fix Lint JS script.
remcotolsma Dec 6, 2023
5f779e0
Update phpcs.xml.dist
remcotolsma Dec 6, 2023
6d8eb24
Update composer.json
remcotolsma Dec 6, 2023
d4a626c
Fix typo.
rvdsteege Dec 6, 2023
b03b489
Fix typos.
rvdsteege Dec 6, 2023
7932800
No longer submit Mollie error to WooCommerce checkout form.
remcotolsma Dec 6, 2023
494e0be
Update Mollie profile ID in config meta instead of transient.
rvdsteege Dec 6, 2023
51ac65d
Merge branch 'mollie-components' of https://github.com/pronamic/wp-pr…
rvdsteege Dec 6, 2023
80ab156
Improve error styling.
remcotolsma Dec 6, 2023
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
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' => 'c1e1308986d4d65f5f9d');
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';
34 changes: 34 additions & 0 deletions assets/src/card-field.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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: #f00;
}
}

.mollie-component {
background: #fff;

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

padding: .5em;
}
}
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": "[email protected]",
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 <[email protected]>
* @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
Loading