From f3a7cdf00b519ac1016b96e5dde98470e80007ab Mon Sep 17 00:00:00 2001 From: Laurens Laman Date: Wed, 24 Apr 2019 15:25:37 +0200 Subject: [PATCH] Add onboarding endpoint (#382) Big thanks to @LauLaman ! --- src/Endpoints/ChargebackEndpoint.php | 2 +- src/Endpoints/CollectionEndpointAbstract.php | 18 ++++ src/Endpoints/CustomerEndpoint.php | 2 +- src/Endpoints/CustomerPaymentsEndpoint.php | 4 +- src/Endpoints/EndpointAbstract.php | 10 --- src/Endpoints/InvoiceEndpoint.php | 4 +- src/Endpoints/MandateEndpoint.php | 4 +- src/Endpoints/MethodEndpoint.php | 2 +- src/Endpoints/OnboardingEndpoint.php | 72 ++++++++++++++++ src/Endpoints/OrderEndpoint.php | 2 +- src/Endpoints/OrderLineEndpoint.php | 2 +- src/Endpoints/OrderPaymentEndpoint.php | 2 +- src/Endpoints/OrderRefundEndpoint.php | 2 +- src/Endpoints/OrganizationEndpoint.php | 2 +- src/Endpoints/PaymentCaptureEndpoint.php | 2 +- src/Endpoints/PaymentChargebackEndpoint.php | 2 +- src/Endpoints/PaymentEndpoint.php | 2 +- src/Endpoints/PaymentRefundEndpoint.php | 2 +- src/Endpoints/PermissionEndpoint.php | 2 +- src/Endpoints/ProfileEndpoint.php | 4 +- src/Endpoints/ProfileMethodEndpoint.php | 2 +- src/Endpoints/RefundEndpoint.php | 2 +- src/Endpoints/SettlementsEndpoint.php | 4 +- src/Endpoints/ShipmentEndpoint.php | 2 +- src/Endpoints/SubscriptionEndpoint.php | 4 +- src/MollieApiClient.php | 10 ++- src/Resources/Onboarding.php | 70 ++++++++++++++++ src/Types/OnboardingStatus.php | 21 +++++ .../API/Endpoints/OnboardingEndpointTest.php | 82 +++++++++++++++++++ tests/Mollie/API/Resources/OnboardingTest.php | 42 ++++++++++ 30 files changed, 342 insertions(+), 39 deletions(-) create mode 100644 src/Endpoints/CollectionEndpointAbstract.php create mode 100644 src/Endpoints/OnboardingEndpoint.php create mode 100644 src/Resources/Onboarding.php create mode 100644 src/Types/OnboardingStatus.php create mode 100644 tests/Mollie/API/Endpoints/OnboardingEndpointTest.php create mode 100644 tests/Mollie/API/Resources/OnboardingTest.php diff --git a/src/Endpoints/ChargebackEndpoint.php b/src/Endpoints/ChargebackEndpoint.php index 189ee67c..48add549 100644 --- a/src/Endpoints/ChargebackEndpoint.php +++ b/src/Endpoints/ChargebackEndpoint.php @@ -6,7 +6,7 @@ use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; -class ChargebackEndpoint extends EndpointAbstract +class ChargebackEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "chargebacks"; diff --git a/src/Endpoints/CollectionEndpointAbstract.php b/src/Endpoints/CollectionEndpointAbstract.php new file mode 100644 index 00000000..83afe756 --- /dev/null +++ b/src/Endpoints/CollectionEndpointAbstract.php @@ -0,0 +1,18 @@ +page( null, null, $parameters); } -} \ No newline at end of file +} diff --git a/src/Endpoints/MandateEndpoint.php b/src/Endpoints/MandateEndpoint.php index e9fc8f36..302ccac7 100644 --- a/src/Endpoints/MandateEndpoint.php +++ b/src/Endpoints/MandateEndpoint.php @@ -7,7 +7,7 @@ use Mollie\Api\Resources\Mandate; use Mollie\Api\Resources\MandateCollection; -class MandateEndpoint extends EndpointAbstract +class MandateEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "customers_mandates"; @@ -147,4 +147,4 @@ public function revokeForId($customerId, $mandateId, $data = []) return parent::rest_delete($mandateId, $data); } -} \ No newline at end of file +} diff --git a/src/Endpoints/MethodEndpoint.php b/src/Endpoints/MethodEndpoint.php index 90dd7169..92e44233 100644 --- a/src/Endpoints/MethodEndpoint.php +++ b/src/Endpoints/MethodEndpoint.php @@ -7,7 +7,7 @@ use Mollie\Api\Resources\MethodCollection; use Mollie\Api\Resources\ResourceFactory; -class MethodEndpoint extends EndpointAbstract +class MethodEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "methods"; diff --git a/src/Endpoints/OnboardingEndpoint.php b/src/Endpoints/OnboardingEndpoint.php new file mode 100644 index 00000000..e7ca7288 --- /dev/null +++ b/src/Endpoints/OnboardingEndpoint.php @@ -0,0 +1,72 @@ +client); + } + + /** + * Retrieve the organization's onboarding status from Mollie. + * + * Will throw a ApiException if the resource cannot be found. + * + * @return Onboarding + * @throws ApiException + */ + public function get() + { + return $this->rest_read('', []); + } + + /** + * Submit data that will be prefilled in the merchant’s onboarding. + * Please note that the data you submit will only be processed when the onboarding status is needs-data. + * + * Information that the merchant has entered in their dashboard will not be overwritten. + * + * Will throw a ApiException if the resource cannot be found. + * + * @return void + * @throws ApiException + */ + public function submit(array $parameters = []) + { + + return $this->rest_create($parameters, []); + } + + protected function rest_read($id, array $filters) + { + $result = $this->client->performHttpCall( + self::REST_READ, + $this->getResourcePath() . $this->buildQueryString($filters) + ); + + return ResourceFactory::createFromApiResult($result, $this->getResourceObject()); + } + + protected function rest_create(array $body, array $filters) + { + $this->client->performHttpCall( + self::REST_CREATE, + $this->getResourcePath() . $this->buildQueryString($filters), + $this->parseRequestBody($body) + ); + } +} diff --git a/src/Endpoints/OrderEndpoint.php b/src/Endpoints/OrderEndpoint.php index 402de5ee..32947339 100644 --- a/src/Endpoints/OrderEndpoint.php +++ b/src/Endpoints/OrderEndpoint.php @@ -6,7 +6,7 @@ use Mollie\Api\Resources\Order; use Mollie\Api\Resources\OrderCollection; -class OrderEndpoint extends EndpointAbstract +class OrderEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "orders"; diff --git a/src/Endpoints/OrderLineEndpoint.php b/src/Endpoints/OrderLineEndpoint.php index 6fbaf7c0..7354d7f0 100644 --- a/src/Endpoints/OrderLineEndpoint.php +++ b/src/Endpoints/OrderLineEndpoint.php @@ -8,7 +8,7 @@ use Mollie\Api\Resources\OrderLineCollection; use Mollie\Api\Resources\ResourceFactory; -class OrderLineEndpoint extends EndpointAbstract +class OrderLineEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "orders_lines"; diff --git a/src/Endpoints/OrderPaymentEndpoint.php b/src/Endpoints/OrderPaymentEndpoint.php index 7bdf907f..75101854 100644 --- a/src/Endpoints/OrderPaymentEndpoint.php +++ b/src/Endpoints/OrderPaymentEndpoint.php @@ -6,7 +6,7 @@ use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentCollection; -class OrderPaymentEndpoint extends EndpointAbstract +class OrderPaymentEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "orders_payments"; diff --git a/src/Endpoints/OrderRefundEndpoint.php b/src/Endpoints/OrderRefundEndpoint.php index 9cfa1228..21007789 100644 --- a/src/Endpoints/OrderRefundEndpoint.php +++ b/src/Endpoints/OrderRefundEndpoint.php @@ -6,7 +6,7 @@ use Mollie\Api\Resources\Refund; use Mollie\Api\Resources\RefundCollection; -class OrderRefundEndpoint extends EndpointAbstract +class OrderRefundEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "orders_refunds"; diff --git a/src/Endpoints/OrganizationEndpoint.php b/src/Endpoints/OrganizationEndpoint.php index 4eee58c1..f84cfe5b 100644 --- a/src/Endpoints/OrganizationEndpoint.php +++ b/src/Endpoints/OrganizationEndpoint.php @@ -7,7 +7,7 @@ use Mollie\Api\Resources\Organization; use Mollie\Api\Resources\OrganizationCollection; -class OrganizationEndpoint extends EndpointAbstract +class OrganizationEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "organizations"; diff --git a/src/Endpoints/PaymentCaptureEndpoint.php b/src/Endpoints/PaymentCaptureEndpoint.php index a492024d..23e68b1c 100644 --- a/src/Endpoints/PaymentCaptureEndpoint.php +++ b/src/Endpoints/PaymentCaptureEndpoint.php @@ -6,7 +6,7 @@ use Mollie\Api\Resources\CaptureCollection; use Mollie\Api\Resources\Payment; -class PaymentCaptureEndpoint extends EndpointAbstract +class PaymentCaptureEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "payments_captures"; diff --git a/src/Endpoints/PaymentChargebackEndpoint.php b/src/Endpoints/PaymentChargebackEndpoint.php index c5ab508a..5c364f10 100644 --- a/src/Endpoints/PaymentChargebackEndpoint.php +++ b/src/Endpoints/PaymentChargebackEndpoint.php @@ -6,7 +6,7 @@ use Mollie\Api\Resources\ChargebackCollection; use Mollie\Api\Resources\Payment; -class PaymentChargebackEndpoint extends EndpointAbstract +class PaymentChargebackEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "payments_chargebacks"; diff --git a/src/Endpoints/PaymentEndpoint.php b/src/Endpoints/PaymentEndpoint.php index ef897f91..6562164e 100644 --- a/src/Endpoints/PaymentEndpoint.php +++ b/src/Endpoints/PaymentEndpoint.php @@ -8,7 +8,7 @@ use Mollie\Api\Resources\Refund; use Mollie\Api\Resources\ResourceFactory; -class PaymentEndpoint extends EndpointAbstract +class PaymentEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "payments"; diff --git a/src/Endpoints/PaymentRefundEndpoint.php b/src/Endpoints/PaymentRefundEndpoint.php index 3e00271c..a3081520 100644 --- a/src/Endpoints/PaymentRefundEndpoint.php +++ b/src/Endpoints/PaymentRefundEndpoint.php @@ -6,7 +6,7 @@ use Mollie\Api\Resources\Refund; use Mollie\Api\Resources\RefundCollection; -class PaymentRefundEndpoint extends EndpointAbstract +class PaymentRefundEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "payments_refunds"; diff --git a/src/Endpoints/PermissionEndpoint.php b/src/Endpoints/PermissionEndpoint.php index 440a49f8..e10dba30 100644 --- a/src/Endpoints/PermissionEndpoint.php +++ b/src/Endpoints/PermissionEndpoint.php @@ -6,7 +6,7 @@ use Mollie\Api\Resources\Permission; use Mollie\Api\Resources\PermissionCollection; -class PermissionEndpoint extends EndpointAbstract +class PermissionEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "permissions"; diff --git a/src/Endpoints/ProfileEndpoint.php b/src/Endpoints/ProfileEndpoint.php index 35c5f0e9..3ce5dce1 100644 --- a/src/Endpoints/ProfileEndpoint.php +++ b/src/Endpoints/ProfileEndpoint.php @@ -7,7 +7,7 @@ use Mollie\Api\Resources\Profile; use Mollie\Api\Resources\ProfileCollection; -class ProfileEndpoint extends EndpointAbstract +class ProfileEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "profiles"; protected $resourceClass = Profile::class; @@ -117,4 +117,4 @@ public function page($from = null, $limit = null, array $parameters = []) } -} \ No newline at end of file +} diff --git a/src/Endpoints/ProfileMethodEndpoint.php b/src/Endpoints/ProfileMethodEndpoint.php index afbea8d6..66acf2ad 100644 --- a/src/Endpoints/ProfileMethodEndpoint.php +++ b/src/Endpoints/ProfileMethodEndpoint.php @@ -7,7 +7,7 @@ use Mollie\Api\Resources\Profile; use Mollie\Api\Resources\ResourceFactory; -class ProfileMethodEndpoint extends EndpointAbstract +class ProfileMethodEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "profiles_methods"; diff --git a/src/Endpoints/RefundEndpoint.php b/src/Endpoints/RefundEndpoint.php index 537b1ed3..9daefd10 100644 --- a/src/Endpoints/RefundEndpoint.php +++ b/src/Endpoints/RefundEndpoint.php @@ -6,7 +6,7 @@ use Mollie\Api\Resources\Refund; use Mollie\Api\Resources\RefundCollection; -class RefundEndpoint extends EndpointAbstract +class RefundEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "refunds"; diff --git a/src/Endpoints/SettlementsEndpoint.php b/src/Endpoints/SettlementsEndpoint.php index eac3476c..8aae0035 100644 --- a/src/Endpoints/SettlementsEndpoint.php +++ b/src/Endpoints/SettlementsEndpoint.php @@ -6,7 +6,7 @@ use Mollie\Api\Resources\Settlement; use Mollie\Api\Resources\SettlementCollection; -class SettlementsEndpoint extends EndpointAbstract +class SettlementsEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "settlements"; @@ -84,4 +84,4 @@ public function page($from = null, $limit = null, array $parameters = []) { return $this->rest_list($from, $limit, $parameters); } -} \ No newline at end of file +} diff --git a/src/Endpoints/ShipmentEndpoint.php b/src/Endpoints/ShipmentEndpoint.php index b7339c96..c7998ef4 100644 --- a/src/Endpoints/ShipmentEndpoint.php +++ b/src/Endpoints/ShipmentEndpoint.php @@ -6,7 +6,7 @@ use Mollie\Api\Resources\Shipment; use Mollie\Api\Resources\ShipmentCollection; -class ShipmentEndpoint extends EndpointAbstract +class ShipmentEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "orders_shipments"; diff --git a/src/Endpoints/SubscriptionEndpoint.php b/src/Endpoints/SubscriptionEndpoint.php index 5882c493..8f213d3a 100644 --- a/src/Endpoints/SubscriptionEndpoint.php +++ b/src/Endpoints/SubscriptionEndpoint.php @@ -8,7 +8,7 @@ use Mollie\Api\Resources\Subscription; use Mollie\Api\Resources\SubscriptionCollection; -class SubscriptionEndpoint extends EndpointAbstract +class SubscriptionEndpoint extends CollectionEndpointAbstract { protected $resourcePath = "customers_subscriptions"; @@ -94,4 +94,4 @@ public function cancelFor(Customer $customer, $subscriptionId, array $data = []) return parent::rest_delete($subscriptionId, $data); } -} \ No newline at end of file +} diff --git a/src/MollieApiClient.php b/src/MollieApiClient.php index 316f4280..a33448fa 100644 --- a/src/MollieApiClient.php +++ b/src/MollieApiClient.php @@ -12,6 +12,7 @@ use Mollie\Api\Endpoints\InvoiceEndpoint; use Mollie\Api\Endpoints\MandateEndpoint; use Mollie\Api\Endpoints\MethodEndpoint; +use Mollie\Api\Endpoints\OnboardingEndpoint; use Mollie\Api\Endpoints\OrderEndpoint; use Mollie\Api\Endpoints\OrderLineEndpoint; use Mollie\Api\Endpoints\OrderPaymentEndpoint; @@ -156,6 +157,13 @@ class MollieApiClient */ public $invoices; + /** + * RESTful Onboarding resource. + * + * @var OnboardingEndpoint + */ + public $onboarding; + /** * RESTful Order resource. * @@ -242,7 +250,6 @@ class MollieApiClient * @var array */ protected $versionStrings = []; - /** * @var int */ @@ -285,6 +292,7 @@ public function initializeEndpoints() $this->invoices = new InvoiceEndpoint($this); $this->permissions = new PermissionEndpoint($this); $this->profiles = new ProfileEndpoint($this); + $this->onboarding = new OnboardingEndpoint($this); $this->organizations = new OrganizationEndpoint($this); $this->orders = new OrderEndpoint($this); $this->orderLines = new OrderLineEndpoint($this); diff --git a/src/Resources/Onboarding.php b/src/Resources/Onboarding.php new file mode 100644 index 00000000..1a9a2a4b --- /dev/null +++ b/src/Resources/Onboarding.php @@ -0,0 +1,70 @@ +status === OnboardingStatus::NEEDS_DATA; + } + + /** + * @return bool + */ + public function isInReview() + { + return $this->status === OnboardingStatus::IN_REVIEW; + } + + /** + * @return bool + */ + public function isCompleted() + { + return $this->status === OnboardingStatus::COMPLETED; + } +} diff --git a/src/Types/OnboardingStatus.php b/src/Types/OnboardingStatus.php new file mode 100644 index 00000000..06d6ded5 --- /dev/null +++ b/src/Types/OnboardingStatus.php @@ -0,0 +1,21 @@ +mockApiCall( + new Request('GET', '/v2/onboarding/me'), + new Response( + 200, + [], + '{ + "resource": "onboarding", + "name": "Mollie B.V.", + "signedUpAt": "2018-12-20T10:49:08+00:00", + "status": "completed", + "canReceivePayments": true, + "canReceiveSettlements": true, + "_links": { + "self": { + "href": "https://api.mollie.com/v2/onboarding/me", + "type": "application/hal+json" + }, + "onboarding": { + "href": "https://www.mollie.com/dashboard/onboarding", + "type": "text/html" + }, + "organization": { + "href": "https://api.mollie.com/v2/organization/org_12345", + "type": "application/hal+json" + }, + "documentation": { + "href": "https://docs.mollie.com/reference/v2/onboarding-api/get-onboarding-status", + "type": "text/html" + } + } + }' + ) + ); + + $onboarding = $this->apiClient->onboarding->get(); + + $this->assertInstanceOf(Onboarding::class, $onboarding); + $this->assertEquals("onboarding", $onboarding->resource); + $this->assertEquals("Mollie B.V.", $onboarding->name); + $this->assertEquals(OnboardingStatus::COMPLETED, $onboarding->status); + $this->assertEquals("2018-12-20T10:49:08+00:00", $onboarding->signedUpAt); + $this->assertEquals(true, $onboarding->canReceivePayments); + $this->assertEquals(true, $onboarding->canReceiveSettlements); + + $selfLink = (object)['href' => 'https://api.mollie.com/v2/onboarding/me', 'type' => 'application/hal+json']; + $this->assertEquals($selfLink, $onboarding->_links->self); + + $onboardingLink = (object)['href' => 'https://www.mollie.com/dashboard/onboarding', 'type' => 'text/html']; + $this->assertEquals($onboardingLink, $onboarding->_links->onboarding); + + $organizationLink = (object)['href' => 'https://api.mollie.com/v2/organization/org_12345', 'type' => 'application/hal+json']; + $this->assertEquals($organizationLink, $onboarding->_links->organization); + + $documentationLink = (object)['href' => 'https://docs.mollie.com/reference/v2/onboarding-api/get-onboarding-status', 'type' => 'text/html']; + $this->assertEquals($documentationLink, $onboarding->_links->documentation); + } + + public function testSubmitWorks() + { + $this->mockApiCall( + new Request('POST', '/v2/onboarding/me'), + new Response(204) + ); + + $this->apiClient->onboarding->submit(); + } +} diff --git a/tests/Mollie/API/Resources/OnboardingTest.php b/tests/Mollie/API/Resources/OnboardingTest.php new file mode 100644 index 00000000..953d190c --- /dev/null +++ b/tests/Mollie/API/Resources/OnboardingTest.php @@ -0,0 +1,42 @@ +createMock(MollieApiClient::class)); + $orderLine->status = $status; + + $this->assertEquals($expected_boolean, $orderLine->{$function}()); + } + + public function dpTestOnboardingStatuses() + { + return [ + [OnboardingStatus::NEEDS_DATA, "needsData", true], + [OnboardingStatus::NEEDS_DATA, "isInReview", false], + [OnboardingStatus::NEEDS_DATA, "isCompleted", false], + + [OnboardingStatus::IN_REVIEW, "needsData", false], + [OnboardingStatus::IN_REVIEW, "isInReview", true], + [OnboardingStatus::IN_REVIEW, "isCompleted", false], + + [OnboardingStatus::COMPLETED, "needsData", false], + [OnboardingStatus::COMPLETED, "isInReview", false], + [OnboardingStatus::COMPLETED, "isCompleted", true], + ]; + } +}