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

V3 without dedicated payloads and queries #759

Draft
wants to merge 5 commits into
base: v3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions examples/customers/create-customer-first-payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* How to create a first payment to allow recurring payments later.
*/

use Mollie\Api\Factories\CreatePaymentPayloadFactory;
use Mollie\Api\Factories\CreatePaymentRequestFactory;
use Mollie\Api\Http\Data\Metadata;
use Mollie\Api\Http\Data\Money;
use Mollie\Api\Http\Requests\CreateCustomerPaymentRequest;
Expand Down Expand Up @@ -40,7 +40,7 @@
*
* @See: https://docs.mollie.com/reference/v2/customers-api/create-customer-payment
*/
$payload = CreatePaymentPayloadFactory::new([
$payload = CreatePaymentRequestFactory::new([

Check failure on line 43 in examples/customers/create-customer-first-payment.php

View workflow job for this annotation

GitHub Actions / phpstan

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.
'description' => "First payment - Order #{$orderId}",
'amount' => new Money('EUR', '10.00'),
'redirectUrl' => "{$protocol}://{$hostname}/payments/return.php?order_id={$orderId}",
Expand All @@ -52,7 +52,7 @@
])->create();

$payment = $mollie->send(
new CreateCustomerPaymentRequest($customer->id, $payload)

Check failure on line 55 in examples/customers/create-customer-first-payment.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #2 $payload of class Mollie\Api\Http\Requests\CreateCustomerPaymentRequest constructor expects Mollie\Api\Http\Data\CreatePaymentPayload, Mollie\Api\Http\Requests\CreatePaymentRequest given.
);

/*
Expand All @@ -67,7 +67,7 @@
* After completion, the customer will have a pending or valid mandate that can be
* used for recurring payments and subscriptions.
*/
header('Location: '.$payment->getCheckoutUrl(), true, 303);
header('Location: ' . $payment->getCheckoutUrl(), true, 303);
} catch (\Mollie\Api\Exceptions\ApiException $e) {
echo 'API call failed: '.htmlspecialchars($e->getMessage());
echo 'API call failed: ' . htmlspecialchars($e->getMessage());
}
13 changes: 0 additions & 13 deletions src/Contracts/Factory.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/EndpointCollection/CustomerPaymentsEndpointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Mollie\Api\EndpointCollection;

use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Factories\CreatePaymentPayloadFactory;
use Mollie\Api\Factories\CreatePaymentRequestFactory;
use Mollie\Api\Factories\GetPaginatedCustomerPaymentsQueryFactory;
use Mollie\Api\Http\Data\CreatePaymentPayload;
use Mollie\Api\Http\Data\CreatePaymentQuery;
Expand All @@ -26,7 +26,7 @@
*
* @throws ApiException
*/
public function createFor(Customer $customer, $payload = [], $query = [], bool $testmode = false): Payment

Check failure on line 29 in src/EndpointCollection/CustomerPaymentsEndpointCollection.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $payload of method Mollie\Api\EndpointCollection\CustomerPaymentsEndpointCollection::createFor() has invalid type Mollie\Api\Http\Data\CreatePaymentPayload.

Check failure on line 29 in src/EndpointCollection/CustomerPaymentsEndpointCollection.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $query of method Mollie\Api\EndpointCollection\CustomerPaymentsEndpointCollection::createFor() has invalid type Mollie\Api\Http\Data\CreatePaymentQuery.
{
return $this->createForId($customer->id, $payload, $query, $testmode);
}
Expand All @@ -40,16 +40,16 @@
*
* @throws ApiException
*/
public function createForId($customerId, $payload = [], $query = [], bool $testmode = false): Payment

Check failure on line 43 in src/EndpointCollection/CustomerPaymentsEndpointCollection.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $payload of method Mollie\Api\EndpointCollection\CustomerPaymentsEndpointCollection::createForId() has invalid type Mollie\Api\Http\Data\CreatePaymentPayload.

Check failure on line 43 in src/EndpointCollection/CustomerPaymentsEndpointCollection.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $query of method Mollie\Api\EndpointCollection\CustomerPaymentsEndpointCollection::createForId() has invalid type Mollie\Api\Http\Data\CreatePaymentQuery.
{
if (! $payload instanceof CreatePaymentPayload) {

Check failure on line 45 in src/EndpointCollection/CustomerPaymentsEndpointCollection.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Mollie\Api\Http\Data\CreatePaymentPayload not found.
$testmode = Utility::extractBool($payload, 'testmode', $testmode);
$payload = CreatePaymentPayloadFactory::new($payload)
$payload = CreatePaymentRequestFactory::new($payload)

Check failure on line 47 in src/EndpointCollection/CustomerPaymentsEndpointCollection.php

View workflow job for this annotation

GitHub Actions / phpstan

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.
->create();
}

if (! $query instanceof CreatePaymentQuery) {

Check failure on line 51 in src/EndpointCollection/CustomerPaymentsEndpointCollection.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Mollie\Api\Http\Data\CreatePaymentQuery not found.
$query = CreatePaymentQuery::fromArray(Arr::wrap($query));

Check failure on line 52 in src/EndpointCollection/CustomerPaymentsEndpointCollection.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method fromArray() on an unknown class Mollie\Api\Http\Data\CreatePaymentQuery.
}

/** @var Payment */
Expand Down
132 changes: 62 additions & 70 deletions src/EndpointCollection/PaymentEndpointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,17 @@
namespace Mollie\Api\EndpointCollection;

use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Factories\CreatePaymentPayloadFactory;
use Mollie\Api\Factories\CreateRefundPaymentPayloadFactory;
use Mollie\Api\Factories\GetPaymentQueryFactory;
use Mollie\Api\Factories\CreatePaymentRefundRequestFactory;
use Mollie\Api\Factories\CreatePaymentRequestFactory;
use Mollie\Api\Factories\GetPaymentRequestFactory;
use Mollie\Api\Factories\SortablePaginatedQueryFactory;
use Mollie\Api\Factories\UpdatePaymentPayloadFactory;
use Mollie\Api\Http\Data\CreatePaymentPayload;
use Mollie\Api\Http\Data\CreatePaymentQuery;
use Mollie\Api\Http\Data\CreateRefundPaymentPayload;
use Mollie\Api\Http\Data\GetPaymentQuery;
use Mollie\Api\Http\Data\UpdatePaymentPayload;
use Mollie\Api\Factories\UpdatePaymentRequestFactory;
use Mollie\Api\Http\Requests\CancelPaymentRequest;
use Mollie\Api\Http\Requests\CreatePaymentRefundRequest;
use Mollie\Api\Http\Requests\CreatePaymentRequest;
use Mollie\Api\Http\Requests\GetPaginatedPaymentsRequest;
use Mollie\Api\Http\Requests\GetPaymentRequest;
use Mollie\Api\Http\Requests\UpdatePaymentRequest;
use Mollie\Api\Resources\LazyCollection;
use Mollie\Api\Resources\Payment;
use Mollie\Api\Resources\PaymentCollection;
use Mollie\Api\Resources\Refund;
use Mollie\Api\Utils\Arr;
use Mollie\Api\Utils\Utility;

class PaymentEndpointCollection extends EndpointCollection
Expand All @@ -33,65 +23,57 @@ class PaymentEndpointCollection extends EndpointCollection
*
* Will throw a ApiException if the payment id is invalid or the resource cannot be found.
*
* @param array|GetPaymentQuery $query
*
* @throws ApiException
*/
public function get(string $id, $query = [], bool $testmode = false): Payment
public function get(string $id, array $query = [], bool $testmode = false): Payment
{
if (! $query instanceof GetPaymentQuery) {
$testmode = Utility::extractBool($query, 'testmode', $testmode);
$query = GetPaymentQueryFactory::new($query)
->create();
}
$testmode = Utility::extractBool($query, 'testmode', $testmode);

$request = GetPaymentRequestFactory::new($id)
->withQuery($query)
->create();

return $this->send((new GetPaymentRequest($id, $query))->test($testmode));
return $this->send($request->test($testmode));
}

/**
* Creates a payment in Mollie.
*
* @param CreatePaymentPayload|array $payload An array containing details on the payment.
* @param CreatePaymentQuery|array|string $query An array of strings or a single string containing the details to include.
* @param array $payload An array containing details on the payment.
* @param array $query An array of strings or a single string containing the details to include.
*
* @throws ApiException
*/
public function create($payload = [], $query = [], bool $testmode = false): Payment
public function create(array $payload = [], array $query = [], bool $testmode = false): Payment
{
if (! $payload instanceof CreatePaymentPayload) {
$testmode = Utility::extractBool($payload, 'testmode', $testmode);
$payload = CreatePaymentPayloadFactory::new($payload)
->create();
}
$testmode = Utility::extractBool($query, 'testmode', $testmode);

if (! $query instanceof CreatePaymentQuery) {
$query = CreatePaymentQuery::fromArray(Arr::wrap($query));
}
$request = CreatePaymentRequestFactory::new()
->withPayload($payload)
->withQuery($query)
->create();

/** @var Payment */
return $this->send((new CreatePaymentRequest($payload, $query))->test($testmode));
return $this->send($request->test($testmode));
}

/**
* Update the given Payment.
*
* Will throw a ApiException if the payment id is invalid or the resource cannot be found.
*
* @param string $id
* @param array|UpdatePaymentPayload $data
*
* @throws ApiException
*/
public function update($id, $data = [], bool $testmode = false): ?Payment
public function update(string $id, array $data = [], bool $testmode = false): ?Payment
{
if (! $data instanceof UpdatePaymentPayload) {
$testmode = Utility::extractBool($data, 'testmode', $testmode);
$data = UpdatePaymentPayloadFactory::new($data)
->create();
}
$testmode = Utility::extractBool($data, 'testmode', $testmode);

$request = UpdatePaymentRequestFactory::new($id)
->withPayload($data)
->create();

/** @var null|Payment */
return $this->send((new UpdatePaymentRequest($id, $data))->test($testmode));
return $this->send($request->test($testmode));
}

/**
Expand All @@ -100,7 +82,6 @@ public function update($id, $data = [], bool $testmode = false): ?Payment
* Will throw a ApiException if the payment id is invalid or the resource cannot be found.
* Returns with HTTP status No Content (204) if successful.
*
*
* @throws ApiException
*/
public function delete(string $id, $data = []): ?Payment
Expand Down Expand Up @@ -132,22 +113,19 @@ public function cancel(string $id, $testmode = false): ?Payment
* The $data parameter may either be an array of endpoint
* parameters, or an instance of CreateRefundPaymentData.
*
* @param array|CreateRefundPaymentPayload $payload
* @param array $payload
*
* @throws ApiException
*/
public function refund(Payment $payment, $payload = [], bool $testmode = false): Refund
public function refund(Payment $payment, array $payload = [], bool $testmode = false): Refund
{
if (! $payload instanceof CreateRefundPaymentPayload) {
$testmode = Utility::extractBool($payload, 'testmode', $testmode);
$payload = CreateRefundPaymentPayloadFactory::new($payload)
->create();
}

return $this->send((new CreatePaymentRefundRequest(
$payment->id,
$payload
))->test($testmode));
$testmode = Utility::extractBool($payload, 'testmode', $testmode);

$request = CreatePaymentRefundRequestFactory::new($payment->id)
->withPayload($payload)
->create();

return $this->send($request->test($testmode));
}

/**
Expand All @@ -156,13 +134,20 @@ public function refund(Payment $payment, $payload = [], bool $testmode = false):
public function page(?string $from = null, ?int $limit = null, array $filters = []): PaymentCollection
{
$testmode = Utility::extractBool($filters, 'testmode', false);
$query = SortablePaginatedQueryFactory::new([
'from' => $from,
'limit' => $limit,
'filters' => $filters,
])->create();

return $this->send((new GetPaginatedPaymentsRequest($query))->test($testmode));
$query = SortablePaginatedQueryFactory::new()
->withQuery([
'from' => $from,
'limit' => $limit,
'filters' => $filters,
])
->create();

return $this->send((new GetPaginatedPaymentsRequest(
$query->from,
$query->limit,
$query->sort,
))->test($testmode));
}

/**
Expand All @@ -174,14 +159,21 @@ public function page(?string $from = null, ?int $limit = null, array $filters =
public function iterator(?string $from = null, ?int $limit = null, array $filters = [], bool $iterateBackwards = false): LazyCollection
{
$testmode = Utility::extractBool($filters, 'testmode', false);
$query = SortablePaginatedQueryFactory::new([
'from' => $from,
'limit' => $limit,
'filters' => $filters,
])->create();

$query = SortablePaginatedQueryFactory::new()
->withQuery([
'from' => $from,
'limit' => $limit,
'filters' => $filters,
])
->create();

return $this->send(
(new GetPaginatedPaymentsRequest($query))
(new GetPaginatedPaymentsRequest(
$query->from,
$query->limit,
$query->sort,
))
->useIterator()
->setIterationDirection($iterateBackwards)
->test($testmode)
Expand Down
22 changes: 9 additions & 13 deletions src/EndpointCollection/PaymentRefundEndpointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace Mollie\Api\EndpointCollection;

use Mollie\Api\Factories\CreateRefundPaymentPayloadFactory;
use Mollie\Api\Factories\CreatePaymentRefundRequestFactory;
use Mollie\Api\Factories\GetPaginatedPaymentRefundQueryFactory;
use Mollie\Api\Factories\GetPaymentRefundQueryFactory;
use Mollie\Api\Http\Data\CreateRefundPaymentPayload;
use Mollie\Api\Http\Data\GetPaymentRefundQuery;
use Mollie\Api\Http\Requests\CancelPaymentRefundRequest;
use Mollie\Api\Http\Requests\CreatePaymentRefundRequest;
use Mollie\Api\Http\Requests\GetPaginatedPaymentRefundsRequest;
use Mollie\Api\Http\Requests\GetPaymentRefundRequest;
use Mollie\Api\Resources\LazyCollection;
Expand All @@ -34,23 +32,21 @@ public function createFor(Payment $payment, array $data, $testmode = false): Ref
/**
* Creates a refund for a specific payment.
*
* @param array|CreateRefundPaymentPayload $payload
* @param array $payload
* @param bool|array $testmode
*
* @throws \Mollie\Api\Exceptions\ApiException
*/
public function createForId(string $paymentId, $payload = [], $testmode = false): Refund
public function createForId(string $paymentId, array $payload = [], $testmode = false): Refund
{
$testmode = Utility::extractBool($testmode, 'testmode', false);
$testmode = Utility::extractBool($payload, 'testmode', false) ??
Utility::extractBool($testmode, 'testmode', false);

if (! $payload instanceof CreateRefundPaymentPayload) {
$testmode = Utility::extractBool($payload, 'testmode', $testmode);
$payload = CreateRefundPaymentPayloadFactory::new($payload)
->create();
}
$request = CreatePaymentRefundRequestFactory::new($paymentId)
->withPayload($payload)
->create();

/** @var Refund */
return $this->send((new CreatePaymentRefundRequest($paymentId, $payload))->test($testmode));
return $this->send($request->test($testmode));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/ApplePayPaymentSessionPayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Mollie\Api\Http\Data\RequestApplePayPaymentSessionPayload;

class ApplePayPaymentSessionPayloadFactory extends Factory
class ApplePayPaymentSessionPayloadFactory extends OldFactory
{
public function create(): RequestApplePayPaymentSessionPayload
{
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/ApplicationFeeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Mollie\Api\Http\Data\ApplicationFee;

class ApplicationFeeFactory extends Factory
class ApplicationFeeFactory extends OldFactory
{
public function create(): ApplicationFee
{
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/CreateClientLinkPayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Mollie\Api\Http\Data\Owner;
use Mollie\Api\Http\Data\OwnerAddress;

class CreateClientLinkPayloadFactory extends Factory
class CreateClientLinkPayloadFactory extends OldFactory
{
public function create(): CreateClientLinkPayload
{
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/CreateCustomerPayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Mollie\Api\Http\Data\CreateCustomerPayload;
use Mollie\Api\Http\Data\Metadata;

class CreateCustomerPayloadFactory extends Factory
class CreateCustomerPayloadFactory extends OldFactory
{
public function create(): CreateCustomerPayload
{
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/CreateMandatePayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DateTimeImmutable;
use Mollie\Api\Http\Data\CreateMandatePayload;

class CreateMandatePayloadFactory extends Factory
class CreateMandatePayloadFactory extends OldFactory
{
public function create(): CreateMandatePayload
{
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/CreatePaymentCapturePayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Mollie\Api\Http\Data\CreatePaymentCapturePayload;
use Mollie\Api\Http\Data\Metadata;

class CreatePaymentCapturePayloadFactory extends Factory
class CreatePaymentCapturePayloadFactory extends OldFactory
{
public function create(): CreatePaymentCapturePayload
{
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/CreatePaymentLinkPayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Mollie\Api\Http\Data\CreatePaymentLinkPayload;

class CreatePaymentLinkPayloadFactory extends Factory
class CreatePaymentLinkPayloadFactory extends OldFactory
{
public function create(): CreatePaymentLinkPayload
{
Expand Down
Loading
Loading