Skip to content
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
10 changes: 10 additions & 0 deletions src/Pay/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ public function getCurrency(): string
*/
abstract public function purchase(int $amount, string $customerId, ?string $paymentMethodId = null, array $additionalParams = []): array;

/**
* Retry a purchase for a payment intent
*
* @param string $paymentId The payment intent ID to retry
* @param string|null $paymentMethodId The payment method to use (optional)
* @param array<mixed> $additionalParams Additional parameters for the retry (optional)
* @return array<mixed> The result of the retry attempt
*/
abstract public function retryPurchase(string $paymentId, ?string $paymentMethodId = null, array $additionalParams = []): array;

/**
* Refund payment
*
Expand Down
24 changes: 24 additions & 0 deletions src/Pay/Adapter/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ public function purchase(int $amount, string $customerId, ?string $paymentMethod
return $result;
}

/**
* Retry a purchase for a payment intent
*
* @param string $paymentId The payment intent ID to retry
* @param string|null $paymentMethodId The payment method to use (optional)
* @param array<mixed> $additionalParams Additional parameters for the retry (optional)
* @return array<mixed> The result of the retry attempt
*/
public function retryPurchase(string $paymentId, ?string $paymentMethodId = null, array $additionalParams = []): array
{
$path = '/payment_intents/'.$paymentId.'/confirm';
$requestBody = [];
if (! empty($paymentMethodId)) {
$requestBody = [
'payment_method' => $paymentMethodId,
];
}

$requestBody = array_merge($requestBody, $additionalParams);
$result = $this->execute(self::METHOD_POST, $path, $requestBody);

return $result;
}

/**
* Refund payment
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Pay/Pay.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ public function purchase(int $amount, string $customerId, string $paymentMethodI
return $this->adapter->purchase($amount, $customerId, $paymentMethodId, $additionalParams);
}

/**
* Retry a purchase for a payment intent
*
* @param string $paymentId The payment intent ID to retry
* @param string|null $paymentMethodId The payment method to use (optional)
* @param array<mixed> $additionalParams Additional parameters for the retry (optional)
* @return array<mixed> The result of the retry attempt
*/
public function retryPurchase(string $paymentId, ?string $paymentMethodId = null, array $additionalParams = []): array
{
return $this->adapter->retryPurchase($paymentId, $paymentMethodId, $additionalParams);
}

/**
* Refund Payment
*
Expand Down
59 changes: 59 additions & 0 deletions tests/Pay/Adapter/StripeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,65 @@ public function testPurchase(array $data): array
return $data;
}

/**
* Test retryPurchase: create a payment with a failing payment method, then retry with a succeeding one.
*
* @depends testCreateCustomer
*
* @param array<mixed> $data
* @return array<mixed>
*/
public function testRetryPurchase(array $data): array
{
$customerId = $data['customerId'];
// Create a payment method that will fail (card_declined)
$failingPm = $this->stripe->createPaymentMethod($customerId, 'card', [
'number' => '4000000000000341',
'exp_month' => 8,
'exp_year' => 2030,
'cvc' => 123,
]);
$this->assertNotEmpty($failingPm['id']);
$failingPmId = $failingPm['id'];

// Create a payment intent with the failing payment method
$paymentIntentId = null;
try {
$this->stripe->purchase(5000, $customerId, $failingPmId);
$this->fail('Expected payment to fail');
} catch (Exception $e) {
$this->assertEquals(Exception::GENERIC_DECLINE, $e->getType());
$this->assertEquals(402, $e->getCode());
$paymentIntentMeta = $e->getMetadata()['payment_intent'] ?? null;
$paymentIntentId = is_array($paymentIntentMeta) && isset($paymentIntentMeta['id']) ? $paymentIntentMeta['id'] : $paymentIntentMeta;
$this->assertNotEmpty($paymentIntentId);
}

// Create a succeeding payment method
$succeedingPm = $this->stripe->createPaymentMethod($customerId, 'card', [
'number' => '4242424242424242', // Stripe test card: always succeeds
'exp_month' => 8,
'exp_year' => 2030,
'cvc' => 123,
]);
$this->assertNotEmpty($succeedingPm['id']);
$succeedingPmId = $succeedingPm['id'];

// Retry the payment intent with the succeeding payment method
$result = $this->stripe->retryPurchase((string) $paymentIntentId, $succeedingPmId);
$this->assertNotEmpty($result['id']);
$this->assertEquals($paymentIntentId, $result['id']);
$this->assertEquals('payment_intent', $result['object']);
$this->assertArrayHasKey('status', $result);
$this->assertEquals('succeeded', $result['status']);

// Save for further tests if needed
$data['paymentId'] = $paymentIntentId;
$data['paymentMethodId'] = $succeedingPmId;

return $data;
}

/**
* @depends testPurchase
*/
Expand Down