From 3ec3d820557bed202966fbc55cd161355e049030 Mon Sep 17 00:00:00 2001 From: Rustam Date: Mon, 16 Jun 2025 10:49:58 +0500 Subject: [PATCH 1/9] feat: Use enums --- README.md | 12 ++++++------ composer.json | 2 +- src/Enums/PaymentIntentStatus.php | 16 ++++++++++++++++ src/Enums/PaymentMethodType.php | 12 ++++++++++++ src/Models/PaymentIntent.php | 18 +++++------------- src/Models/PaymentMethod.php | 14 ++++++-------- 6 files changed, 46 insertions(+), 28 deletions(-) create mode 100644 src/Enums/PaymentIntentStatus.php create mode 100644 src/Enums/PaymentMethodType.php diff --git a/README.md b/README.md index 14945b3..6ffa812 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ Represents how a customer will pay (credit card, PayPal, etc.). Contains: ```php $paymentMethod = new PaymentMethod( id: 'pm_123', - type: 'card', + type: PaymentMethodType::Card, details: [ 'last4' => '4242', 'brand' => 'visa', @@ -129,7 +129,7 @@ $intent = new PaymentIntent( id: 'pi_123', // null for new intents amount: 1000, // $10.00 currency: 'usd', - status: 'requires_payment_method', + status: PaymentIntentStatus::RequiresPaymentMethod, customerId: 'cus_123', paymentMethodId: 'pm_123', metadata: ['order_id' => 'abc123'] @@ -175,7 +175,7 @@ const { paymentMethod, error } = await stripe.createPaymentMethod({ ```php $paymentMethod = $gateway->createPaymentMethod(new PaymentMethod( id: $_POST['payment_method_id'], - type: 'card', + type: PaymentMethodType::Card, customerId: $customer->id )); ``` @@ -333,7 +333,7 @@ $gateway->deleteCustomer('cus_123'); // Create a payment method (e.g., card) $paymentMethod = new \PaymentGateway\Models\PaymentMethod( null, // id will be generated by the gateway - 'card', + \Yiisoft\Payments\Enums\PaymentMethodType::Card, [ 'number' => '4242424242424242', 'exp_month' => '12', @@ -425,7 +425,7 @@ use Yiisoft\PaymentGateway\Gateways\AbstractGateway; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; -use Psr\Log\LoggerInterface; +use Psr\Log\LoggerInterface;use Yiisoft\Payments\Enums\PaymentIntentStatus; final class AcmePayGateway extends AbstractGateway { @@ -477,7 +477,7 @@ final class AcmePayGateway extends AbstractGateway return new PaymentIntent( id: $response['id'], - status: $response['status'], + status: PaymentIntentStatus::tryFrom($response['status']), amount: $intent->amount, currency: $intent->currency, customerId: $intent->customerId, diff --git a/composer.json b/composer.json index c8838b7..f0999bf 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ } ], "require": { - "php": "^8.1", + "php": "^8.2", "ext-json": "*", "psr/http-client": "^1.0", "psr/http-factory": "^1.0", diff --git a/src/Enums/PaymentIntentStatus.php b/src/Enums/PaymentIntentStatus.php new file mode 100644 index 0000000..6218e7f --- /dev/null +++ b/src/Enums/PaymentIntentStatus.php @@ -0,0 +1,16 @@ + $this->id, - 'status' => $this->status, + 'status' => $this->status->value, 'amount' => $this->amount, 'currency' => $this->currency, 'customer_id' => $this->customerId, @@ -118,7 +110,7 @@ public static function fromArray(array $data): self return new self( id: $data['id'] ?? null, - status: $data['status'] ?? null, + status: PaymentIntentStatus::tryFrom($data['status']), amount: $data['amount'] ?? null, currency: $currency, customerId: $data['customer_id'] ?? $data['customerId'] ?? null, diff --git a/src/Models/PaymentMethod.php b/src/Models/PaymentMethod.php index a2131b9..3ae70ca 100644 --- a/src/Models/PaymentMethod.php +++ b/src/Models/PaymentMethod.php @@ -4,18 +4,16 @@ namespace Yiisoft\Payments\Models; +use Yiisoft\Payments\Enums\PaymentMethodType; + /** * Represents a payment method in the payment gateway system. */ readonly class PaymentMethod { - public const TYPE_CARD = 'card'; - public const TYPE_PAYPAL = 'paypal'; - public const TYPE_SEPA_DEBIT = 'sepa_debit'; - /** * @param string|null $id The unique identifier for the payment method. - * @param string|null $type The type of the payment method (e.g., 'card', 'paypal', 'sepa_debit'). + * @param PaymentMethodType|null $type The type of the payment method (e.g., 'card', 'paypal', 'sepa_debit'). * @param array|null $details Additional details specific to the payment method. * @param string|null $customerId The ID of the customer this payment method belongs to. * @param array|null $billingDetails Billing information associated with the payment method. @@ -23,7 +21,7 @@ */ public function __construct( public ?string $id = null, - public ?string $type = null, + public ?PaymentMethodType $type = null, public ?array $details = null, public ?string $customerId = null, public ?array $billingDetails = null, @@ -35,7 +33,7 @@ public function toArray(): array { return [ 'id' => $this->id, - 'type' => $this->type, + 'type' => $this->type->value, 'details' => $this->details, 'customer_id' => $this->customerId, 'billing_details' => $this->billingDetails, @@ -53,7 +51,7 @@ public static function fromArray(array $data): self { return new self( id: $data['id'] ?? null, - type: $data['type'] ?? null, + type: PaymentMethodType::tryFrom($data['type']), details: $data['details'] ?? null, customerId: $data['customer_id'] ?? $data['customerId'] ?? null, billingDetails: $data['billing_details'] ?? $data['billingDetails'] ?? null, From 1c7df20cbdd31f0af40b01117352c2d49539d49f Mon Sep 17 00:00:00 2001 From: Rustam Date: Mon, 16 Jun 2025 10:52:27 +0500 Subject: [PATCH 2/9] Fix tests --- tests/Gateways/PayPalGatewayTest.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/Gateways/PayPalGatewayTest.php b/tests/Gateways/PayPalGatewayTest.php index 7ffb8e0..44ae058 100644 --- a/tests/Gateways/PayPalGatewayTest.php +++ b/tests/Gateways/PayPalGatewayTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Payments\Tests\Gateways; +use Yiisoft\Payments\Enums\PaymentMethodType; use Yiisoft\Payments\Models\Customer; use Yiisoft\Payments\Models\PaymentIntent; use Yiisoft\Payments\Models\PaymentMethod; @@ -52,7 +53,7 @@ private function getLastRequest(): array public function testCreateCustomer(): void { $this->mockTokenRequest(); - + $testCustomer = new Customer( id: null, email: 'test@example.com', @@ -89,7 +90,7 @@ public function testCreateCustomer(): void $this->assertSame('Test User', $result->name); $this->assertSame('+1234567890', $result->phone); $this->assertSame('Test Customer', $result->description); - + $lastRequest = $this->getLastRequest(); $this->assertSame('POST', $lastRequest['method']); $this->assertStringContainsString('/customer/partner-referrals', $lastRequest['uri']); @@ -98,7 +99,7 @@ public function testCreateCustomer(): void public function testCreatePaymentIntent(): void { $this->mockTokenRequest(); - + $paymentIntent = new PaymentIntent( id: null, amount: 1000, @@ -147,7 +148,7 @@ public function testCreatePaymentIntent(): void $this->assertSame('paypal', $result->paymentMethodId); $this->assertSame('12345', $result->metadata['order_id']); $this->assertSame('test@example.com', $result->receiptEmail); - + $lastRequest = $this->getLastRequest(); $this->assertSame('POST', $lastRequest['method']); $this->assertStringContainsString('/payments/payment', $lastRequest['uri']); @@ -156,10 +157,10 @@ public function testCreatePaymentIntent(): void public function testCreatePaymentMethod(): void { $this->mockTokenRequest(); - + $paymentMethod = new PaymentMethod( id: null, - type: 'paypal', + type: PaymentMethodType::PayPal, details: ['email' => 'test@example.com'], customerId: 'CUST-123', billingDetails: [ @@ -174,7 +175,7 @@ public function testCreatePaymentMethod(): void $this->assertSame('paypal', $result->type); $this->assertSame('CUST-123', $result->customerId); - + // Since PayPal handles payment methods differently, we don't expect an API call here // The method should just return the payment method with the customer ID set } @@ -182,7 +183,7 @@ public function testCreatePaymentMethod(): void public function testCreateRefund(): void { $this->mockTokenRequest(); - + $this->withResponse([ 'id' => 'REF-123', 'state' => 'completed', @@ -207,7 +208,7 @@ public function testCreateRefund(): void $this->assertSame('completed', $result['state']); $this->assertSame('10.00', $result['amount']['total']); $this->assertSame('USD', $result['amount']['currency']); - + $lastRequest = $this->getLastRequest(); $this->assertSame('POST', $lastRequest['method']); $this->assertStringContainsString('/payments/capture/CAPTURE-123/refund', $lastRequest['uri']); From fa926d8a0d28c773555fe846999ea80e9a5da447 Mon Sep 17 00:00:00 2001 From: rustamwin <16498265+rustamwin@users.noreply.github.com> Date: Mon, 16 Jun 2025 05:54:07 +0000 Subject: [PATCH 3/9] Apply Rector changes (CI) --- src/Gateways/PayPalGateway.php | 14 ++++---------- src/Gateways/StripeGateway.php | 4 +--- tests/Support/TestHttpClient.php | 4 +--- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/Gateways/PayPalGateway.php b/src/Gateways/PayPalGateway.php index 3a31327..efe41f2 100644 --- a/src/Gateways/PayPalGateway.php +++ b/src/Gateways/PayPalGateway.php @@ -16,24 +16,18 @@ class PayPalGateway extends AbstractGateway { private ?string $accessToken = null; private ?int $tokenExpires = null; - private const TOKEN_EXPIRY_BUFFER = 300; // 5 minutes in seconds - private string $clientId; - private string $clientSecret; - private bool $sandbox; + private const TOKEN_EXPIRY_BUFFER = 300; public function __construct( - string $clientId, - string $clientSecret, - bool $sandbox, + private string $clientId, + private string $clientSecret, + private bool $sandbox, ClientInterface $httpClient, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory, ?LoggerInterface $logger = null ) { parent::__construct($httpClient, $requestFactory, $streamFactory, $logger); - $this->clientId = $clientId; - $this->clientSecret = $clientSecret; - $this->sandbox = $sandbox; } protected function getBaseUri(): string diff --git a/src/Gateways/StripeGateway.php b/src/Gateways/StripeGateway.php index 9fdf9fb..48e625b 100644 --- a/src/Gateways/StripeGateway.php +++ b/src/Gateways/StripeGateway.php @@ -14,18 +14,16 @@ class StripeGateway extends AbstractGateway { - private string $apiKey; private string $apiVersion = '2023-10-16'; public function __construct( - string $apiKey, + private string $apiKey, ClientInterface $httpClient, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory, ?LoggerInterface $logger = null ) { parent::__construct($httpClient, $requestFactory, $streamFactory, $logger); - $this->apiKey = $apiKey; } protected function getBaseUri(): string diff --git a/tests/Support/TestHttpClient.php b/tests/Support/TestHttpClient.php index 6e4d56e..37591ac 100644 --- a/tests/Support/TestHttpClient.php +++ b/tests/Support/TestHttpClient.php @@ -11,13 +11,11 @@ class TestHttpClient implements ClientInterface { - private Psr17Factory $factory; private ?array $nextResponse = null; public array $lastRequest = []; - public function __construct(Psr17Factory $factory) + public function __construct(private Psr17Factory $factory) { - $this->factory = $factory; } public function setNextResponse(?array $response): void From ba3b9b96d41ff1bf19fbecb6edcec1d3186ded27 Mon Sep 17 00:00:00 2001 From: Rustam Date: Mon, 16 Jun 2025 10:55:21 +0500 Subject: [PATCH 4/9] Raise php version --- .github/workflows/build.yml | 2 +- .github/workflows/composer-require-checker.yml | 2 +- .github/workflows/static.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 05e7b40..e0e272a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -31,4 +31,4 @@ jobs: os: >- ['ubuntu-latest', 'windows-latest'] php: >- - ['8.1', '8.2', '8.3'] + ['8.2', '8.3', '8.4'] diff --git a/.github/workflows/composer-require-checker.yml b/.github/workflows/composer-require-checker.yml index a857bce..fda7a3d 100644 --- a/.github/workflows/composer-require-checker.yml +++ b/.github/workflows/composer-require-checker.yml @@ -31,4 +31,4 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['8.1', '8.2', '8.3'] + ['8.2', '8.3', '8.4'] diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index e33eca8..48c3bb8 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -29,4 +29,4 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['8.1', '8.2', '8.3'] + ['8.2', '8.3', '8.4'] From 6a2a023ebb72dc64420018033852498719d89e8f Mon Sep 17 00:00:00 2001 From: Rustam Date: Mon, 16 Jun 2025 11:04:53 +0500 Subject: [PATCH 5/9] Minor fix --- src/Gateways/PayPalGateway.php | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Gateways/PayPalGateway.php b/src/Gateways/PayPalGateway.php index efe41f2..fb717f4 100644 --- a/src/Gateways/PayPalGateway.php +++ b/src/Gateways/PayPalGateway.php @@ -4,6 +4,7 @@ namespace Yiisoft\Payments\Gateways; +use Yiisoft\Payments\Enums\PaymentIntentStatus; use Yiisoft\Payments\Models\Customer; use Yiisoft\Payments\Models\PaymentIntent; use Yiisoft\Payments\Models\PaymentMethod; @@ -32,7 +33,7 @@ public function __construct( protected function getBaseUri(): string { - return $this->sandbox + return $this->sandbox ? 'https://api-m.sandbox.paypal.com/v1' : 'https://api-m.paypal.com/v1'; } @@ -62,11 +63,11 @@ private function getAccessToken(): string ); $data = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR); - + if (!isset($data['access_token'])) { throw new \RuntimeException('Failed to get access token: ' . ($data['error'] ?? 'Unknown error')); } - + $this->accessToken = $data['access_token']; $this->tokenExpires = time() + ($data['expires_in'] ?? 3600); @@ -76,12 +77,12 @@ private function getAccessToken(): string protected function createRequest(string $method, string $endpoint, array $data = []): \Psr\Http\Message\RequestInterface { $request = parent::createRequest($method, $endpoint, $data); - + // Skip adding auth header for token requests if (str_contains($endpoint, '/oauth2/token')) { return $request; } - + return $request ->withHeader('Authorization', 'Bearer ' . $this->getAccessToken()) ->withHeader('PayPal-Request-Id', uniqid('', true)); @@ -103,7 +104,7 @@ public function createCustomer(Customer $customer): Customer ]; $response = $this->sendRequest($this->createRequest('POST', '/customer/partner-referrals', $data)); - + // PayPal doesn't have a direct customer creation endpoint in the same way as Stripe // This is a simplified implementation return new Customer( @@ -155,7 +156,7 @@ public function updateCustomer(Customer $customer): Customer ]; $this->sendRequest($this->createRequest('PATCH', "/customer/partner-referrals/{$customer->id}", [$data])); - + return $customer; } @@ -224,10 +225,10 @@ public function createPaymentIntent(PaymentIntent $intent): PaymentIntent break; } } - + return new PaymentIntent( id: $response['id'], - status: strtoupper($response['state'] ?? 'created'), + status: PaymentIntentStatus::tryFrom(strtolower($response['state'] ?? 'created')), amount: $intent->amount, currency: strtolower($response['transactions'][0]['amount']['currency'] ?? 'usd'), customerId: $intent->customerId, @@ -249,10 +250,10 @@ public function confirmPaymentIntent(string $intentId, array $params = []): Paym public function capturePaymentIntent(string $intentId, array $params = []): PaymentIntent { $response = $this->createRequest('POST', "/checkout/orders/{$intentId}/capture", $params); - + return new PaymentIntent( $response['id'], - strtolower($response['status']), + PaymentIntentStatus::tryFrom(strtolower($response['status'])), (int) ((float) $response['purchase_units'][0]['payments']['captures'][0]['amount']['value'] * 100), strtolower($response['purchase_units'][0]['payments']['captures'][0]['amount']['currency_code']), null, @@ -268,10 +269,10 @@ public function cancelPaymentIntent(string $intentId, array $params = []): Payme { // In PayPal, we void the authorization $response = $this->createRequest('POST', "/checkout/orders/{$intentId}/void-authorization", $params); - + return new PaymentIntent( $response['id'], - 'canceled', + PaymentIntentStatus::Canceled, null, null, null, @@ -295,7 +296,7 @@ public function createRefund(string $captureId, array $params = []): array $response = $this->sendRequest( $this->createRequest('POST', "/v1/payments/capture/{$captureId}/refund", $data) ); - + return [ 'id' => $response['id'], 'state' => $response['state'], @@ -320,7 +321,7 @@ public function retrievePaymentIntent(string $intentId): PaymentIntent return new PaymentIntent( id: $data['id'], - status: strtoupper($data['state']), + status: PaymentIntentStatus::tryFrom(strtoupper($data['state'])), amount: (int)($amount['total'] * 100) ?? 0, currency: $amount['currency'] ?? null, customerId: $data['payer']['payer_info']['customer_id'] ?? null, From fdbb0e86eaf87c12f9026b1cb98024cce8b9c5b1 Mon Sep 17 00:00:00 2001 From: Rustam Date: Mon, 16 Jun 2025 11:18:36 +0500 Subject: [PATCH 6/9] Minor fix --- src/Gateways/PayPalGateway.php | 9 +++++---- tests/Gateways/PayPalGatewayTest.php | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Gateways/PayPalGateway.php b/src/Gateways/PayPalGateway.php index fb717f4..2dd44a7 100644 --- a/src/Gateways/PayPalGateway.php +++ b/src/Gateways/PayPalGateway.php @@ -5,6 +5,7 @@ namespace Yiisoft\Payments\Gateways; use Yiisoft\Payments\Enums\PaymentIntentStatus; +use Yiisoft\Payments\Enums\PaymentMethodType; use Yiisoft\Payments\Models\Customer; use Yiisoft\Payments\Models\PaymentIntent; use Yiisoft\Payments\Models\PaymentMethod; @@ -171,21 +172,21 @@ public function createPaymentMethod(PaymentMethod $paymentMethod): PaymentMethod $data = [ 'customer' => $paymentMethod->customerId, 'type' => $paymentMethod->type, - $paymentMethod->type => $paymentMethod->details, + $paymentMethod->type->value => $paymentMethod->details, 'billing_details' => $paymentMethod->billingDetails, 'metadata' => $paymentMethod->metadata, ]; // In PayPal, payment methods are typically associated with orders, not directly with customers // This is a simplified implementation - return new PaymentMethod($paymentMethod->id, 'paypal', [], $paymentMethod->customerId); + return new PaymentMethod($paymentMethod->id, PaymentMethodType::PayPal, [], $paymentMethod->customerId); } public function attachPaymentMethod(string $paymentMethodId, string $customerId): PaymentMethod { // In PayPal, payment methods are typically associated with orders, not directly with customers // This is a simplified implementation - return new PaymentMethod($paymentMethodId, 'paypal', [], $customerId); + return new PaymentMethod($paymentMethodId, PaymentMethodType::PayPal, [], $customerId); } public function createPaymentIntent(PaymentIntent $intent): PaymentIntent @@ -321,7 +322,7 @@ public function retrievePaymentIntent(string $intentId): PaymentIntent return new PaymentIntent( id: $data['id'], - status: PaymentIntentStatus::tryFrom(strtoupper($data['state'])), + status: PaymentIntentStatus::tryFrom(strtolower($data['state'])), amount: (int)($amount['total'] * 100) ?? 0, currency: $amount['currency'] ?? null, customerId: $data['payer']['payer_info']['customer_id'] ?? null, diff --git a/tests/Gateways/PayPalGatewayTest.php b/tests/Gateways/PayPalGatewayTest.php index 44ae058..ed3db8e 100644 --- a/tests/Gateways/PayPalGatewayTest.php +++ b/tests/Gateways/PayPalGatewayTest.php @@ -173,7 +173,7 @@ public function testCreatePaymentMethod(): void // For PayPal, we're just testing that the method returns a PaymentMethod with the same customer ID $result = $this->gateway->createPaymentMethod($paymentMethod); - $this->assertSame('paypal', $result->type); + $this->assertSame(PaymentMethodType::PayPal, $result->type); $this->assertSame('CUST-123', $result->customerId); // Since PayPal handles payment methods differently, we don't expect an API call here From 3190fe1b4c4e7f3241cdb9f83114693f66f09598 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Mon, 16 Jun 2025 10:27:21 +0300 Subject: [PATCH 7/9] Fixes --- src/Gateways/PayPalGateway.php | 10 +++++++++- tests/Gateways/PayPalGatewayTest.php | 3 ++- tests/Gateways/StripeGatewayTest.php | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Gateways/PayPalGateway.php b/src/Gateways/PayPalGateway.php index 2dd44a7..9bb9c8d 100644 --- a/src/Gateways/PayPalGateway.php +++ b/src/Gateways/PayPalGateway.php @@ -227,9 +227,17 @@ public function createPaymentIntent(PaymentIntent $intent): PaymentIntent } } + // Map PayPal state to PaymentIntentStatus + $status = match (strtolower($response['state'] ?? '')) { + 'created' => PaymentIntentStatus::RequiresPaymentMethod, + 'approved' => PaymentIntentStatus::Succeeded, + 'failed' => PaymentIntentStatus::Canceled, + default => PaymentIntentStatus::RequiresPaymentMethod, + }; + return new PaymentIntent( id: $response['id'], - status: PaymentIntentStatus::tryFrom(strtolower($response['state'] ?? 'created')), + status: $status, amount: $intent->amount, currency: strtolower($response['transactions'][0]['amount']['currency'] ?? 'usd'), customerId: $intent->customerId, diff --git a/tests/Gateways/PayPalGatewayTest.php b/tests/Gateways/PayPalGatewayTest.php index ed3db8e..ca9fb1f 100644 --- a/tests/Gateways/PayPalGatewayTest.php +++ b/tests/Gateways/PayPalGatewayTest.php @@ -8,6 +8,7 @@ use Yiisoft\Payments\Models\Customer; use Yiisoft\Payments\Models\PaymentIntent; use Yiisoft\Payments\Models\PaymentMethod; +use Yiisoft\Payments\Enums\PaymentIntentStatus; use Yiisoft\Payments\Gateways\PayPalGateway; use Yiisoft\Payments\Tests\Support\TestHttpClient; use PHPUnit\Framework\TestCase; @@ -141,7 +142,7 @@ public function testCreatePaymentIntent(): void $result = $this->gateway->createPaymentIntent($paymentIntent); - $this->assertSame('CREATED', $result->status); + $this->assertSame(PaymentIntentStatus::RequiresPaymentMethod, $result->status); $this->assertSame(1000, $result->amount); $this->assertSame('USD', $result->currency); $this->assertSame('CUST-123', $result->customerId); diff --git a/tests/Gateways/StripeGatewayTest.php b/tests/Gateways/StripeGatewayTest.php index 2b0cbca..547adfb 100644 --- a/tests/Gateways/StripeGatewayTest.php +++ b/tests/Gateways/StripeGatewayTest.php @@ -8,6 +8,7 @@ use Yiisoft\Payments\Models\Customer; use Yiisoft\Payments\Models\PaymentIntent; use Yiisoft\Payments\Models\PaymentMethod; +use Yiisoft\Payments\Enums\PaymentIntentStatus; use Yiisoft\Payments\Gateways\StripeGateway; use Yiisoft\Payments\Tests\Support\TestHttpClient; use Nyholm\Psr7\Factory\Psr17Factory; @@ -149,7 +150,7 @@ public function testConfirmPaymentIntent(): void $result = $this->gateway->confirmPaymentIntent('pi_test123', ['return_url' => 'https://example.com/return']); $this->assertSame('pi_test123', $result->id); - $this->assertSame('succeeded', $result->status); + $this->assertSame(PaymentIntentStatus::Succeeded, $result->status); $lastRequest = $this->getLastRequest(); $this->assertSame('POST', $lastRequest['method']); From b72784fef40731d72b883d7a194e56c9dd347cef Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Mon, 16 Jun 2025 10:28:44 +0300 Subject: [PATCH 8/9] Adjust version --- README.md | 2 +- src/PaymentGatewayInterface.php | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6ffa812..a5b9253 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ A modern PHP 8.4+ library providing a unified interface for multiple payment gat ## Requirements -- PHP 8.1 or higher. +- PHP 8.2 or higher. ## Installation diff --git a/src/PaymentGatewayInterface.php b/src/PaymentGatewayInterface.php index 25ce8e8..e107f16 100644 --- a/src/PaymentGatewayInterface.php +++ b/src/PaymentGatewayInterface.php @@ -10,15 +10,13 @@ /** * Payment Gateway Interface - * + * * This interface defines the standard contract that all payment gateway implementations must follow. * It provides a consistent API for processing payments, managing customers, and handling payment methods * across different payment service providers. * * Implementations of this interface should handle the communication with specific payment gateways * (like Stripe, PayPal, etc.) while providing a unified interface to the application. - * - * @package Yiisoft\\Payments\\Core */ interface PaymentGatewayInterface { @@ -51,7 +49,7 @@ public function createPaymentMethod(PaymentMethod $paymentMethod): PaymentMethod * Attaches a payment method to a customer */ public function attachPaymentMethod( - string $paymentMethodId, + string $paymentMethodId, string $customerId ): PaymentMethod; From 4a43498613d020a8adc59f05d1e718c1f7836460 Mon Sep 17 00:00:00 2001 From: Rustam Date: Mon, 16 Jun 2025 16:34:05 +0500 Subject: [PATCH 9/9] Fix: remove PaymentMethodType enum --- README.md | 8 ++++---- src/Enums/PaymentMethodType.php | 12 ------------ src/Gateways/PayPalGateway.php | 8 ++++---- tests/Gateways/PayPalGatewayTest.php | 6 +++--- 4 files changed, 11 insertions(+), 23 deletions(-) delete mode 100644 src/Enums/PaymentMethodType.php diff --git a/README.md b/README.md index 002e1c2..f45a756 100644 --- a/README.md +++ b/README.md @@ -318,7 +318,7 @@ $stripe = new StripeGateway( #### PayPal ```php -use PaymentGateway\Gateways\PayPalGateway; +use \Yiisoft\Payments\Gateways\PayPalGateway; $paypal = new PayPalGateway( 'your-paypal-client-id', @@ -334,7 +334,7 @@ $paypal = new PayPalGateway( ```php // Create a customer -$customer = new \PaymentGateway\Models\Customer( +$customer = new \Yiisoft\Payments\Models\Customer( null, // id will be generated by the gateway 'customer@example.com', 'John Doe' @@ -357,9 +357,9 @@ $gateway->deleteCustomer('cus_123'); ```php // Create a payment method (e.g., card) -$paymentMethod = new \PaymentGateway\Models\PaymentMethod( +$paymentMethod = new \Yiisoft\Payments\Models\PaymentMethod( null, // id will be generated by the gateway - \Yiisoft\Payments\Enums\PaymentMethodType::Card, + \Yiisoft\Payments\Models\PaymentMethodType::CARD, [ 'number' => '4242424242424242', 'exp_month' => '12', diff --git a/src/Enums/PaymentMethodType.php b/src/Enums/PaymentMethodType.php deleted file mode 100644 index 3391e24..0000000 --- a/src/Enums/PaymentMethodType.php +++ /dev/null @@ -1,12 +0,0 @@ - $paymentMethod->customerId, 'type' => $paymentMethod->type, - $paymentMethod->type->value => $paymentMethod->details, + $paymentMethod->type => $paymentMethod->details, 'billing_details' => $paymentMethod->billingDetails, 'metadata' => $paymentMethod->metadata, ]; // In PayPal, payment methods are typically associated with orders, not directly with customers // This is a simplified implementation - return new PaymentMethod($paymentMethod->id, PaymentMethodType::PayPal, [], $paymentMethod->customerId); + return new PaymentMethod($paymentMethod->id, PaymentMethodType::PAYPAL, [], $paymentMethod->customerId); } public function attachPaymentMethod(string $paymentMethodId, string $customerId): PaymentMethod { // In PayPal, payment methods are typically associated with orders, not directly with customers // This is a simplified implementation - return new PaymentMethod($paymentMethodId, PaymentMethodType::PayPal, [], $customerId); + return new PaymentMethod($paymentMethodId, PaymentMethodType::PAYPAL, [], $customerId); } public function createPaymentIntent(PaymentIntent $intent): PaymentIntent diff --git a/tests/Gateways/PayPalGatewayTest.php b/tests/Gateways/PayPalGatewayTest.php index ca9fb1f..ba5a180 100644 --- a/tests/Gateways/PayPalGatewayTest.php +++ b/tests/Gateways/PayPalGatewayTest.php @@ -4,12 +4,12 @@ namespace Yiisoft\Payments\Tests\Gateways; -use Yiisoft\Payments\Enums\PaymentMethodType; use Yiisoft\Payments\Models\Customer; use Yiisoft\Payments\Models\PaymentIntent; use Yiisoft\Payments\Models\PaymentMethod; use Yiisoft\Payments\Enums\PaymentIntentStatus; use Yiisoft\Payments\Gateways\PayPalGateway; +use Yiisoft\Payments\Models\PaymentMethodType; use Yiisoft\Payments\Tests\Support\TestHttpClient; use PHPUnit\Framework\TestCase; @@ -161,7 +161,7 @@ public function testCreatePaymentMethod(): void $paymentMethod = new PaymentMethod( id: null, - type: PaymentMethodType::PayPal, + type: PaymentMethodType::PAYPAL, details: ['email' => 'test@example.com'], customerId: 'CUST-123', billingDetails: [ @@ -174,7 +174,7 @@ public function testCreatePaymentMethod(): void // For PayPal, we're just testing that the method returns a PaymentMethod with the same customer ID $result = $this->gateway->createPaymentMethod($paymentMethod); - $this->assertSame(PaymentMethodType::PayPal, $result->type); + $this->assertSame(PaymentMethodType::PAYPAL, $result->type); $this->assertSame('CUST-123', $result->customerId); // Since PayPal handles payment methods differently, we don't expect an API call here