Jump to content

Saving Stripe's charge_id to order


Spinbox
 Share

Recommended Posts

I need to include Stripe's Charge ID to invoices. This way some accounting software can relate the payment to the invoice via Stripe.

On successfull payment (Order complete);

Inside the Stripe Payment Processer I found that 'isSuccessfulPaymentCapture($response, $options = [])' $response is returning it's latest_charge (the charge_id).

How can I save this to the order?

  • Like 1
Link to comment
Share on other sites

Hi @Spinbox,

I have made this hookable so you have access to $response and $options. However, we have no dedicated field for saving this value. You could add a text field to your order template. Then save the latest_charge value to that field. This way, it stays together with the order and you can easily retrieve and add it to your invoices. Does this make sense or you need example code?

Thanks.

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

I have managed to save the charge id to the order. For this to work I needed to make captureOrder hookable. This is probably not the right way tough..

    $wire->addHookAfter('PadloperProcessOrder::captureOrder', function(HookEvent $event) {

        $paymentIntent = $event->return; 
        $orderId = $paymentIntent->metadata->reference_id; 

        if ($paymentIntent && $paymentIntent->status === 'succeeded') {
            $chargeId = $paymentIntent->latest_charge; 
            
            // Load the order page using the order ID and save the charge ID
            $orderPage = wire()->pages->get("template=padloper-order, id={$orderId}");
            if ($orderPage->id) {
                $orderPage->of(false);
                $orderPage->set('stripe_charge_id', $chargeId);
                $orderPage->save();
            }
        }
    });
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Hey @Spinbox,

That's fine. In Padloper 009 you could also do it like this (saves you making captureOrder hookable; will get overwritten with upgrades unless I make it hookable here as well):

<?php

namespace ProcessWire;

$wire->addHookAfter('PadloperProcessOrder::isSuccessfulPaymentCaptureHook', function (HookEvent $event) {
    $paymentIntent = $event->arguments('response');
    $orderId = $paymentIntent->metadata->reference_id;
    // ALTERNATIVELY
    // $options = $event->arguments('options');
    // $orderId = $options['expected_order_id'];
    if ($paymentIntent && $paymentIntent->status === 'succeeded') {
        $chargeId = $paymentIntent->latest_charge;
        // Load the order page using the order ID and save the charge ID
        $orderPage = wire()->pages->get("template=padloper-order, id={$orderId}");
        if ($orderPage->id) {
            $orderPage->of(false);
            $orderPage->set('stripe_charge_id', $chargeId);
            $orderPage->save('stripe_charge_id');
        }
    }
});

Hope this helps.

 

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...