WooCommerce scripts

This is a script to query the Stripe API using WooCommerce helpers to check and see if a payment token is attached to a customer:

// See if payment tokens are attached to a customer
<?php
ini_set( 'display_errors', 1 );
error_reporting(E_ALL);

$payment_tokens = array( 'pm_token1', 'pm_token2', 'etc', );

foreach ( $payment_tokens as $payment_token ) {
	$token_data = WC_Stripe_Gateway::load()->get_payment_method( $payment_token, 'live' );
	if ( ! empty( $token_data->customer ) ) {
		echo "$token_data->customer, $payment_token \n";
	}
}

echo "Script complete.\n";
?>Code language: PHP (php)

Save it to a file called query-tokens.php and then run it on a site with WooCommerce and the Stripe plugin installed with a valid API using wp eval-file query-tokens.php


This is a script to query the Stripe API using WooCommerce helpers to fetch all payment tokens attached to a given customer token.

// Get all payment tokens for a given customer
<?php
ini_set( 'display_errors', 1 );
error_reporting(E_ALL);

$customer_ids = array( 'cus_token1', 'cus_token2', 'etc', );

foreach ( $customer_ids as $customer_id ) {

	$payment_methods_per_user = WC_Stripe_Gateway::load()->paymentMethods->mode( 'live' )->all( array(
		'customer' => $customer_id,
		'type'     => 'card',
	) );
	if ( empty( $payment_methods_per_user['data'][0]['id'] ) ) {
		echo "No payment methods found for customer $customer_id.\n";
		continue;
	}
	foreach ( $payment_methods_per_user['data'] as $payment_method ) {
		echo $customer_id . ', ' . $payment_method['id'] . PHP_EOL;
	}
}

echo "Script complete.\n";
?>
Code language: PHP (php)

Save it to a file called query-customer-tokens.php and then run it on a site with WooCommerce and the Stripe plugin installed with a valid API using wp eval-file query-customer-tokens.php

Updated 2023-03-11

Categories

Tags:

Related Posts