By Nenad Lucic


2019-06-11 20:49:17 8 Comments

I would like to know if it is possible to capture payments by subscribing to order transition events.

Example:

  1. Transaction mode: Authorize only
  2. On order placed - Capture some amount (application fee)
  3. On order validated - Capture outstanding amount

Thank you!

1 comments

@Nenad Lucic 2019-06-13 09:45:44

I've managed to create new payment and modify authorisation amount. Here is what I have so far:

public static function getSubscribedEvents() {
  $events = ['commerce_order.place.post_transition' => ['captureFee', -50]];
  return $events;
}

public function captureFee(WorkflowTransitionEvent $event) {
  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $event->getEntity();

  if ($order->bundle() != 'training') {
    return;
  }

  /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
  $payment_gateway = $order->payment_gateway->entity;
  $payment_gateway_plugin = $payment_gateway->getPlugin();

  $payment_storage = $this->entityTypeManager->getStorage('commerce_payment');

  // Load first order payment
  $payments = $payment_storage->loadMultipleByOrder($order);
  $first_payment = current($payments);

  // Only proceed if payment is not captured
  if ($first_payment->isCompleted()) {
    return;
  }

  // Default fee amount
  $fee_amount = new \Drupal\commerce_price\Price('50.00', 'USD');

  // Get fee amount from product variation
  if (isset($order->getItems()[0]->getPurchasedEntity()->field_app_fee) && !$order->getItems()[0]->getPurchasedEntity()->field_app_fee->isEmpty()) {
    $fee_amount = $order->getItems()[0]->getPurchasedEntity()->field_app_fee->first()->toPrice();
  }

  // Create fee payment
  /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
  $payment = $payment_storage->create([
    'state' => 'new',
    'amount' => $fee_amount,
    'payment_gateway' => $payment_gateway->id(),
    'order_id' => $order->id(),
  ]);

  // Capture fee payment
  if ($payment_gateway_plugin instanceof OnsitePaymentGatewayInterface) {
    $payment->payment_method = $order->payment_method->entity;
    $payment_gateway_plugin->createPayment($payment, 'capture');
  }
  elseif ($payment_gateway_plugin instanceof ManualPaymentGatewayInterface) {
    $payment_gateway_plugin->createPayment($payment, TRUE);
  }
  else {
    return;
  }

  // Subtract captured amount from authorized amount and save first payment
  $first_payment_amount = $first_payment->getAmount();
  $first_payment->setAmount($first_payment_amount->subtract($fee_amount));
  $first_payment->save();

}

I have "training" bundle that is limited to 1 item per cart. This is the result with variation price set to $2,100.00 and $45.00 fee.

enter image description here

Related Questions

Sponsored Content

1 Answered Questions

Order list: Pay unpaid invoices

1 Answered Questions

[SOLVED] Pay order only after it been placed and validated

  • 2019-05-28 08:55:24
  • Niklan
  • 25 View
  • 0 Score
  • 1 Answer
  • Tags:   commerce

1 Answered Questions

[SOLVED] Temporarily change the current language during a request

  • 2018-10-11 20:50:30
  • stefdewa
  • 129 View
  • 5 Score
  • 1 Answer
  • Tags:   8

1 Answered Questions

How do I capture a page as HTML?

1 Answered Questions

1 Answered Questions

0 Answered Questions

Using Commerce Funds module, how to charge user each time that user withdraws money?

  • 2016-06-04 07:13:22
  • swasin
  • 134 View
  • 1 Score
  • 0 Answer
  • Tags:   commerce

1 Answered Questions

Find the total amount of multiple products of the same type - Commerce

  • 2015-11-23 03:04:00
  • dang42
  • 47 View
  • 1 Score
  • 1 Answer
  • Tags:   commerce rules

1 Answered Questions

[SOLVED] Why is Rules reacting on Commerce Events multiple times and out of order?

  • 2015-01-23 17:34:38
  • Channel Islander
  • 138 View
  • 0 Score
  • 1 Answer
  • Tags:   commerce rules

1 Answered Questions

Use Rules to give users a discount on their order after a few days (Commerce)

  • 2014-06-25 21:48:07
  • e.shell
  • 173 View
  • 0 Score
  • 1 Answer
  • Tags:   rules commerce

Sponsored Content