Skip to content

Instantly share code, notes, and snippets.

@stephywells
Last active July 6, 2020 07:03
Show Gist options
  • Save stephywells/b86d06dc67a3798135f8523ad1d05e32 to your computer and use it in GitHub Desktop.
Save stephywells/b86d06dc67a3798135f8523ad1d05e32 to your computer and use it in GitHub Desktop.
Void Shareasale commission when EDD payment is refunded
function s11_void_refunded( $payment ) {
global $edd_options;
if ( empty( $payment->ID ) ) {
return;
}
$merchant_id = $edd_options['sas_affiliate_id']; // TODO: If not using the EDD Shareasale addon, replace with your affiliate id
$api_token = 'aflj24sadflk3'; // TODO: Set API token here
$secret_key = 'asldkjlj12312oiksdlfksld'; // TODO: Set secrect key
$timestamp = gmdate( DATE_RFC1123 );
$version = 2.9;
$action = 'void';
$order_num = $payment->ID;
$order_date = date( 'm/d/Y', strtotime( $payment->date ) );
$sig = $api_token . ':' . $timestamp . ':' . $action . ':' . $secret_key;
$url = 'https://api.shareasale.com/w.cfm?merchantId=' . $merchant_id . '&token=' . $api_token . '&version=' . $version . '&action=' . $action . '&date=' . $order_date . '&ordernumber=' . $order_num . '&reason=ReturnedMerchandise';
$response = wp_remote_post( $url, array(
'method' => 'POST',
'headers' => array(
'x-ShareASale-Date' => $timestamp,
'x-ShareASale-Authentication' => hash( 'sha256', $sig ),
),
) );
$body = wp_remote_retrieve_body( $response );
if ( ! is_wp_error( $response ) && $body !== 'error' && ! is_wp_error( $body ) ) {
if ( strpos( $body, 'Transaction Voided' ) !== false ) {
// Add a note to the transaction
edd_insert_payment_note( $payment->ID, 'Affiliate commision voided in Shareasale.' );
}
}
}
add_action( 'edd_post_refund_payment', 's11_void_refunded', 40 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment