You are here

public function Mollie::createRemotePayment in Commerce Mollie 8

File

src/Plugin/Commerce/PaymentGateway/Mollie.php, line 245

Class

Mollie
Provides the Mollie payment gateway.

Namespace

Drupal\commerce_mollie\Plugin\Commerce\PaymentGateway

Code

public function createRemotePayment(PaymentInterface $payment, $data) {

  // Prepare data for remote payment creation.
  $transaction_data = [
    'amount' => [
      'currency' => $payment
        ->getAmount()
        ->getCurrencyCode(),
      'value' => number_format($payment
        ->getAmount()
        ->getNumber(), 2, '.', ''),
    ],
    'description' => $this
      ->t('@store order @order_id', [
      '@store' => $payment
        ->getOrder()
        ->getStore()
        ->label(),
      '@order_id' => $payment
        ->getOrderId(),
    ]),
    'redirectUrl' => $data['return'],
    'webhookUrl' => $this
      ->getNotifyUrl()
      ->toString(),
    'metadata' => [
      'order_id' => $payment
        ->getOrderId(),
    ],
  ];

  // Create remote payment and save remote data in the drupal payment.
  try {
    $mollie_payment = $this
      ->getApi()->payments
      ->create($transaction_data);
    $payment
      ->setRemoteId($mollie_payment->id);
    $payment
      ->setRemoteState($mollie_payment->status);
    $payment
      ->save();
    ErrorHelper::handleErrors($mollie_payment);
  } catch (MollieApiException $e) {
    ErrorHelper::handleException($e);
  }
  return $mollie_payment;
}