Score:0

How to programmatically capture a payment?

us flag

Payment is in Authorized state. How do I programmatically capture it? I am using Authorize.net payment gateway.

Score:1
us flag

Figured it out on my own:

function capture_payment($order_id) {
  $order = Order::load($order_id);
  $payment_method_id = $order->payment_method->target_id;
  $payment = getPayment($payment_method_id);
  $amount = new Price($order->total_price[0]->number, $order->total_price[0]->currency_code);

  $gateway = \Drupal::entityTypeManager()
    ->getStorage('commerce_payment_gateway')->loadByProperties([
      'plugin' => 'authorizenet_acceptjs',
    ]);
  $plugin = $gateway['authorize_net_accept_js'];
  $payment_gateway_plugin = $plugin->getPlugin();

  $payment_gateway_plugin->capturePayment($payment, $amount);
}

function getPayment($method_id) {
  $payments = \Drupal::entityTypeManager()
    ->getStorage('commerce_payment')
    ->loadByProperties(['payment_method' => $method_id]);
  $payment = current($payments);
  return $payment;
}
Score:-1
bn flag

To programmatically capture a payment using the Authorize.Net payment gateway, you will need to follow these steps:

  1. Integrate your application with the Authorize.Net payment gateway. This will typically involve setting up an account with Authorize.Net, and installing their SDK or library in your application.
  2. Create a payment form in your application that allows users to enter their payment details. This form will typically include fields for the user's credit card number, expiration date, and security code.
  3. When the user submits the payment form, send a request to the Authorize.Net payment gateway to initiate the payment. This request will include the user's payment details, as well as any other relevant information such as the amount of the payment and the currency.
  4. The payment gateway will process the payment, and send a response back to your application indicating whether the payment was successful or not. If the payment was successful, you can then capture the payment by sending a capture request to the payment gateway.

To send a capture request to the Authorize.Net payment gateway, you will need to use the CaptureOnly API method. This method requires the following parameters:

x_trans_id: The transaction ID of the authorization you want to capture. x_amount: The amount you want to capture. x_invoice_num: An optional invoice number for the capture. You can find more information on the CaptureOnly API method in the Authorize.Net documentation.

Keep in mind that these steps are just a general outline, and the specific details will vary depending on your specific implementation. It's important to consult the documentation and support resources provided by Authorize.Net to get more detailed information on how to use their APIs and functionality.

liquidcms avatar
us flag
Took a while to realize you are not talking abut Drupal. You are talking abut writing this all from scratch. Hopefully there is an answer specific to the existing Auth.net payment gateway (for Drupal).
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.