Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add payment method options parameter #13

Merged
merged 8 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come all the functions use the same endpoint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they do, create and list is same with different HTTP method.

$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
Loading