diff --git a/src/Pay/Adapter.php b/src/Pay/Adapter.php index 059ab83..2c72014 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 @@ -135,6 +135,18 @@ 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; + /** * Call * Make a request diff --git a/src/Pay/Adapter/Stripe.php b/src/Pay/Adapter/Stripe.php index ded22bb..4a19e44 100644 --- a/src/Pay/Adapter/Stripe.php +++ b/src/Pay/Adapter/Stripe.php @@ -83,6 +83,26 @@ public function listCards(string $customerId): array return $this->execute(self::METHOD_GET, $path); } + /** + * List Customer Payment Methods + */ + public function listCustomerPaymentMethods(string $customerId): array + { + $path = '/customers/'.$customerId.'/payment_methods'; + + return $this->execute(self::METHOD_GET, $path); + } + + /** + * List Customer Payment Methods + */ + public function getCustomerPaymentMethod(string $customerId, string $paymentMethodId): array + { + $path = '/customers/'.$customerId.'/payment_methods/'.$paymentMethodId; + + return $this->execute(self::METHOD_GET, $path); + } + /** * Update card */ @@ -138,7 +158,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 = [ @@ -149,7 +169,7 @@ public function createCustomer(string $name, string $email, Address $address = n $requestBody['payment_method'] = $paymentMethod; } if (! is_null($address)) { - $requestBody['address'] = $address->asArray(); + $requestBody['address'] = $address; } $result = $this->execute(self::METHOD_POST, $path, $requestBody); @@ -206,10 +226,23 @@ 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); + $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); } } diff --git a/src/Pay/Pay.php b/src/Pay/Pay.php index a4e816b..3dcddbe 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); } @@ -224,4 +224,25 @@ 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); + } } diff --git a/tests/Pay/Adapter/StripeTest.php b/tests/Pay/Adapter/StripeTest.php index b8b7ed1..8393ec7 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', new Address('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'); @@ -78,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']; @@ -94,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; @@ -108,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;