The user's payment method pages need a "Set default" link. Just like profiles have.

Issue fork commerce-2790533

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bojanz created an issue. See original summary.

miteshmap’s picture

Assigned: Unassigned » miteshmap

working on this.

miteshmap’s picture

Assigned: miteshmap » Unassigned

couldn't make it work. :(

vasike’s picture

Assigned: Unassigned » vasike

i'll try

vasike’s picture

Status: Active » Needs review

There is PR for this:
https://github.com/drupalcommerce/commerce/pull/655

- Create new route for set-default
- Build a controller for the route
- Add this to payment methods list operations
- Default by default for the first or unique user payment method
- Extend and update Payment method tests to include this functionality (both Entity and functional)
- Extend the payment method storage with loadMultipleByUser & loadDefaultByUser methods.

Not sure about the loadMultipleByUser & loadDefaultByUser methods implementations.
Maybe some extra properties should be involved.

vasike’s picture

Now that we have #2827144: Support multiple payment gateways on Checkout Payment information pane
we need to use the default payment method in the Payment information pane.

PR updated with a commit on this.

vasike’s picture

PR updated with a commit to fix travis errors.
Now we're green
Last commit fix the postSave make it default for anonymous: no need to check/set the default for anonymous.

sumanthkumarc’s picture

Adding related issue, https://www.drupal.org/node/2860102, the getDefaultPaymentMethodOption doesn't check if gateway is avaialble.

vasike’s picture

PR "reroll"

nikathone’s picture

Uploading a patch based on @vasike PR for patching sake.

Status: Needs review » Needs work
nikathone’s picture

Status: Needs work » Needs review
StatusFileSize
new19.57 KB

Another patch for the sake of patching with a rebase of the current commerce based on the PR at https://github.com/drupalcommerce/commerce/pull/848 which is just a rebase of https://github.com/drupalcommerce/commerce/pull/655 by @vasike

Status: Needs review » Needs work
nikathone’s picture

+++ b/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php
@@ -309,7 +309,18 @@ protected function getDefaultPaymentMethodOption(array $options) {
+        if (in_array($default_payment_method->id(), $option_ids)) {

This might cause Call to a member function id() on boolean in Drupal\commerce_payment\Plugin\Commerce\CheckoutPane\PaymentInformation->getDefaultPaymentMethodOption() if the current user doesn't have any payment method. Note when testing my user did have a completed order though. The fix would be something like if ($default_payment_method && in_array($default_payment_method->id(), $option_ids))

nikathone’s picture

Status: Needs work » Needs review
StatusFileSize
new16.72 KB

Rerolled PR at https://github.com/drupalcommerce/commerce/pull/655 with latest commerce.

Status: Needs review » Needs work
nikathone’s picture

StatusFileSize
new20.39 KB
new3.09 KB

Adding an event to which is dispatched on post save about payment is default. I wasn't sure if this should be added on this patch or in a follow up issue. I will need the maintainer to let me know then we can go with the patch at #15 or if ok then we go with this one. Also leaving it to need work until we figure out why the test are not passing.

nikathone’s picture

StatusFileSize
new21.97 KB
new5.82 KB

Just found out that I uploaded a wrong file for #15. Here is another one hopefully it will pass the tests now.

nikathone’s picture

Status: Needs work » Needs review
StatusFileSize
new22.78 KB
new637 bytes

Status: Needs review » Needs work

The last submitted patch, 19: 2790533-19.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

nikathone’s picture

StatusFileSize
new22.79 KB

Oops uploaded wrong patch file.

anas_maw’s picture

Status: Needs work » Needs review
StatusFileSize
new19.07 KB

Reroll the latest patch to the latest dev version

ctrladel’s picture

StatusFileSize
new18.73 KB

Rerolled against dev

ctrladel’s picture

StatusFileSize
new18.77 KB
new622 bytes

Small fix to address when the order has a payment method attached but that payment method is not available in the options.

Status: Needs review » Needs work

The last submitted patch, 24: 2790533-24.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mjpa’s picture

Status: Needs work » Needs review
StatusFileSize
new14.54 KB
new18.07 KB

Attached a reroll against dev and the following changes:

  • Removed the adding to cart changes as they're not related to the issue
  • Cleaned up the mark as default controller for D9
  • Changed the preSave / postSave default checks to match the Profile module
  • Updated loadMultipleByUser and loadByUser to match Profile's so it brings in the default first so the preSave/postSave work
  • Slight refactor on the code to select a default payment method if the one used is no longer available.
  • Updated the test to use loadByUser() instead of loadDefaultByUser()

Status: Needs review » Needs work

The last submitted patch, 26: 2790533-26.patch, failed testing. View results

socialnicheguru’s picture

socialnicheguru’s picture

does not apply to commerce 2.x-dev

scotthooker made their first commit to this issue’s fork.

rhip’s picture

.

ahlam aljawahreh’s picture

StatusFileSize
new21.17 KB

update the patch for Drupal 10 & commerce 2.x-dev version

anas_maw’s picture

StatusFileSize
new20.21 KB

Reroll the patch to the latest dev version

nno’s picture

Here is the patch updated for Commerce 3.x

jsacksick’s picture

Version: 8.x-2.x-dev » 3.x-dev
Assigned: vasike » Unassigned

Can we get an MR that applies to 3.x so we can see if the tests are green? The latest patch includes unrelated changes to the CartManager too.

Also, PaymentMethod::postSave() should probably query payment methods that have the is_default flag set to TRUE, so we don't have to load all payment methods belonging to the user (not sure there is a risk of a customer having hundreds of payment methods but still.

nno’s picture

Here is the patch for 3.x without changes to the CartManager.

jsacksick’s picture

Is is possible to open a merge request with the changes?

zaporylie made their first commit to this issue’s fork.

zaporylie’s picture

Status: Needs work » Needs review

I rebased the MR, reviewed it, and provided necessary updates, including fixes to code and tests. The current test actually revealed an issue with the owner check, which is now corrected.

jsacksick’s picture

I left a quick comment for a minor change with the controller. (using $commerce_payment_method directly).

Pushed a commit addressing that, and merged the 3.x changes, let's see where we stand now.

jsacksick’s picture

Status: Needs review » Needs work

Ok... tests aren't passing...

The following code looks wrong to me:

elseif (count($storage->loadMultipleByUser($this->getOwner())) == 1) {
      // Set as default if it is the only payment method for the user.
      $this->setDefault(TRUE);
      $this->save();
    }

This doesn't belong to the postSave() method, as this will resave a payment method that was just saved, this should be moved to the presave().

zaporylie’s picture

Status: Needs work » Needs review

Added test coverage for the PaymentOptionsBuilder.

There are 2 things that should likely be addressed:
1) non-reusable payment methods should never be set as default.
2) deleting default payment method should make the next eligible payment method default

Both these points can be done as follow-up(s) and need some extra considerations.

  • jsacksick committed ed78537c on 3.x authored by nno
    [#2790533] feat: Add the ability to set a default payment method
    By:...
jsacksick’s picture

Status: Needs review » Fixed

@zaporylie: Thank you for wrapping this up! Merged!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.