Skip to content

Commit

Permalink
Merge pull request #4 from pronamic/orders-api-for-klarna
Browse files Browse the repository at this point in the history
Add support for Klarna methods with Mollie Orders API
  • Loading branch information
remcotolsma authored Jun 30, 2022
2 parents 7aef404 + d2b3c25 commit 8444f3a
Show file tree
Hide file tree
Showing 59 changed files with 2,654 additions and 674 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@
# WordPress
/wordpress/
/wp-content/

# PHPUnit
/.phpunit.result.cache
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,22 @@
},
"require": {
"php": ">=5.6.20",
"ext-json": "*",
"justinrainbow/json-schema": "^5.2",
"pronamic/wp-http": "^1.0",
"woocommerce/action-scheduler": "^3.4",
"wp-pay/core": "^4.0"
"wp-pay/core": "^4.3"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4",
"overtrue/phplint": "^2.3",
"php-coveralls/php-coveralls": "^2.4",
"phpmd/phpmd": "^2.7",
"phpunit/phpunit": "^5.7 || ^6.0",
"pronamic/wp-coding-standards": "^1.0",
"roots/wordpress": "^5.8",
"roots/wordpress": "^5.9",
"wp-cli/wp-cli": "^2.3",
"wp-phpunit/wp-phpunit": "^5.8"
"wp-phpunit/wp-phpunit": "^6.0",
"yoast/phpunit-polyfills": "^1.0"
},
"scripts": {
"ci": [
Expand Down
21 changes: 21 additions & 0 deletions json-schemas/mandate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$id": "https://github.com/wp-pronamic-pay-mollie/blob/master/json-schemas/mandate.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Mollie mandate.",
"type": "object",
"properties": {
"resource": {
"description": "Indicates the response contains a mandate object. Will always contain `mandate` for this endpoint.",
"type": "string",
"const": "mandate"
},
"id": {
"description": "The identifier uniquely referring to this mandate.",
"type": "string"
}
},
"required": [
"resource",
"id"
]
}
27 changes: 27 additions & 0 deletions json-schemas/order.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$id": "https://github.com/wp-pronamic-pay-mollie/blob/master/json-schemas/order.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Mollie order.",
"type": "object",
"properties": {
"resource": {
"description": "Indicates the response contains an order object. Will always contain `order` for this endpoint.",
"type": "string",
"const": "order"
},
"id": {
"description": "The order’s unique identifier.",
"type": "string"
},
"status": {
"description": "The status of the order.",
"type": "string",
"enum": [ "created", "paid", "authorized", "canceled", "shipping", "completed", "expired" ]
}
},
"required": [
"resource",
"id",
"status"
]
}
21 changes: 21 additions & 0 deletions json-schemas/shipment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$id": "https://github.com/wp-pronamic-pay-mollie/blob/master/json-schemas/shipment.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Mollie shipment.",
"type": "object",
"properties": {
"resource": {
"description": "Indicates the response contains a shipment object. Will always contain `shipment` for this endpoint.",
"type": "string",
"const": "shipment"
},
"id": {
"description": "The shipment’s unique identifier.",
"type": "string"
}
},
"required": [
"resource",
"id"
]
}
6 changes: 1 addition & 5 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
includes:
- vendor-bin/phpstan/vendor/szepeviktor/phpstan-wordpress/extension.neon
parameters:
customRulesetUsed: false
level: max
level: 8
bootstrapFiles:
- tests/phpstan/bootstrap.php
paths:
- src/
scanDirectories:
- vendor/wp-cli
- wp-content/plugins/action-scheduler
ignoreErrors:
- '#^Function apply_filters invoked with \d parameters, 2 required\.$#'
35 changes: 14 additions & 21 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory suffix=".php">tests/src</directory>
</testsuite>
</testsuites>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>

<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<testsuites>
<testsuite name="pronamic-pay-mollie">
<directory suffix=".php">tests/src</directory>
</testsuite>
</testsuites>

<php>
<env name="WP_PHPUNIT__TESTS_CONFIG" value="tests/wp-config.php" />
</php>
<php>
<env name="WP_PHPUNIT__TESTS_CONFIG" value="tests/wp-config.php"/>
</php>
</phpunit>
222 changes: 222 additions & 0 deletions src/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<?php
/**
* Address
*
* @author Pronamic <info@pronamic.eu>
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay\Gateways\Mollie
*/

namespace Pronamic\WordPress\Pay\Gateways\Mollie;

use InvalidArgumentException;
use JsonSerializable;
use Pronamic\WordPress\Pay\Address as Core_Address;

