Skip to content

Commit

Permalink
Merge pull request #13 from utopia-php/feat-future-payment-additional…
Browse files Browse the repository at this point in the history
…-options

add payment method options parameter
  • Loading branch information
lohanidamodar authored Feb 7, 2024
2 parents b134c83 + 3e706b6 commit 4032deb
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 7 deletions.
26 changes: 25 additions & 1 deletion src/Pay/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 49 additions & 1 deletion src/Pay/Adapter/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,71 @@ 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 = [
'customer' => $customerId,
'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);

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);
Expand Down
29 changes: 24 additions & 5 deletions src/Pay/Pay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
}
69 changes: 69 additions & 0 deletions tests/Pay/Adapter/StripeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 4032deb

Please sign in to comment.