Automattic\WooCommerce\Internal\Orders
PaymentInfo::get_wcpay_card_info
Get info about the card used for payment on an order, when the payment gateway is WooPayments.
Method of the class: PaymentInfo{}
No Hooks.
Returns
Array.
Usage
$result = PaymentInfo::get_wcpay_card_info( $order ): array;
- $order(WC_Abstract_Order) (required)
- The order in question.
Notes
PaymentInfo::get_wcpay_card_info() PaymentInfo::get wcpay card info code WC 10.8.1
private static function get_wcpay_card_info( WC_Abstract_Order $order ): array {
if ( 'woocommerce_payments' !== $order->get_payment_method() ) {
return array();
}
// This is a Woo-specific meta key, not used within WooPayments.
$cache_meta_key = '_wcpay_raw_payment_method_details';
$payment_details = null;
$stored_payment_details = $order->get_meta( $cache_meta_key );
if ( is_string( $stored_payment_details ) && strlen( $stored_payment_details ) > 0 ) {
$payment_details = json_decode( $stored_payment_details, true );
}
if ( ! $payment_details ) {
if ( ! class_exists( \WC_Payments::class ) ) {
return array();
}
$payment_method_id = $order->get_meta( '_payment_method_id' );
if ( ! $payment_method_id ) {
return array();
}
try {
$payment_details = \WC_Payments::get_payments_api_client()->get_payment_method( $payment_method_id );
} catch ( \Throwable $ex ) {
$order_id = $order->get_id();
$message = $ex->getMessage();
wc_get_logger()->error(
sprintf(
'%s - retrieving info for payment method %s for order %s: %s',
StringUtil::class_name_without_namespace( static::class ),
$payment_method_id,
$order_id,
$message
),
array(
'source' => 'payment-info',
)
);
return array();
}
// Cache payment method details.
$order->update_meta_data( $cache_meta_key, wp_json_encode( $payment_details ) );
$order->save_meta_data();
}
$card_info = array();
if ( isset( $payment_details['type'], $payment_details[ $payment_details['type'] ] ) ) {
$details = $payment_details[ $payment_details['type'] ];
switch ( $payment_details['type'] ) {
case 'card':
default:
$card_info['brand'] = $details['brand'] ?? '';
$card_info['last4'] = $details['last4'] ?? '';
break;
case 'card_present':
case 'interac_present':
$card_info['brand'] = $details['brand'] ?? '';
$card_info['last4'] = $details['last4'] ?? '';
$card_info['account_type'] = $details['receipt']['account_type'] ?? '';
$card_info['aid'] = $details['receipt']['dedicated_file_name'] ?? '';
$card_info['app_name'] = $details['receipt']['application_preferred_name'] ?? '';
break;
}
}
return array_map( 'sanitize_text_field', $card_info );
}