From 838c48e6f4f65484cc6055ada9d6e5f61f168bb4 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 7 Feb 2023 07:30:14 +0000 Subject: [PATCH 01/13] future payment intent --- src/Pay/Adapter.php | 2 ++ src/Pay/Adapter/Stripe.php | 12 ++++++++++++ src/Pay/Pay.php | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/src/Pay/Adapter.php b/src/Pay/Adapter.php index 059ab83..7518270 100644 --- a/src/Pay/Adapter.php +++ b/src/Pay/Adapter.php @@ -135,6 +135,8 @@ abstract public function updateCustomer(string $customerId, string $name, string */ abstract public function deleteCustomer(string $customerId): bool; + abstract public function createFuturePayment(string $customerId, array $paymentMethodTypes = []): array; + /** * Call * Make a request diff --git a/src/Pay/Adapter/Stripe.php b/src/Pay/Adapter/Stripe.php index ded22bb..3c19652 100644 --- a/src/Pay/Adapter/Stripe.php +++ b/src/Pay/Adapter/Stripe.php @@ -206,6 +206,18 @@ public function deleteCustomer(string $customerId): bool return $result['deleted'] ?? false; } + public function createFuturePayment(string $customerId, array $paymentMethodTypes = ['card']): array + { + $path = '/setup_intents'; + $requestBody = [ + 'customer' => $customerId, + 'payment_method_types' => $paymentMethodTypes + ]; + + $result = $this->execute(self::METHOD_POST, $path, $requestBody); + return $result; + } + private function execute(string $method, string $path, array $requestBody = [], array $headers = []): array { $headers = array_merge(['content-type' => 'application/x-www-form-urlencoded'], $headers); diff --git a/src/Pay/Pay.php b/src/Pay/Pay.php index a4e816b..2490493 100644 --- a/src/Pay/Pay.php +++ b/src/Pay/Pay.php @@ -224,4 +224,9 @@ public function deleteCustomer(string $customerId): bool { return $this->adapter->deleteCustomer($customerId); } + + public function createFuturePayment(string $customerId, array $paymentMethodTypes = ['card']): array + { + return $this->adapter->createFuturePayment($customerId, $paymentMethodTypes); + } } From f136b09e0cfa09cff19b787416172fdfb24cab97 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 7 Feb 2023 07:56:25 +0000 Subject: [PATCH 02/13] fix error --- src/Pay/Pay.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Pay/Pay.php b/src/Pay/Pay.php index 2490493..94c0181 100644 --- a/src/Pay/Pay.php +++ b/src/Pay/Pay.php @@ -209,7 +209,7 @@ public function getCustomer(string $customerId): array * @param string $paymentMethod * @return array */ - public function updateCustomer(string $customerId, string $name, string $email, string $paymentMethod, array $address = []): array + public function updateCustomer(string $customerId, string $name, string $email, string $paymentMethod, array $address = null): array { return $this->adapter->updateCustomer($customerId, $name, $email, $address, $paymentMethod); } From 60daf69a9027e854ecc687e476d13fe1231e2d31 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 8 Feb 2023 07:23:39 +0000 Subject: [PATCH 03/13] fix type error --- src/Pay/Adapter.php | 2 +- src/Pay/Adapter/Stripe.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Pay/Adapter.php b/src/Pay/Adapter.php index 7518270..26bb78b 100644 --- a/src/Pay/Adapter.php +++ b/src/Pay/Adapter.php @@ -110,7 +110,7 @@ abstract public function deleteCard(string $customerId, string $cardId): bool; * * @throws Exception */ - abstract public function createCustomer(string $name, string $email, Address $address = null, string $paymentMethod = null): array; + abstract public function createCustomer(string $name, string $email, array $address = [], string $paymentMethod = null): array; /** * List customers diff --git a/src/Pay/Adapter/Stripe.php b/src/Pay/Adapter/Stripe.php index 3c19652..08e1416 100644 --- a/src/Pay/Adapter/Stripe.php +++ b/src/Pay/Adapter/Stripe.php @@ -138,7 +138,7 @@ public function deleteCard(string $customerId, string $cardId): bool * * @throws Exception */ - public function createCustomer(string $name, string $email, Address $address = null, string $paymentMethod = null): array + public function createCustomer(string $name, string $email, array $address = [], string $paymentMethod = null): array { $path = '/customers'; $requestBody = [ From ee5f05cc2871b8f025a564b6281aaf21aca59fce Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 8 Feb 2023 07:28:08 +0000 Subject: [PATCH 04/13] fix address --- src/Pay/Adapter/Stripe.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Pay/Adapter/Stripe.php b/src/Pay/Adapter/Stripe.php index 08e1416..5b19b59 100644 --- a/src/Pay/Adapter/Stripe.php +++ b/src/Pay/Adapter/Stripe.php @@ -149,7 +149,7 @@ public function createCustomer(string $name, string $email, array $address = [], $requestBody['payment_method'] = $paymentMethod; } if (! is_null($address)) { - $requestBody['address'] = $address->asArray(); + $requestBody['address'] = $address; } $result = $this->execute(self::METHOD_POST, $path, $requestBody); From 60d2f72d54fdfbb9dcc89c3c38504fd04cd906b2 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 8 Feb 2023 07:39:12 +0000 Subject: [PATCH 05/13] send authorization header --- src/Pay/Adapter/Stripe.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Pay/Adapter/Stripe.php b/src/Pay/Adapter/Stripe.php index 5b19b59..090f374 100644 --- a/src/Pay/Adapter/Stripe.php +++ b/src/Pay/Adapter/Stripe.php @@ -220,8 +220,8 @@ public function createFuturePayment(string $customerId, array $paymentMethodType private function execute(string $method, string $path, array $requestBody = [], array $headers = []): array { - $headers = array_merge(['content-type' => 'application/x-www-form-urlencoded'], $headers); + $headers = array_merge(['content-type' => 'application/x-www-form-urlencoded', 'Authorization' => 'Bearer ' . $this->secretKey], $headers); - return $this->call($method, $this->baseUrl.$path, $requestBody, $headers, [CURLOPT_USERPWD => $this->secretKey.':']); + return $this->call($method, $this->baseUrl.$path, $requestBody, $headers); } } From 8d89d87b7c64b8db58cc29a7a4dc83a851534938 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 10 Feb 2023 06:30:16 +0000 Subject: [PATCH 06/13] update to get pyament method --- src/Pay/Adapter.php | 10 ++++++++++ src/Pay/Adapter/Stripe.php | 18 ++++++++++++++++++ src/Pay/Pay.php | 16 ++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/Pay/Adapter.php b/src/Pay/Adapter.php index 26bb78b..2c72014 100644 --- a/src/Pay/Adapter.php +++ b/src/Pay/Adapter.php @@ -135,6 +135,16 @@ abstract public function updateCustomer(string $customerId, string $name, string */ abstract public function deleteCustomer(string $customerId): bool; + /** + * List Customer Payment Methods + */ + abstract public function listCustomerPaymentMethods(string $customerId): array; + + /** + * List Customer Payment Methods + */ + abstract public function getCustomerPaymentMethod(string $customerId, string $paymentMethodId): array; + abstract public function createFuturePayment(string $customerId, array $paymentMethodTypes = []): array; /** diff --git a/src/Pay/Adapter/Stripe.php b/src/Pay/Adapter/Stripe.php index 090f374..a2f2ac6 100644 --- a/src/Pay/Adapter/Stripe.php +++ b/src/Pay/Adapter/Stripe.php @@ -83,6 +83,24 @@ public function listCards(string $customerId): array return $this->execute(self::METHOD_GET, $path); } + /** + * List Customer Payment Methods + */ + public function listCustomerPaymentMethods(string $customerId): array + { + $path = '/customer/' . $customerId . '/payment_methods'; + return $this->execute(self::METHOD_GET, $path); + } + + /** + * List Customer Payment Methods + */ + public function getCustomerPaymentMethod(string $customerId, string $paymentMethodId): array + { + $path = '/customer/' . $customerId . '/payment_methods/' . $paymentMethodId; + return $this->execute(self::METHOD_GET, $path); + } + /** * Update card */ diff --git a/src/Pay/Pay.php b/src/Pay/Pay.php index 94c0181..6627e78 100644 --- a/src/Pay/Pay.php +++ b/src/Pay/Pay.php @@ -225,6 +225,22 @@ public function deleteCustomer(string $customerId): bool return $this->adapter->deleteCustomer($customerId); } + /** + * List Customer Payment Methods + */ + public function listCustomerPaymentMethods(string $customerId): array + { + return $this->adapter->listCustomerPaymentMethods($customerId); + } + + /** + * List Customer Payment Methods + */ + public function getCustomerPaymentMethod(string $customerId, string $paymentMethodId): array + { + return $this->adapter->getCustomerPaymentMethod($customerId, $paymentMethodId); + } + public function createFuturePayment(string $customerId, array $paymentMethodTypes = ['card']): array { return $this->adapter->createFuturePayment($customerId, $paymentMethodTypes); From f1b8c01ca65f4c2a767637c7a74e89445e946c72 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 10 Feb 2023 07:12:06 +0000 Subject: [PATCH 07/13] fix path --- src/Pay/Adapter/Stripe.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Pay/Adapter/Stripe.php b/src/Pay/Adapter/Stripe.php index a2f2ac6..93d4f9f 100644 --- a/src/Pay/Adapter/Stripe.php +++ b/src/Pay/Adapter/Stripe.php @@ -88,7 +88,7 @@ public function listCards(string $customerId): array */ public function listCustomerPaymentMethods(string $customerId): array { - $path = '/customer/' . $customerId . '/payment_methods'; + $path = '/customers/' . $customerId . '/payment_methods'; return $this->execute(self::METHOD_GET, $path); } @@ -97,7 +97,7 @@ public function listCustomerPaymentMethods(string $customerId): array */ public function getCustomerPaymentMethod(string $customerId, string $paymentMethodId): array { - $path = '/customer/' . $customerId . '/payment_methods/' . $paymentMethodId; + $path = '/customers/' . $customerId . '/payment_methods/' . $paymentMethodId; return $this->execute(self::METHOD_GET, $path); } From 5191018bbd987c44d238d58d207c3314b2236f92 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 13 Feb 2023 05:42:57 +0000 Subject: [PATCH 08/13] fix formatting --- src/Pay/Adapter/Stripe.php | 11 +++++++---- src/Pay/Pay.php | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Pay/Adapter/Stripe.php b/src/Pay/Adapter/Stripe.php index 93d4f9f..4a19e44 100644 --- a/src/Pay/Adapter/Stripe.php +++ b/src/Pay/Adapter/Stripe.php @@ -88,7 +88,8 @@ public function listCards(string $customerId): array */ public function listCustomerPaymentMethods(string $customerId): array { - $path = '/customers/' . $customerId . '/payment_methods'; + $path = '/customers/'.$customerId.'/payment_methods'; + return $this->execute(self::METHOD_GET, $path); } @@ -97,7 +98,8 @@ public function listCustomerPaymentMethods(string $customerId): array */ public function getCustomerPaymentMethod(string $customerId, string $paymentMethodId): array { - $path = '/customers/' . $customerId . '/payment_methods/' . $paymentMethodId; + $path = '/customers/'.$customerId.'/payment_methods/'.$paymentMethodId; + return $this->execute(self::METHOD_GET, $path); } @@ -229,16 +231,17 @@ public function createFuturePayment(string $customerId, array $paymentMethodType $path = '/setup_intents'; $requestBody = [ 'customer' => $customerId, - 'payment_method_types' => $paymentMethodTypes + 'payment_method_types' => $paymentMethodTypes, ]; $result = $this->execute(self::METHOD_POST, $path, $requestBody); + return $result; } 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); + $headers = array_merge(['content-type' => 'application/x-www-form-urlencoded', 'Authorization' => 'Bearer '.$this->secretKey], $headers); return $this->call($method, $this->baseUrl.$path, $requestBody, $headers); } diff --git a/src/Pay/Pay.php b/src/Pay/Pay.php index 6627e78..3dcddbe 100644 --- a/src/Pay/Pay.php +++ b/src/Pay/Pay.php @@ -225,7 +225,7 @@ public function deleteCustomer(string $customerId): bool return $this->adapter->deleteCustomer($customerId); } - /** + /** * List Customer Payment Methods */ public function listCustomerPaymentMethods(string $customerId): array From aaff5f4baaec3c4663b0be6ce4f71779b42674b9 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 13 Feb 2023 05:43:38 +0000 Subject: [PATCH 09/13] fix test --- tests/Pay/Adapter/StripeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Pay/Adapter/StripeTest.php b/tests/Pay/Adapter/StripeTest.php index b8b7ed1..e922be7 100644 --- a/tests/Pay/Adapter/StripeTest.php +++ b/tests/Pay/Adapter/StripeTest.php @@ -27,7 +27,7 @@ public function testName() public function testCreateCustomer(): array { - $customer = $this->stripe->createCustomer('Test customer', 'testcustomer@email.com', new Address('Kathmandu', 'NP', 'Gaurighat', 'Pambu Marga', '44600', 'Bagmati')); + $customer = $this->stripe->createCustomer('Test customer', 'testcustomer@email.com', array('Kathmandu', 'NP', 'Gaurighat', 'Pambu Marga', '44600', 'Bagmati')); $this->assertNotEmpty($customer['id']); $this->assertEquals($customer['name'], 'Test customer'); $this->assertEquals($customer['email'], 'testcustomer@email.com'); From a4a76d4408aad44217e8ef7fab4b4ed257ee8717 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 20 Feb 2023 04:13:40 +0000 Subject: [PATCH 10/13] update --- tests/Pay/Adapter/StripeTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Pay/Adapter/StripeTest.php b/tests/Pay/Adapter/StripeTest.php index e922be7..3c652ce 100644 --- a/tests/Pay/Adapter/StripeTest.php +++ b/tests/Pay/Adapter/StripeTest.php @@ -4,7 +4,6 @@ use PHPUnit\Framework\TestCase; use Utopia\Pay\Adapter\Stripe; -use Utopia\Pay\Address; class StripeTest extends TestCase { @@ -27,7 +26,7 @@ public function testName() public function testCreateCustomer(): array { - $customer = $this->stripe->createCustomer('Test customer', 'testcustomer@email.com', array('Kathmandu', 'NP', 'Gaurighat', 'Pambu Marga', '44600', 'Bagmati')); + $customer = $this->stripe->createCustomer('Test customer', 'testcustomer@email.com', ['Kathmandu', 'NP', 'Gaurighat', 'Pambu Marga', '44600', 'Bagmati']); $this->assertNotEmpty($customer['id']); $this->assertEquals($customer['name'], 'Test customer'); $this->assertEquals($customer['email'], 'testcustomer@email.com'); From 753d6814458592ca8b2762600f2b243bb8509819 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 20 Feb 2023 04:21:43 +0000 Subject: [PATCH 11/13] fix test --- tests/Pay/Adapter/StripeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Pay/Adapter/StripeTest.php b/tests/Pay/Adapter/StripeTest.php index 3c652ce..7b8428e 100644 --- a/tests/Pay/Adapter/StripeTest.php +++ b/tests/Pay/Adapter/StripeTest.php @@ -26,7 +26,7 @@ public function testName() public function testCreateCustomer(): array { - $customer = $this->stripe->createCustomer('Test customer', 'testcustomer@email.com', ['Kathmandu', 'NP', 'Gaurighat', 'Pambu Marga', '44600', 'Bagmati']); + $customer = $this->stripe->createCustomer('Test customer', 'testcustomer@email.com', ['city' => 'Kathmandu', 'country' => 'NP', 'line1' => 'Gaurighat', 'line2' => 'Pambu Marga', 'postal_code' => '44600', 'state' => 'Bagmati']); $this->assertNotEmpty($customer['id']); $this->assertEquals($customer['name'], 'Test customer'); $this->assertEquals($customer['email'], 'testcustomer@email.com'); From b838957298a8ff7c6c5e82b7b0e5e78d7f183e6c Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 20 Feb 2023 04:28:10 +0000 Subject: [PATCH 12/13] update year --- tests/Pay/Adapter/StripeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Pay/Adapter/StripeTest.php b/tests/Pay/Adapter/StripeTest.php index 7b8428e..304368b 100644 --- a/tests/Pay/Adapter/StripeTest.php +++ b/tests/Pay/Adapter/StripeTest.php @@ -77,7 +77,7 @@ public function testCreateCard(array $data) $this->assertNotEmpty($card['id']); $this->assertEquals('Visa', $card['brand']); $this->assertEquals('US', $card['country']); - $this->assertEquals(2023, $card['exp_year']); + $this->assertEquals(2024, $card['exp_year']); $this->assertEquals(date('m'), $card['exp_month']); $data['cardId'] = $card['id']; From e585f375b4cd539b8a52a0b0440c31810090d78c Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 20 Feb 2023 04:32:01 +0000 Subject: [PATCH 13/13] fix more years --- tests/Pay/Adapter/StripeTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Pay/Adapter/StripeTest.php b/tests/Pay/Adapter/StripeTest.php index 304368b..8393ec7 100644 --- a/tests/Pay/Adapter/StripeTest.php +++ b/tests/Pay/Adapter/StripeTest.php @@ -93,7 +93,7 @@ public function testListCards(array $data) $this->assertNotEmpty($card['id']); $this->assertEquals('Visa', $card['brand']); $this->assertEquals('US', $card['country']); - $this->assertEquals(2023, $card['exp_year']); + $this->assertEquals(2024, $card['exp_year']); $this->assertEquals(date('m'), $card['exp_month']); return $data; @@ -107,7 +107,7 @@ public function testGetCard(array $data) $this->assertNotEmpty($card['id']); $this->assertEquals('Visa', $card['brand']); $this->assertEquals('US', $card['country']); - $this->assertEquals(2023, $card['exp_year']); + $this->assertEquals(2024, $card['exp_year']); $this->assertEquals(date('m'), $card['exp_month']); return $data;