Passing custom fields from WooCommerce to Stripe Payments

Aug 16, 05:06 AM

First, your domain must pass the certification test!

certification tester

One: name the custom field

Notice leading underscore to designate a meta field: _billing_clients_email



Two: Create a code snippet


Click here for instructions to add snippet plugin first


// added by mark edwards 2024-05-22
// 
// 
// https://www.wpservices.com/how-to-get-your-custom-checkout-field-value-in-woocommerce/

add_filter(‘wc_stripe_payment_intent_args’, function($args, $order) { $orderNbr = WC()->session->get(‘order_awaiting_payment’); $order = new WC_Order($orderNbr);

$args[‘metadata’][‘orderNbr’] = $orderNbr ; $args[‘metadata’][‘clients_email’] = $order->get_meta(‘_billing_clients_email’) ; $args[‘metadata’][‘first_name’] = $order->get_billing_first_name() ; // 2024-08-17 $args[‘metadata’][‘last_name’] = $order->get_billing_last_name() ; // 2024-08-17

return $args; }, 10, 2);
DONT FORGET TO RESTART PHP-FPM IF THAT IS HOW PHP IS RUNNING! ! !


Three: Name an endpoint



Last, create an endpoint on server:

Name this identically to the Stripe Endpoint name. Most of this code is provided for you when the endpoint is created!
require ‘vendor/autoload.php’;

// The library needs to be configured with your account’s secret key.
// Ensure the key is kept out of any version control system you might be using.
$stripe = new StripeStripeClient(‘sk_test_XXXXXXXXXXXXXXXXX’);

// This is your Stripe CLI webhook secret for testing your endpoint locally.
$endpoint_secret = ‘whsec_XXXXXXXXXXXXXXXXXXXXXXXXXXXXX’;

$payload = @file_get_contents(‘php://input’);
$sig_header = $_SERVER[‘HTTP_STRIPE_SIGNATURE’];
$event = null;

try { error_log(‘’); error_log(‘Endpoint Test’); error_log(FILE);

$event = StripeWebhook::constructEvent( $payload, $sig_header, $endpoint_secret ); error_log(‘results:’); // too much stuff here! error_log(print_r($event->data , true)); error_log(print_r($event->data[‘object’][‘billing_details’][‘email’] , true)); error_log(print_r($event->data[‘object’][‘billing_details’][‘name’] , true)); error_log(print_r($event->data->object->metadata->clients_email , true));

} catch(UnexpectedValueException $e) { // Invalid payload http_response_code(400); exit();
} catch(StripeExceptionSignatureVerificationException $e) { // Invalid signature http_response_code(400); exit();
}

// Handle the event
echo ‘Received unknown event type ‘ . $event->type;

http_response_code(200);
Mark Edwards

,

---

Commenting is closed for this article.

---