diff --git a/src/Pay/Adapter.php b/src/Pay/Adapter.php index 0232f54..62b3956 100644 --- a/src/Pay/Adapter.php +++ b/src/Pay/Adapter.php @@ -141,10 +141,34 @@ abstract public function getPaymentMethod(string $customerId, string $paymentMet * Create setup for accepting future payments * * @param string $customerId + * @param string|null $paymentMethod * @param array $paymentMethodTypes + * @param array $paymentMethodOptions + * @param ?string $paymentMethodConfiguration * @return array */ - abstract public function createFuturePayment(string $customerId, array $paymentMethodTypes = [], ?string $paymentMethodConfiguration = null): array; + abstract public function createFuturePayment(string $customerId, ?string $paymentMethod = null, array $paymentMethodTypes = [], array $paymentMethodOptions = [], ?string $paymentMethodConfiguration = null): array; + + /** + * List future payments associated with the provided customer or payment method + * + * @param string|null $customerId + * @param string|null $paymentMethodId + * @return array + */ + abstract public function listFuturePayments(?string $customerId = null, ?string $paymentMethodId = null): array; + + /** + * Update future payment setup + * + * @param string $id, + * @param string $customerId + * @param string|null $paymentMethod + * @param array $paymentMethodOptions + * @param string|null $paymentMethodConfiguration + * @return array + */ + abstract public function updateFuturePayment(string $id, ?string $customerId = null, ?string $paymentMethod = null, array $paymentMethodOptions = [], ?string $paymentMethodConfiguration = null): array; /** * Call diff --git a/src/Pay/Adapter/Stripe.php b/src/Pay/Adapter/Stripe.php index 12d2ef7..363f092 100644 --- a/src/Pay/Adapter/Stripe.php +++ b/src/Pay/Adapter/Stripe.php @@ -229,7 +229,7 @@ public function deleteCustomer(string $customerId): bool return $result['deleted'] ?? false; } - public function createFuturePayment(string $customerId, array $paymentMethodTypes = ['card'], ?string $paymentMethodConfiguration = null): array + public function createFuturePayment(string $customerId, ?string $paymentMethod = null, array $paymentMethodTypes = ['card'], array $paymentMethodOptions = [], ?string $paymentMethodConfiguration = null): array { $path = '/setup_intents'; $requestBody = [ @@ -237,8 +237,20 @@ public function createFuturePayment(string $customerId, array $paymentMethodType 'payment_method_types' => $paymentMethodTypes, ]; + if ($paymentMethod != null) { + $requestBody['payment_method'] = $paymentMethod; + } + if ($paymentMethodConfiguration != null) { $requestBody['payment_method_configuration'] = $paymentMethodConfiguration; + $requestBody['automatic_payment_methods'] = [ + 'enabled' => 'true', + ]; + unset($requestBody['payment_method_types']); + } + + if (! empty($paymentMethodOptions)) { + $requestBody['payment_method_options'] = $paymentMethodOptions; } $result = $this->execute(self::METHOD_POST, $path, $requestBody); @@ -246,6 +258,42 @@ public function createFuturePayment(string $customerId, array $paymentMethodType return $result; } + public function listFuturePayments(?string $customerId = null, ?string $pyamentMethodId = null): array + { + $path = '/setup_intents'; + $requestBody = []; + if ($customerId != null) { + $requestBody['customer'] = $customerId; + } + + if ($pyamentMethodId != null) { + $requestBody['payment_method'] = $pyamentMethodId; + } + $result = $this->execute(self::METHOD_GET, $path, $requestBody); + + return $result; + } + + public function updateFuturePayment(string $id, ?string $customerId = null, ?string $paymentMethod = null, array $paymentMethodOptions = [], ?string $paymentMethodConfiguration = null): array + { + $path = '/setup_intents/'.$id; + $requestBody = []; + if ($customerId != null) { + $requestBody['customer'] = $customerId; + } + if ($paymentMethod != null) { + $requestBody['payment_method'] = $paymentMethod; + } + if ($paymentMethodConfiguration != null) { + $requestBody['payment_method_configuration'] = $paymentMethodConfiguration; + } + if (! empty($paymentMethodOptions)) { + $requestBody['payment_method_options'] = $paymentMethodOptions; + } + + return $this->execute(self::METHOD_POST, $path, $requestBody); + } + private function execute(string $method, string $path, array $requestBody = [], array $headers = []): array { $headers = array_merge(['content-type' => 'application/x-www-form-urlencoded', 'Authorization' => 'Bearer '.$this->secretKey], $headers); diff --git a/src/Pay/Pay.php b/src/Pay/Pay.php index 4d3da2d..89d1a66 100644 --- a/src/Pay/Pay.php +++ b/src/Pay/Pay.php @@ -218,11 +218,11 @@ public function getCustomer(string $customerId): array * @param string $customerId * @param string $name * @param string $email - * @param array $billingDetails * @param string $paymentMethod + * @param Address $address * @return array */ - public function updateCustomer(string $customerId, string $name, string $email, string $paymentMethod, array $address = null): array + public function updateCustomer(string $customerId, string $name, string $email, Address $address = null, ?string $paymentMethod = null): array { return $this->adapter->updateCustomer($customerId, $name, $email, $address, $paymentMethod); } @@ -242,12 +242,31 @@ public function deleteCustomer(string $customerId): bool * Create Setup for accepting future payments * * @param string $customerId + * @param string|null $paymentMethod * @param array $paymentMethodTypes - * @param ?string $paymentMethodConfiguration + * @param array $paymentMethodOptions + * @param string $paymentMethodConfiguration + * @return array + */ + public function createFuturePayment(string $customerId, ?string $paymentMethod = null, array $paymentMethodTypes = ['card'], array $paymentMethodOptions = [], ?string $paymentMethodConfiguration = null): array + { + return $this->adapter->createFuturePayment($customerId, $paymentMethod, $paymentMethodTypes, $paymentMethodOptions, $paymentMethodConfiguration); + } + + public function updateFuturePayment(string $id, ?string $customerId = null, ?string $paymentMethod = null, array $paymentMethodOptions = [], ?string $paymentMethodConfiguration = null): array + { + return $this->adapter->updateFuturePayment($id, $customerId, $paymentMethod, $paymentMethodOptions, $paymentMethodConfiguration); + } + + /** + * List future payment + * + * @param string|null $customerId + * @param string|null $paymentMethodId * @return array */ - public function createFuturePayment(string $customerId, array $paymentMethodTypes = ['card'], ?string $paymentMethodConfiguration = null): array + public function listFuturePayment(?string $customerId, ?string $paymentMethodId = null): array { - return $this->adapter->createFuturePayment($customerId, $paymentMethodTypes); + return $this->adapter->listFuturePayments($customerId, $paymentMethodId); } } diff --git a/tests/Pay/Adapter/StripeTest.php b/tests/Pay/Adapter/StripeTest.php index 2406cfb..4af59c1 100644 --- a/tests/Pay/Adapter/StripeTest.php +++ b/tests/Pay/Adapter/StripeTest.php @@ -134,6 +134,75 @@ public function testGetPaymentMethod(array $data) return $data; } + /** @depends testCreatePaymentMethod */ + public function testCreateFuturePayment(array $data) + { + $customerId = $data['customerId']; + $setupIntent = $this->stripe->createFuturePayment($customerId, paymentMethodOptions: [ + 'card' => [ + 'mandate_options' => [ + 'reference' => \uniqid(), + 'description' => 'Utopia pay test', + 'amount' => 15000, + 'currency' => 'USD', + 'start_date' => time(), + 'amount_type' => 'maximum', + 'interval' => 'day', + 'interval_count' => 30, + 'supported_types' => ['india'], + ], + ], + ]); + $this->assertNotEmpty($setupIntent); + $this->assertNotEmpty($setupIntent['client_secret']); + $data['setupIntentId'] = $setupIntent['id']; + + return $data; + } + + /** @depends testCreateFuturePayment */ + public function testUpdateFuturePayment(array $data) + { + $customerId = $data['customerId']; + $setupIntentId = $data['setupIntentId']; + + $reference = uniqid(); + $setupIntent = $this->stripe->updateFuturePayment($setupIntentId, $customerId, paymentMethodOptions: [ + 'card' => [ + 'mandate_options' => [ + 'reference' => $reference, + 'description' => 'Utopia monthly subscription', + 'amount' => 1500, + 'currency' => 'USD', + 'start_date' => time(), + 'amount_type' => 'maximum', + 'interval' => 'day', + 'interval_count' => 5, + 'supported_types' => ['india'], + ], + ], + ]); + + $this->assertNotEmpty($setupIntent); + $this->assertEquals($setupIntentId, $setupIntent['id']); + $this->assertIsArray($setupIntent['payment_method_options']); + $this->assertArrayHasKey('card', $setupIntent['payment_method_options']); + $this->assertArrayHasKey('mandate_options', $setupIntent['payment_method_options']['card']); + $this->assertEquals($reference, $setupIntent['payment_method_options']['card']['mandate_options']['reference']); + } + + /** @depends testCreateFuturePayment */ + public function testListFuturePayment(array $data) + { + $customerId = $data['customerId']; + $setupIntentId = $data['setupIntentId']; + + $setupIntents = $this->stripe->listFuturePayments($customerId); + $this->assertNotEmpty($setupIntents['data'] ?? []); + $this->assertCount(1, $setupIntents['data'] ?? []); + $this->assertEquals($setupIntentId, $setupIntents['data'][0]['id']); + } + /** @depends testCreatePaymentMethod */ public function testUpdatePaymentMethod(array $data) {