diff --git a/src/Endpoints/SettlementCaptureEndpoint.php b/src/Endpoints/SettlementCaptureEndpoint.php new file mode 100644 index 00000000..0c46d330 --- /dev/null +++ b/src/Endpoints/SettlementCaptureEndpoint.php @@ -0,0 +1,44 @@ +client); + } + + protected function getResourceCollectionObject($count, $_links) + { + return new CaptureCollection($this->client, $count, $_links); + } + + /** + * Retrieves a collection of Settlement Captures from Mollie. + * + * @param string $settlementId + * @param string|null $from The first capture ID you want to include in your list. + * @param int|null $limit + * @param array $parameters + * + * @return mixed + * @throws \Mollie\Api\Exceptions\ApiException + */ + public function pageForId(string $settlementId, string $from = null, int $limit = null, array $parameters = []) + { + $this->parentId = $settlementId; + + return $this->rest_list($from, $limit, $parameters); + } +} diff --git a/src/Endpoints/SettlementChargebackEndpoint.php b/src/Endpoints/SettlementChargebackEndpoint.php new file mode 100644 index 00000000..850d0256 --- /dev/null +++ b/src/Endpoints/SettlementChargebackEndpoint.php @@ -0,0 +1,47 @@ +client); + } + + /** + * @inheritDoc + */ + protected function getResourceCollectionObject($count, $_links) + { + return new ChargebackCollection($this->client, $count, $_links); + } + + /** + * Retrieves a collection of Settlement Chargebacks from Mollie. + * + * @param string $settlementId + * @param string|null $from The first chargeback ID you want to include in your list. + * @param int|null $limit + * @param array $parameters + * + * @return mixed + * @throws \Mollie\Api\Exceptions\ApiException + */ + public function pageForId(string $settlementId, string $from = null, int $limit = null, array $parameters = []) + { + $this->parentId = $settlementId; + + return $this->rest_list($from, $limit, $parameters); + } +} diff --git a/src/Endpoints/SettlementRefundEndpoint.php b/src/Endpoints/SettlementRefundEndpoint.php new file mode 100644 index 00000000..31e9c1d1 --- /dev/null +++ b/src/Endpoints/SettlementRefundEndpoint.php @@ -0,0 +1,47 @@ +client, $count, $_links); + } + + /** + * @inheritDoc + */ + protected function getResourceObject() + { + return new Refund($this->client); + } + + /** + * Retrieves a collection of Settlement Refunds from Mollie. + * + * @param string $settlementId + * @param string|null $from The first refund ID you want to include in your list. + * @param int|null $limit + * @param array $parameters + * + * @return mixed + * @throws \Mollie\Api\Exceptions\ApiException + */ + public function pageForId(string $settlementId, string $from = null, int $limit = null, array $parameters = []) + { + $this->parentId = $settlementId; + + return $this->rest_list($from, $limit, $parameters); + } +} diff --git a/src/MollieApiClient.php b/src/MollieApiClient.php index 545d4006..bdd079cb 100644 --- a/src/MollieApiClient.php +++ b/src/MollieApiClient.php @@ -30,7 +30,10 @@ use Mollie\Api\Endpoints\ProfileEndpoint; use Mollie\Api\Endpoints\ProfileMethodEndpoint; use Mollie\Api\Endpoints\RefundEndpoint; +use Mollie\Api\Endpoints\SettlementCaptureEndpoint; +use Mollie\Api\Endpoints\SettlementChargebackEndpoint; use Mollie\Api\Endpoints\SettlementPaymentEndpoint; +use Mollie\Api\Endpoints\SettlementRefundEndpoint; use Mollie\Api\Endpoints\SettlementsEndpoint; use Mollie\Api\Endpoints\ShipmentEndpoint; use Mollie\Api\Endpoints\SubscriptionEndpoint; @@ -117,6 +120,20 @@ class MollieApiClient */ public $settlements; + /** + * RESTful Settlement capture resource. + * + * @var \Mollie\Api\Endpoints\SettlementCaptureEndpoint + */ + public $settlementCaptures; + + /** + * RESTful Settlement chargeback resource. + * + * @var \Mollie\Api\Endpoints\SettlementChargebackEndpoint + */ + public $settlementChargebacks; + /** * RESTful Settlement payment resource. * @@ -124,6 +141,13 @@ class MollieApiClient */ public $settlementPayments; + /** + * RESTful Settlement refund resource. + * + * @var \Mollie\Api\Endpoints\SettlementRefundEndpoint + */ + public $settlementRefunds; + /** * RESTful Subscription resource. * @@ -365,7 +389,10 @@ public function initializeEndpoints() $this->profileMethods = new ProfileMethodEndpoint($this); $this->customers = new CustomerEndpoint($this); $this->settlements = new SettlementsEndpoint($this); + $this->settlementCaptures = new SettlementCaptureEndpoint($this); + $this->settlementChargebacks = new SettlementChargebackEndpoint($this); $this->settlementPayments = new SettlementPaymentEndpoint($this); + $this->settlementRefunds = new SettlementRefundEndpoint($this); $this->subscriptions = new SubscriptionEndpoint($this); $this->customerPayments = new CustomerPaymentsEndpoint($this); $this->mandates = new MandateEndpoint($this); diff --git a/src/Resources/Settlement.php b/src/Resources/Settlement.php index 8bdef55a..54f71cd2 100644 --- a/src/Resources/Settlement.php +++ b/src/Resources/Settlement.php @@ -3,7 +3,6 @@ namespace Mollie\Api\Resources; use Mollie\Api\Exceptions\ApiException; -use Mollie\Api\MollieApiClient; use Mollie\Api\Types\SettlementStatus; class Settlement extends BaseResource @@ -92,7 +91,7 @@ public function isPending() } /** - * Is this settlement paidout? + * Is this settlement paid out? * * @return bool */ @@ -102,7 +101,7 @@ public function isPaidout() } /** - * Is this settlement failed? + * Has this settlement failed? * * @return bool */ @@ -112,7 +111,7 @@ public function isFailed() } /** - * Retrieves all payments associated with this settlement + * Retrieve the first page of payments associated with this settlement. * * @param int|null $limit * @param array $parameters @@ -121,72 +120,65 @@ public function isFailed() */ public function payments(int $limit = null, array $parameters = []): PaymentCollection { - return $this->client->settlementPayments->pageForId($this->id, null, $limit, $parameters); + return $this->client->settlementPayments->pageForId( + $this->id, + null, + $limit, + $parameters + ); } /** - * Retrieves all refunds associated with this settlement + * Retrieve the first page of refunds associated with this settlement. * + * @param int|null $limit + * @param array $parameters * @return RefundCollection * @throws ApiException */ - public function refunds() + public function refunds(int $limit = null, array $parameters = []) { - if (! isset($this->_links->refunds->href)) { - return new RefundCollection($this->client, 0, null); - } - - $result = $this->client->performHttpCallToFullUrl(MollieApiClient::HTTP_GET, $this->_links->refunds->href); - - return ResourceFactory::createCursorResourceCollection( - $this->client, - $result->_embedded->refunds, - Refund::class, - $result->_links + return $this->client->settlementRefunds->pageForId( + $this->id, + null, + $limit, + $parameters ); } /** - * Retrieves all chargebacks associated with this settlement + * Retrieve the first page of chargebacks associated with this settlement. * + * @param int|null $limit + * @param array $parameters * @return ChargebackCollection * @throws ApiException */ - public function chargebacks() + public function chargebacks(int $limit = null, array $parameters = []) { - if (! isset($this->_links->chargebacks->href)) { - return new ChargebackCollection($this->client, 0, null); - } - - $result = $this->client->performHttpCallToFullUrl(MollieApiClient::HTTP_GET, $this->_links->chargebacks->href); - - return ResourceFactory::createCursorResourceCollection( - $this->client, - $result->_embedded->chargebacks, - Chargeback::class, - $result->_links + return $this->client->settlementChargebacks->pageForId( + $this->id, + null, + $limit, + $parameters ); } /** - * Retrieves all captures associated with this settlement + * Retrieve the first page of cap associated with this settlement. * + * @param int|null $limit + * @param array $parameters * @return CaptureCollection * @throws ApiException */ - public function captures() + public function captures(int $limit = null, array $parameters = []) { - if (! isset($this->_links->captures->href)) { - return new CaptureCollection($this->client, 0, null); - } - - $result = $this->client->performHttpCallToFullUrl(MollieApiClient::HTTP_GET, $this->_links->captures->href); - - return ResourceFactory::createCursorResourceCollection( - $this->client, - $result->_embedded->captures, - Capture::class, - $result->_links + return $this->client->settlementCaptures->pageForId( + $this->id, + null, + $limit, + $parameters ); } } diff --git a/tests/Mollie/API/Endpoints/SettlementCaptureEndpointTest.php b/tests/Mollie/API/Endpoints/SettlementCaptureEndpointTest.php new file mode 100644 index 00000000..366a66a3 --- /dev/null +++ b/tests/Mollie/API/Endpoints/SettlementCaptureEndpointTest.php @@ -0,0 +1,98 @@ +mockApiCall( + new Request( + 'GET', + '/v2/settlements/stl_jDk30akdN/captures?limit=5&foo=bar' + ), + new Response( + 200, + [], + '{ + "_embedded": { + "captures": [ + { + "resource": "capture", + "id": "cpt_4qqhO89gsT", + "mode": "live", + "amount": { + "value": "1027.99", + "currency": "EUR" + }, + "settlementAmount": { + "value": "399.00", + "currency": "EUR" + }, + "paymentId": "tr_WDqYK6vllg", + "shipmentId": "shp_3wmsgCJN4U", + "settlementId": "stl_jDk30akdN", + "createdAt": "2018-08-02T09:29:56+00:00", + "_links": { + "self": { + "href": "https://api.mollie.com/v2/payments/tr_WDqYK6vllg/captures/cpt_4qqhO89gsT", + "type": "application/hal+json" + }, + "payment": { + "href": "https://api.mollie.com/v2/payments/tr_WDqYK6vllg", + "type": "application/hal+json" + }, + "shipment": { + "href": "https://api.mollie.com/v2/orders/ord_8wmqcHMN4U/shipments/shp_3wmsgCJN4U", + "type": "application/hal+json" + }, + "settlement": { + "href": "https://api.mollie.com/v2/settlements/stl_jDk30akdN", + "type": "application/hal+json" + }, + "documentation": { + "href": "https://docs.mollie.com/reference/v2/captures-api/get-capture", + "type": "text/html" + } + } + } + ] + }, + "count": 1, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/settlements-api/list-settlement-captures", + "type": "text/html" + }, + "self": { + "href": "https://api.mollie.com/v2/settlements/stl_jDk30akdN/captures?limit=50", + "type": "application/hal+json" + }, + "previous": null, + "next": null + } + }' + ) + ); + + $settlement = new Settlement($this->apiClient); + $settlement->id = 'stl_jDk30akdN'; + + $captures = $settlement->captures(5, ['foo' => 'bar']); + + $this->assertInstanceOf(CaptureCollection::class, $captures); + $this->assertCount(1, $captures); + + $capture = $captures[0]; + $this->assertInstanceOf(Capture::class, $capture); + $this->assertEquals("cpt_4qqhO89gsT", $capture->id); + } +} diff --git a/tests/Mollie/API/Endpoints/SettlementChargebackEndpointTest.php b/tests/Mollie/API/Endpoints/SettlementChargebackEndpointTest.php new file mode 100644 index 00000000..f7680849 --- /dev/null +++ b/tests/Mollie/API/Endpoints/SettlementChargebackEndpointTest.php @@ -0,0 +1,88 @@ +mockApiCall( + new Request( + 'GET', + '/v2/settlements/stl_jDk30akdN/chargebacks?limit=5&foo=bar' + ), + new Response( + 200, + [], + '{ + "count": 1, + "_embedded": { + "chargebacks": [ + { + "resource": "chargeback", + "id": "chb_n9z0tp", + "amount": { + "value": "43.38", + "currency": "USD" + }, + "settlementAmount": { + "value": "-37.14", + "currency": "EUR" + }, + "createdAt": "2018-03-14T17:00:52.0Z", + "reversedAt": null, + "paymentId": "tr_WDqYK6vllg", + "settlementId": "stl_jDk30akdN", + "_links": { + "self": { + "href": "https://api.mollie.com/v2/payments/tr_WDqYK6vllg/chargebacks/chb_n9z0tp", + "type": "application/hal+json" + }, + "payment": { + "href": "https://api.mollie.com/v2/payments/tr_WDqYK6vllg", + "type": "application/hal+json" + }, + "settlement": { + "href": "https://api.mollie.com/v2/settlements/stl_jDk30akdN", + "type": "application/hal+json" + } + } + } + ] + }, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/settlements-api/list-settlement-chargebacks", + "type": "text/html" + }, + "self": { + "href": "https://api.mollie.com/v2/settlements/stl_jDk30akdN/chargebacks", + "type": "application/hal+json" + }, + "previous": null, + "next": null + } + }' + ) + ); + + $settlement = new Settlement($this->apiClient); + $settlement->id = 'stl_jDk30akdN'; + + $chargebacks = $settlement->chargebacks(5, ['foo' => 'bar']); + + $this->assertInstanceOf(ChargebackCollection::class, $chargebacks); + $this->assertCount(1, $chargebacks); + + $chargeback = $chargebacks[0]; + $this->assertInstanceOf(Chargeback::class, $chargeback); + $this->assertEquals("chb_n9z0tp", $chargeback->id); + } +} diff --git a/tests/SettlementRefundEndpointTest.php b/tests/SettlementRefundEndpointTest.php new file mode 100644 index 00000000..14df7f53 --- /dev/null +++ b/tests/SettlementRefundEndpointTest.php @@ -0,0 +1,91 @@ +mockApiCall( + new Request( + 'GET', + '/v2/settlements/stl_jDk30akdN/refunds?limit=5&foo=bar' + ), + new Response( + 200, + [], + '{ + "_embedded": { + "refunds": [ + { + "resource": "refund", + "id": "re_3aKhkUNigy", + "amount": { + "value": "10.00", + "currency": "EUR" + }, + "status": "refunded", + "createdAt": "2018-08-30T07:59:02+00:00", + "description": "Order #33", + "paymentId": "tr_maJaG2j8OM", + "settlementAmount": { + "value": "-10.00", + "currency": "EUR" + }, + "settlementId": "stl_jDk30akdN", + "_links": { + "self": { + "href": "https://api.mollie.com/v2/payments/tr_maJaG2j8OM/refunds/re_3aKhkUNigy", + "type": "application/hal+json" + }, + "payment": { + "href": "https://api.mollie.com/v2/payments/tr_maJaG2j8OM", + "type": "application/hal+json" + }, + "settlement": { + "href": "https://api.mollie.com/v2/settlements/stl_jDk30akdN", + "type": "application/hal+json" + } + } + }, + { } + ] + }, + "count": 1, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/settlements-api/list-settlement-refunds", + "type": "text/html" + }, + "self": { + "href": "https://api.mollie.com/v2/settlements/stl_jDk30akdN/refunds?limit=50", + "type": "application/hal+json" + }, + "previous": null, + "next": null + } + }' + ) + ); + + $settlement = new Settlement($this->apiClient); + $settlement->id = 'stl_jDk30akdN'; + + $refunds = $settlement->refunds(5, ['foo' => 'bar']); + + $this->assertInstanceOf(RefundCollection::class, $refunds); + $this->assertCount(2, $refunds); + + $refund = $refunds[0]; + $this->assertInstanceOf(Refund::class, $refund); + $this->assertEquals("re_3aKhkUNigy", $refund->id); + } +}