/**
* Address class
*
* @link https://docs.mollie.com/reference/v2/orders-api/create-order
* @link https://docs.mollie.com/overview/common-data-types#address-object
*/
class Address implements JsonSerializable {
/**
* The person’s organization, if applicable.
*
* @var string|null
*/
private ?string $organization_name = null;

/**
* The title of the person, for example Mr. or Mrs.
*
* @var string|null
*/
private ?string $title = null;

/**
* The given name (first name) of the person.
*
* @var string
*/
private string $given_name;

/**
* Organization name.
*
* @var string
*/
private string $family_name;

/**
* The email address of the person.
*
* @var string
*/
private string $email;

/**
* The phone number of the person. Some payment methods require this information. If
* you have it, you should pass it so that your customer does not have to enter it again
* in the checkout. Must be in the E.164 format. For example +31208202070.
*
* @link https://en.wikipedia.org/wiki/E.164
* @var string|null
*/
private ?string $phone = null;

/**
* Street and number.
*
* @var string
*/
private string $street_and_number;

/**
* Additional street details.
*
* @var string|null
*/
private ?string $street_additional = null;

/**
* Postal code.
*
* @var string|null
*/
private ?string $postal_code = null;

/**
* City.
*
* @var string
*/
private string $city;

/**
* Region.
*
* @var string|null
*/
private ?string $region = null;

/**
* The country of the address in ISO 3166-1 alpha-2 format.
*
* @link https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
* @var string
*/
private string $country;

/**
* Construct address.
*
* @param string $given_name Given name.
* @param string $family_name Family name.
* @param string $email Email address.
* @param string $street_and_number Street and house number.
* @param string $city City.
* @param string $country Country.
* @throws InvalidArgumentException Throws exception on invalid arguments.
*/
public function __construct( string $given_name, string $family_name, string $email, string $street_and_number, string $city, string $country ) {
/*
* The two-character country code of the address.
*
* The permitted country codes are defined in ISO-3166-1 alpha-2 (e.g. 'NL').
*/
if ( 2 !== strlen( $country ) ) {
throw new InvalidArgumentException(
sprintf(
'Given country `%s` not ISO 3166-1 alpha-2 value.',
$country
)
);
}

// Ok.
$this->given_name = $given_name;
$this->family_name = $family_name;
$this->email = $email;
$this->street_and_number = $street_and_number;
$this->city = $city;
$this->country = $country;
}

/**
* Transform from WordPress Pay core address.
*
* @param Core_Address $address Address.
* @return Address
* @throws InvalidArgumentException Throws exception on invalid arguments.
*/
public static function from_wp_address( Core_Address $address ): Address {
$name = $address->get_name();

$given_name = null === $name ? null : $name->get_first_name();
$family_name = null === $name ? null : $name->get_last_name();
$email = $address->get_email();
$street_and_number = $address->get_line_1();
$city = $address->get_city();
$country = $address->get_country_code();

if ( null === $given_name ) {
throw new InvalidArgumentException( 'Mollie requires a given name in an address.' );
}

if ( null === $family_name ) {
throw new InvalidArgumentException( 'Mollie requires a family name in an address.' );
}

if ( null === $email ) {
throw new InvalidArgumentException( 'Mollie requires an email in an address.' );
}

if ( null === $street_and_number ) {
throw new InvalidArgumentException( 'Mollie requires a street and number in an address.' );
}

if ( null === $city ) {
throw new InvalidArgumentException( 'Mollie requires a city in an address.' );
}

if ( null === $country ) {
throw new InvalidArgumentException( 'Mollie requires a country in an address.' );
}

$mollie_address = new self( $given_name, $family_name, $email, $street_and_number, $city, $country );

$mollie_address->organization_name = $address->get_company_name();
$mollie_address->phone = $address->get_phone();
$mollie_address->street_additional = $address->get_line_2();
$mollie_address->postal_code = $address->get_postal_code();
$mollie_address->region = $address->get_region();

return $mollie_address;
}

/**
* JSON serialize.
*
* @return mixed
*/
public function jsonSerialize() {
$object_builder = new ObjectBuilder();

$object_builder->set_optional( 'organizationName', $this->organization_name );
$object_builder->set_optional( 'title', $this->title );
$object_builder->set_required( 'givenName', $this->given_name );
$object_builder->set_required( 'familyName', $this->family_name );
$object_builder->set_required( 'email', $this->email );
$object_builder->set_optional( 'phone', $this->phone );
$object_builder->set_required( 'streetAndNumber', $this->street_and_number );
$object_builder->set_optional( 'streetAdditional', $this->street_additional );
$object_builder->set_optional( 'postalCode', $this->postal_code );
$object_builder->set_required( 'city', $this->city );
$object_builder->set_optional( 'region', $this->region );
$object_builder->set_required( 'country', $this->country );

return $object_builder->jsonSerialize();
}
}
Loading

0 comments on commit 8444f3a

Please sign in to comment.