Created
June 8, 2022 14:59
-
-
Save vanbo/2589a51df92d245761a8ff2ddc9259ea to your computer and use it in GitHub Desktop.
paytrace-add-card-details-to-order.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* NOTE: Add to 'wp-content/themes/your-theme/functions.php' file | |
*/ | |
add_action( 'wc_paytrace_transaction_approved_response_processed', 'vanbodevelops_add_order_details', 10, 2 ); | |
/** | |
* @param \WcPaytrace\Abstracts\Response $response | |
* @param \WC_Order $order | |
*/ | |
function vanbodevelops_add_order_details( $response, $order ) { | |
try { | |
$gateway = \WcPaytrace\Helpers\Factories::get_gateway( 'paytrace' ); | |
$integration = $gateway->get_integration_class(); | |
// Get the transaction details | |
$transaction_id = $response->get_transaction_id(); | |
if ( ! is_callable( array( $integration, 'export_transaction' ) ) ) { | |
return; | |
} | |
$transactions = $integration->export_transaction( $transaction_id ); | |
if ( ! $transactions ) { | |
// Log the result | |
\WC_Paytrace::add_debug_log( 'VD Add Order Details: No transactions found' ); | |
return; | |
} | |
$transaction = array_shift( $transactions ); | |
$card = \WC_Paytrace::get_field( 'credit_card', $transaction, false ); | |
if ( $card ) { | |
// Init the class | |
$services = $integration->get_api_services(); | |
$validator = $services->get_validator(); | |
/** | |
* "credit_card":{ | |
* "masked_number":"424242******1111", | |
* "expiration_month":"12", | |
* "expiration_year":"2020" | |
* }, | |
*/ | |
$card_type = $validator->get_card_type_from_first_six( \WC_Paytrace::get_field( 'masked_number', $card, '' ) ); | |
$order->add_order_note( sprintf( 'Card type: %s', $card_type ) ); | |
} | |
} | |
catch ( Exception $e ) { | |
// Log the exceptions | |
\WC_Paytrace::add_debug_log( 'VD Add Order Details: Something went wrong. Error: ' . print_r( $e->getMessage(), true ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment