• Resolved snowdrop

    (@nymla)


    Hi!
    I am wondering if its possible to add some (more) metadata from Stripe, to my woocommerce invoices.

    I have successfully added a few meta keys already, that show the Stripe net payout, fee and currency used.

    But can I also add the “amount” and “payment exchange rate”?
    This is what it looks like, in my Stripe account, when clicking a specific payment:

    And this is the code that I added to my themes functions.php file to add the meta keys, that show the Stripe net payout, fee and currency, which is working:

    add_action( 'fi/core/template/invoice/after_notes', function ( $document ) {
        $order = wc_get_order( $document->get_order_id() );
        $data  = [];
        if ( $order ) {
           $stripe_currency = $order->get_meta( '_stripe_currency' );
           if ( $stripe_currency ) {
              $data[] = $stripe_currency;
           }
           $stripe_fee = $order->get_meta( '_stripe_fee' );
           if ( $stripe_fee ) {
              $data[] = $stripe_fee;
           }
           $stripe_net = $order->get_meta( '_stripe_net' );
           if ( $stripe_net ) {
              $data[] = $stripe_net;
           }
    
           if ( $data ) {
              echo implode( ', ', $data );
           }
        }
    } );

    Could you advise me on how to add more stripe meta keys?

    Thank you very much / Caroline

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Payment Plugins

    (@mrclayton)

    Hi @nymla

    Could you advise me on how to add more stripe meta keys?

    The answer depends on what info from Stripe you’re wanting to add to the order.

    You could use action wc_stripe_order_payment_complete to decorate the order with info from the Stripe charge object.

    https://plugins.trac.www.ads-software.com/browser/woo-stripe-payment/tags/3.3.56/includes/abstract/abstract-wc-stripe-payment.php#L128

    Kind Regards

    Thread Starter snowdrop

    (@nymla)

    Hi! The info I want is specifically the “amount” and “payment exchange rate” as shown in my screenshot.
    Will “wc_stripe_order_payment_complete” do that?

    I don’t really know how to code – could you possibly let me know exactly what code I should add and where? Or I will take this info to someone who can help me.
    Thank you!

    Plugin Author Payment Plugins

    (@mrclayton)

    Here is an example that saves the exchange rate to the order’s meta.

    add_action('wc_stripe_order_payment_complete', function($charge, $order){
    	if ( isset( $charge->balance_transaction ) && is_object( $charge->balance_transaction ) ) {
    		$balance_transaction = $charge->balance_transaction;
    		$exchange_rate = $balance_transaction->exchange_rate === null ? 1 : $balance_transaction->exchange_rate;
    		$order->update_meta_data('_stripe_exchange_rate', $exchange_rate);
    		$order->save();
    	}
    }, 10, 2);

    You can then use that exchange rate in your custom code and multiple it by the order’s total.

    Kind Regards

    Thread Starter snowdrop

    (@nymla)

    Hello again!
    Thank you very much for your support! I am replying to this again because I ran into a problem.

    I have added some code to my invoices, which has been working fine up until now.
    I used this to display the exchange rate: $exchange_rate = $order->get_meta( ‘_stripe_exchange_rate’ );

    But now this meta data stopped working for some reason? It shows nothing.

    Here is the entire section of code added to my invoice to display Stripe meta data:


    add_action( 'fi/core/template/invoice/after_notes', function ( $document ) {
    $order = wc_get_order( $document->get_order_id() );

    if ( $order ) {
    $stripe_currency = $order->get_meta( '_stripe_currency' );
    $stripe_fee = $order->get_meta( '_stripe_fee' );
    $stripe_net = $order->get_meta( '_stripe_net' );
    $exchange_rate = $order->get_meta( '_stripe_exchange_rate' );

    // Display the new line for "Amount"
    if ( $stripe_net && $stripe_fee ) {
    $amount = $stripe_net + $stripe_fee;
    echo "<strong>Stripe Payment Details</strong><br>";
    echo "Amount: " . $amount . "<br>";
    }
    // ***

    if ( $stripe_net ) {
    echo "Net amount: " . $stripe_net . "<br>";
    }

    if ( $stripe_fee ) {
    echo "Fee (paid by seller): " . $stripe_fee . "<br>";
    }

    if ( $stripe_currency ) {
    echo "Currency: " . $stripe_currency . "<br>";
    }

    if ( $exchange_rate ) {
    echo "Exchange rate: " . $exchange_rate . "<br><br>";
    }

    }
    } );

    Do you know why the “_stripe_exchange_rate” stopped working? Everything else in the code is working.

    Thank you! / Caroline

    Plugin Author Payment Plugins

    (@mrclayton)

    I recommend you verify within your database that the _stripe_exchange_rate metadata exists. If it doesn’t then there is likely something wrong with your custom code that’s hooking into the filter wc_stripe_order_payment_complete.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using Stripe meta keys?’ is closed to new replies.