Skip to content

Commit

Permalink
Merge pull request #273 from craftcms/bugfix/265-direct-debit-payment…
Browse files Browse the repository at this point in the history
…-support

Update support for direct debit payment methods and sources
  • Loading branch information
nfourtythree authored Dec 19, 2023
2 parents 5055572 + a74d432 commit 6cbadfe
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

## Unreleased

- Fixed a bug where failed PayPal payments would cause infinite redirects. ([#266](https://github.com/craftcms/commerce-stripe/issues/266))
- Stripe for Craft Commerce now requires Commerce 4.3.3 or later.
- It is now possible to create SEPA and Bacs Direct Debit payment sources.
- Payment method data is now stored in expanded form within transaction response data. ([#276](https://github.com/craftcms/commerce-stripe/pull/276))
- Billing address information is now passed to the payment intent. ([#257](https://github.com/craftcms/commerce-stripe/issues/257), [#258](https://github.com/craftcms/commerce-stripe/issues/263))
- Fixed a bug where it wasn’t possible to pay using the SEPA Direct Debit payment method. ([#265](https://github.com/craftcms/commerce/issues/265))
- Fixed a bug where failed PayPal payments would cause infinite redirects. ([#266](https://github.com/craftcms/commerce-stripe/issues/266))
- Fixed a bug where JavaScript files were being served incorrectly. ([#270](https://github.com/craftcms/commerce-stripe/issues/270))
- Added `craft\commerce\stripe\SubscriptionGateway::handlePaymentIntentSucceeded()`.

## 4.0.1.1 - 2023-10-25

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"php": "^8.0.2",
"craftcms/cms": "^4.0.0",
"stripe/stripe-php": "^10.0",
"craftcms/commerce": "^4.3"
"craftcms/commerce": "^4.3.3"
},
"require-dev": {
"craftcms/phpstan": "dev-main",
Expand Down
55 changes: 53 additions & 2 deletions src/base/SubscriptionGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use craft\commerce\models\subscriptions\SubscriptionForm as BaseSubscriptionForm;
use craft\commerce\models\subscriptions\SubscriptionPayment;
use craft\commerce\models\subscriptions\SwitchPlansForm;
use craft\commerce\models\Transaction;
use craft\commerce\Plugin;
use craft\commerce\Plugin as CommercePlugin;
use craft\commerce\records\Transaction as TransactionRecord;
Expand Down Expand Up @@ -484,6 +485,9 @@ public function handleWebhook(array $data): void
case 'payment_method.detached':
$this->handlePaymentMethodDetached($data);
break;
case 'payment_intent.succeeded':
$this->handlePaymentIntentSucceeded($data);
break;
case 'charge.refunded':
$this->handleRefunded($data);
break;
Expand Down Expand Up @@ -517,6 +521,47 @@ public function handleWebhook(array $data): void
parent::handleWebhook($data);
}

/**
* @param array $data
* @return void
* @throws InvalidConfigException
* @since 4.1.0
*/
public function handlePaymentIntentSucceeded(array $data): void
{
$paymentIntent = $data['data']['object'];
if ($paymentIntent['object'] === 'payment_intent') {
$transaction = Plugin::getInstance()->getTransactions()->getTransactionByReference($paymentIntent['id']);
$updateTransaction = null;

if ($transaction->parentId === null) {
$children = Plugin::getInstance()->getTransactions()->getChildrenByTransactionId($transaction->id);

if (empty($children) && $transaction->status === TransactionRecord::STATUS_PROCESSING) {
$updateTransaction = $transaction;
}

foreach ($children as $child) {
if ($child->reference === $transaction->reference && $child->status === TransactionRecord::STATUS_PROCESSING && $paymentIntent['status'] === 'succeeded') {
$updateTransaction = $child;

break;
}
}
}

if ($updateTransaction) {
$transactionRecord = TransactionRecord::findOne($updateTransaction->id);
$transactionRecord->status = TransactionRecord::STATUS_SUCCESS;
$transactionRecord->message = '';
$transactionRecord->response = $paymentIntent;

$transactionRecord->save(false);
$transaction->getOrder()->updateOrderPaidInformation();
}
}
}

/**
* Handle a failed invoice by updating the subscription data for the subscription it failed.
*
Expand Down Expand Up @@ -688,10 +733,16 @@ public function handlePaymentMethodUpdated(array $data)
$paymentSource->customerId = $user->id;
$paymentSource->response = Json::encode($stripePaymentMethod);

if ($stripePaymentMethod['card']['brand'] && $stripePaymentMethod['card']['last4']) {
$paymentSource->description = $stripePaymentMethod['card']['brand'] . ' ending in ' . $stripePaymentMethod['card']['last4'];
$description = 'Stripe payment source';

if ($stripePaymentMethod['type'] === 'card') {
$description = ($stripePaymentMethod['card']['brand'] ?: 'Card') . ' ending in ' . $stripePaymentMethod['card']['last4'];
} elseif (isset($stripePaymentMethod[$stripePaymentMethod['type']], $stripePaymentMethod[$stripePaymentMethod['type']]['last4'])) {
$description = 'Payment source ending in ' . $stripePaymentMethod[$stripePaymentMethod['type']]['last4'];
}

$paymentSource->description = $description;

$paymentMethod = $this->getStripeClient()->paymentMethods->retrieve($stripePaymentMethod['id']);
$paymentMethod->attach(['customer' => $stripeCustomer->id]);

Expand Down
4 changes: 4 additions & 0 deletions src/responses/PaymentIntentResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function isProcessing(): bool
}
}

if ((!array_key_exists('next_action', $this->data) || $this->data['next_action'] === null) && array_key_exists('status', $this->data) && $this->data['status'] === 'processing') {
return true;
}

return false;
}

Expand Down

0 comments on commit 6cbadfe

Please sign in to comment.