• Resolved careyrob

    (@careyrob)


    On Stripe transactions I want to save also the receipt_url charge attribute to the order_meta.

    Is there a way to accomplish this through a code snippet rather than updating the following functions?

    * Woo_MP\Woo_MP_Order::get_charge_defaults()

        private function get_charge_defaults() {
            return [
                'id'              => '',
                'date'            => current_time( 'M d, Y' ),
                'last4'           => '',
                'amount'          => 0,
                'currency'        => '',
                'captured'        => false,
                'held_for_review' => false,
                'receipt_url'	  => '',
            ];
        }

    * Woo_MP\Woo_MP_Order::add_woo_mp_payment()

    
        public function add_woo_mp_payment( $payment ) {
            $payment += $this->get_charge_defaults();
    
            $payments = $this->get_woo_mp_payments();
    
            $payments[] = $payment;
    
            $this->update_meta_data( 'woo-mp-' . WOO_MP_PAYMENT_PROCESSOR . '-charges', json_encode( $payments ) );
    	
    	$this->update_meta_data( WOO_MP_PAYMENT_PROCESSOR . '_receipt_url', $payments->receipt_url);
    		
        }

    * Woo_MP\Payment_Processor::save_charge()

        private function save_charge( $charge ) {
            $charge['order']->add_woo_mp_payment( [
                'id'              => $charge['trans_id'],
                'last4'           => $charge['last_4'],
                'amount'          => $charge['amount'],
                'currency'        => $charge['currency'],
                'captured'        => $charge['capture'],
                'held_for_review' => $charge['held_for_review'],
    			'receipt_url' 	  => $charge['receipt_url'],
            ] );...

    * Woo_MP\Payment_Gateways\Stripe\Payment_Processor::process()

           ... 
            return [
                'trans_id'        => $charge->id,
                'held_for_review' => $charge->outcome->type === 'manual_review',
                'receipt_url'     => $charge->receipt_url,
            ];...
    • This topic was modified 4 years, 7 months ago by careyrob.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author bfl

    (@bfl)

    Currently, there is no good way to do this. I will consider adding an action hook. For now, you can use the following snippet:

    
    add_filter( 'http_response', function ( $response, $r, $url ) {
        if (
            doing_action( 'wp_ajax_woo_mp_process_transaction' ) &&
            isset( $_REQUEST['order_id'] ) &&
            $url === 'https://api.stripe.com/v1/payment_intents'
        ) {
            $decoded_body = json_decode( wp_remote_retrieve_body( $response ) );
    
            if ( isset( $decoded_body->charges->data[0] ) ) {
                $order = wc_get_order( $_REQUEST['order_id'] );
    
                if ( $order ) {
                    $charge = $decoded_body->charges->data[0];
    
                    $order->add_meta_data( 'manual_payment_stripe_receipt_url', json_encode( [
                        'charge_id'   => $charge->id,
                        'receipt_url' => $charge->receipt_url,
                    ] ) );
    
                    $order->save_meta_data();
                }
            }
        }
    
        return $response;
    }, 10, 3 );
    

    This will add a new custom field for each payment containing the charge ID and the receipt URL. This requires version 2.4.0. Note that since this snippet relies on the internal workings of the plugin, it is not guaranteed to work in future versions.

    Thread Starter careyrob

    (@careyrob)

    I’m getting an error when I use this. I don’t see the function save_meta_data() defined in Woo-MP or WordPress.

    Should that be $order->save(); instead?

    Plugin Author bfl

    (@bfl)

    That snippet requires WooCommerce 3.0.0 or above. What version are you on?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add Stripe [receipt_url] to order_meta’ is closed to new replies.