Skip to content

Commit

Permalink
Allow making payment source primary when creating
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Jul 3, 2023
1 parent bde5847 commit 0c2fb29
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/controllers/PaymentSourcesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function actionAdd(): ?Response
$description = (string)$this->request->getBodyParam('description');

try {
$paymentSource = $plugin->getPaymentSources()->createPaymentSource($customer->id, $gateway, $paymentForm, $description);
$paymentSource = $plugin->getPaymentSources()->createPaymentSource($customer->id, $gateway, $paymentForm, $description, $isPrimaryPaymentSource);
} catch (Throwable $exception) {
Craft::$app->getErrorHandler()->logException($exception);
return $this->asModelFailure(
Expand Down
6 changes: 5 additions & 1 deletion src/services/PaymentSources.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function getPaymentSourceByIdAndUserId(int $sourceId, int $userId): ?Paym
* @throws InvalidConfigException
* @throws PaymentSourceException If unable to create the payment source
*/
public function createPaymentSource(int $customerId, GatewayInterface $gateway, BasePaymentForm $paymentForm, string $sourceDescription = null): PaymentSource
public function createPaymentSource(int $customerId, GatewayInterface $gateway, BasePaymentForm $paymentForm, string $sourceDescription = null, bool $makePrimarySource = false): PaymentSource
{
try {
$source = $gateway->createPaymentSource($paymentForm, $customerId);
Expand All @@ -284,6 +284,10 @@ public function createPaymentSource(int $customerId, GatewayInterface $gateway,
throw new PaymentSourceException(Craft::t('commerce', 'Could not create the payment source.'));
}

if($makePrimarySource) {
Plugin::getInstance()->getCustomers()->savePrimaryPaymentSourceId($source->getCustomer(), $source->id);
}

return $source;
}

Expand Down

0 comments on commit 0c2fb29

Please sign in to comment.