Skip to content

Instantly share code, notes, and snippets.

@vanbo
Last active March 31, 2020 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanbo/121bbfcd9c4d718cf1354c8b33fd48a2 to your computer and use it in GitHub Desktop.
Save vanbo/121bbfcd9c4d718cf1354c8b33fd48a2 to your computer and use it in GitHub Desktop.
WooCommerce Psigate: Add Order details to XML API call
/**
* The code goes into your "theme\functions.php" file or any php file that is loaded on page load.
*/
add_filter( 'wc_psigate_process_credit_card_payment_parameters', 'prigate_xml_api_add_order_details', 11, 2 );
/**
* @param array $original_parameters
* @param WC_Order $order
*
* @return array
*/
function prigate_xml_api_add_order_details( $original_parameters, $order ) {
/**
* IMPORTANT: The snippet will add itemized details to the payment request.
* By doing this the total of the transaction will be taken from the order items, not from the passed order total.
* Please test your setup and thoroughly before going live.
* If you find any issues please let us know at: support@ vanbodevelops.com
*/
$i = 0;
$parameters = array();
if ( 0 < count( $order->get_items() ) ) {
foreach ( $order->get_items() as $item ) {
if ( \WcPsigate\Compatibility::get_item_quantity( $item ) ) {
$product = \WcPsigate\Compatibility::get_product_from_item( $item, $order );
$item_meta = \WcPsigate\Compatibility::wc_display_item_meta( $item );
$item_name = \WcPsigate\Compatibility::get_item_name( $item );
if ( $item_meta ) {
$item_name .= ' (' . $item_meta . ')';
}
$parameters[ $i ] = $this->build_request_item(
$this->html_entity_decode_numeric( $item_name ),
( $product->get_sku() ) ? $product->get_sku() : $product->id,
\WcPsigate\Compatibility::get_item_quantity( $item ),
$this->format_amount( $order->get_item_subtotal( $item ) ),
$this->format_amount( $order->get_line_tax( $item ) )
);
}
$i ++;
}
}
if ( 0 < \WcPsigate\Compatibility::get_total_shipping( $order ) ) {
$parameters[ $i ] = $this->build_request_item(
__( 'Shipping', WC_PsiGate::TEXT_DOMAIN ),
'shipping',
1,
$this->format_amount( \WcPsigate\Compatibility::get_total_shipping( $order ) ),
$this->format_amount( $order->get_shipping_tax() )
);
$i ++;
}
// Add the order discount
if ( 0 < $order->get_total_discount() ) {
$parameters[ $i ] = $this->build_request_item(
__( 'Discount', WC_PsiGate::TEXT_DOMAIN ),
'discount',
1,
- $this->format_amount( $order->get_total_discount() ),
$order->get_discount_tax()
);
$i ++;
}
$fees = $order->get_items( 'fee' );
if ( 0 < $fees ) {
foreach ( $fees as $fee ) {
$fee_name = \WcPsigate\Compatibility::get_item_name( $fee );
$parameters[ $i ] = $this->build_request_item(
$this->html_entity_decode_numeric( $fee_name ),
sanitize_title( $fee_name ),
1,
$this->format_amount( $fee['line_total'] ),
$this->format_amount( $fee['line_tax'] )
);
$i ++;
}
}
foreach ( $parameters as $i => $item ) {
$original_parameters['Item'][ $i ]['ItemDescription'] = $item['Description'];
$original_parameters['Item'][ $i ]['ItemID'] = $item['ProductID'];
$original_parameters['Item'][ $i ]['ItemQty'] = $item['Quantity'];
$original_parameters['Item'][ $i ]['ItemPrice'] = $item['Price'];
}
return $original_parameters;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment