Skip to content

Commit

Permalink
refactor: remove payment related query and payload classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Jan 20, 2025
1 parent 5e3d724 commit e3898ff
Show file tree
Hide file tree
Showing 78 changed files with 801 additions and 701 deletions.
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 / PHP - 8.2

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.

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

View workflow job for this annotation

GitHub Actions / PHP - 8.4

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.

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

View workflow job for this annotation

GitHub Actions / PHP - 8.3

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.

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

View workflow job for this annotation

GitHub Actions / PHP - 8.1

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.

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

View workflow job for this annotation

GitHub Actions / PHP - 7.4

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.

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

View workflow job for this annotation

GitHub Actions / PHP - 8

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 @@ -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 Down Expand Up @@ -44,7 +44,7 @@ public function createForId($customerId, $payload = [], $query = [], bool $testm
{
if (! $payload instanceof CreatePaymentPayload) {

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

View workflow job for this annotation

GitHub Actions / PHP - 8.2

Class Mollie\Api\Http\Data\CreatePaymentPayload not found.

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

View workflow job for this annotation

GitHub Actions / PHP - 8.4

Class Mollie\Api\Http\Data\CreatePaymentPayload not found.

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

View workflow job for this annotation

GitHub Actions / PHP - 8.3

Class Mollie\Api\Http\Data\CreatePaymentPayload not found.

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

View workflow job for this annotation

GitHub Actions / PHP - 8.1

Class Mollie\Api\Http\Data\CreatePaymentPayload not found.

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

View workflow job for this annotation

GitHub Actions / PHP - 7.4

Class Mollie\Api\Http\Data\CreatePaymentPayload not found.

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

View workflow job for this annotation

GitHub Actions / PHP - 8

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 / PHP - 8.2

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.

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

View workflow job for this annotation

GitHub Actions / PHP - 8.4

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.

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

View workflow job for this annotation

GitHub Actions / PHP - 8.3

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.

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

View workflow job for this annotation

GitHub Actions / PHP - 8.1

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.

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

View workflow job for this annotation

GitHub Actions / PHP - 7.4

Static method Mollie\Api\Factories\CreatePaymentRequestFactory::new() invoked with 1 parameter, 0 required.

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

View workflow job for this annotation

GitHub Actions / PHP - 8

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

Expand Down
138 changes: 65 additions & 73 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\CreatePaymentRequestFactory;
use Mollie\Api\Factories\CreatePaymentRefundRequestFactory;
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 All @@ -114,13 +95,13 @@ public function delete(string $id, $data = []): ?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.
*
* @param array|bool $data
* @param array|bool $testmode
*
* @throws ApiException
*/
public function cancel(string $id, $data = []): ?Payment
public function cancel(string $id, $testmode = false): ?Payment
{
$testmode = Utility::extractBool($data, 'testmode', false);
$testmode = Utility::extractBool($testmode, 'testmode', false);

/** @var null|Payment */
return $this->send((new CancelPaymentRequest($id))->test($testmode));
Expand All @@ -132,22 +113,19 @@ public function cancel(string $id, $data = []): ?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
4 changes: 2 additions & 2 deletions 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 All @@ -19,7 +19,7 @@ public function create(): CreateMandatePayload
$this->get('consumerAccount'),
$this->get('consumerBic'),
$this->get('consumerEmail'),
$this->mapIfNotNull('signatureDate', fn (string $date) => DateTimeImmutable::createFromFormat('Y-m-d', $date)),
$this->mapIfNotNull('signatureDate', fn(string $date) => DateTimeImmutable::createFromFormat('Y-m-d', $date)),
$this->get('mandateReference'),
$this->get('paypalBillingAgreementId'),
);
Expand Down
Loading

0 comments on commit e3898ff

Please sign in to comment.