Skip to content

Commit

Permalink
Merge pull request #6 from utopia-php/feat-payment-intent
Browse files Browse the repository at this point in the history
Payment updates
  • Loading branch information
eldadfux authored Feb 20, 2023
2 parents 771cf84 + e585f37 commit ec95250
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
14 changes: 13 additions & 1 deletion src/Pay/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
41 changes: 37 additions & 4 deletions src/Pay/Adapter/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 = [
Expand All @@ -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);

Expand Down Expand Up @@ -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);
}
}
23 changes: 22 additions & 1 deletion src/Pay/Pay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
}
9 changes: 4 additions & 5 deletions tests/Pay/Adapter/StripeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\TestCase;
use Utopia\Pay\Adapter\Stripe;
use Utopia\Pay\Address;

class StripeTest extends TestCase
{
Expand All @@ -27,7 +26,7 @@ public function testName()

public function testCreateCustomer(): array
{
$customer = $this->stripe->createCustomer('Test customer', '[email protected]', new Address('Kathmandu', 'NP', 'Gaurighat', 'Pambu Marga', '44600', 'Bagmati'));
$customer = $this->stripe->createCustomer('Test customer', '[email protected]', ['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'], '[email protected]');
Expand Down Expand Up @@ -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'];

Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit ec95250

Please sign in to comment.