Problem/Motivation

https://hopeforelika.com/checkout/6/payment/return?PayerID=AXs6Z22sCBTJG...
If create payment subscription anonymous have php erorr.
Error: Call to a member function id() on null in Drupal\paypal_subscriptions\Plugin\Commerce\PaymentGateway\ExpressCheckoutSubscriptions->doExpressCheckoutDetails() (line 159 of /home/wibeone/hopeforelika.com/web/modules/contrib/paypal_subscriptions/src/Plugin/Commerce/PaymentGateway/ExpressCheckoutSubscriptions.php).

Steps to reproduce

Proposed resolution

Get customer from order.
// Build NVP data for PayPal API request.
$account = User::load($order->getCustomerId());

Remaining tasks

User interface changes

API changes

Data model changes

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

Utilvideo created an issue. See original summary.

abhishek_gupta1’s picture

Assigned: Unassigned » abhishek_gupta1
abhishek_gupta1’s picture

Assigned: abhishek_gupta1 » Unassigned
Status: Active » Needs review
StatusFileSize
new832 bytes

@Utilvideo , I have fixed the issue and created Patch.

pavelculacov’s picture

Thanks.

pavelculacov’s picture

+1 Confirm worked.

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

cbovard’s picture

Assigned: Unassigned » cbovard

Hello Everyone,
I have encountered this same bug and saw in the codebase https://git.drupalcode.org/project/paypal_subscriptions/-/blob/2.0.x/src...

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->current_user = $container->get('current_user');
    return $instance;
  }

The

$instance->current_user = $container->get('current_user'); 

should be

$instance->currentUser = $container->get('current_user'); 

The protected property is:

protected $currentUser;

https://git.drupalcode.org/project/paypal_subscriptions/-/blob/2.0.x/src...

abhishek_gupta1 Did a great job with the patch. I have built up more on this to fix the property declaration and some error handling.

I have run tests with a Drupal Commerce install using Commerce Recurring. Further tests were working well with the Subscriptions in the Paypal Sandbox.

cbovard’s picture

I added the PR and can test this patch. Please let me know if there needs to be changes or the error handling logic needs to be stripped out.

cbovard’s picture

StatusFileSize
new2.06 KB

Had to redo the patch off of the 2.0.x branch as there was some issues when I tested it. The Diff patch above wanted to alter the paypal_subscriptions.info.yml file.

pavelculacov’s picture

"I do not agree with the patch. Why can't an anonymous user subscribe on PayPal?
Suggestion: Load the user if they exist. If not, allow an anonymous donation.
I don't think the 'name' value from the user is strictly required. We should make it dependent on whether the user is logged in. If they are logged in, get the name; if not, use 'Anonymous' as the default."
'SUBSCRIBERNAME' => $account->get('name')->value,

In my donation charity for girl, it used for anonymous users....
https://hopeforelika.com/donate

I need test your patch. Maybe it worked. because if user Id 0 name for anonymous is empty ""

cbovard’s picture

Hello,
I added in some new logic here:

    // Handle the current user or fallback to the order's customer.
    $account = NULL;

    if ($this->currentUser->isAuthenticated()) {
      // Try to load the current user.
      $account = User::load($this->currentUser->id());
    }
    else {
      // User is Anonymous / Guest checkout.
      $customer_id = $order->getCustomerId();
      $account = User::load($customer_id);
    }

I ran some tests with Paypal with a membership based Commerce site with a Guest checkout. The username that appears is: guest user name

Which is used below here:

'SUBSCRIBERNAME' => $account->get('name')->value,

Of course I kept the property name fix.

Attached is the patch and let me know if there are needed changes.

cbovard’s picture

Assigned: cbovard » Unassigned

Unassigning for review. Thank you.