Having the order object can be useful when building payment instructions.

Comments

jackbravo created an issue. See original summary.

jackbravo’s picture

Status: Active » Needs review
StatusFileSize
new2.21 KB

Here is the patch.
And also the PR: https://github.com/drupalcommerce/commerce/pull/783

bojanz’s picture

I think it would be more natural to pass the payment instead.
"Here's this pending thing, show how to complete it".
It still gives you the order via $payment->getOrder(), of course.

jackbravo’s picture

I agree. But I'm not sure how to get the payment from the order =P.

This is the code we have on commerce_payment_preprocess_commerce_checkout_completion_message(&$variables):


$order = $variables['order_entity'];
$payment_gateway = $order->get('payment_gateway')->entity;
$payment_gateway_plugin = $payment_gateway->getPlugin();

Maybe it is just querying the database for the last payment for that order?

bojanz’s picture

Title: Send order information to buildPaymentInstructions method in the HasPaymentInstructionsInterface » Pass the payment to HasPaymentInstructionsInterface::buildPaymentInstructions
Status: Needs review » Needs work

Retitling. Updating status.

We need something like:

$payment_storage = \Drupal::entityTypeManager()->getStorage('commerce_payment');
$payments = $payment_storage->loadMultipleByOrder($order);
$payments = array_filter($payments, function ($payment) use ($payment_gateway) {
  return $payment->getPaymentGatewayId() == $payment_gateway->id();
});
$payment = reset($payments);
if ($payment) {
  $variables['payment_instructions'] = $payment_gateway_plugin->buildPaymentInstructions($payment);
}
jackbravo’s picture

Status: Needs work » Needs review
StatusFileSize
new5.97 KB

This is the new patch with this changes. I have also updated the PR.

jackbravo’s picture

StatusFileSize
new6.94 KB

That piece of code worked just as intended =). Thanks!

The PR is updated now and here is the patch.

mglaman’s picture

Status: Needs review » Reviewed & tested by the community

I went ahead and rebased your PR and changed the commit message to match the Drupal.org commit message suggestion. Want to make sure bojanz thumbs up this.

  • bojanz committed f72e638 on 8.x-2.x authored by jackbravo
    Issue #2906457 by jackbravo: Pass the payment to...
bojanz’s picture

Status: Reviewed & tested by the community » Fixed

Thanks!

ndf’s picture

Note that this can break modules that implement the RC2 interface. (The RC2 interface does not have the $payment parameter).

Unless on the implementing side you use:

public function buildPaymentInstructions(PaymentInterface $payment = NULL) {
}

When you use this interface and want to support commerce-RC2, you should use the = NULL default value.

Status: Fixed » Closed (fixed)

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

fathershawn’s picture

As noted in #11 this was a breaking change after entering RC - was there a change record created? I did not find one.

bojanz’s picture

Looks like we forgot to add one. The change was introduced in RC3.

Would you like to do the honors?

fathershawn’s picture

Sure, thanks! Draft created for your review: https://www.drupal.org/node/2921869

bojanz’s picture

Thanks! Published.