Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a filter to the IPN Notification order ID #133

Closed
wants to merge 1 commit into from

Conversation

eriktdesign
Copy link

@eriktdesign eriktdesign commented Dec 3, 2021

I'm using the woocommerce_amazon_pa_update_checkout_session_payload filter to send a custom order number (from the WooCommerce Sequential Order Numbers Pro plugin) to the API when the order is created. This works for sending the order to the Amazon API, and capturing the order works, but it breaks IPN updates because the API sends the sequential order number. I needed a way to filter the $order_id that is being processed in the WC_Amazon_Payments_Advanced_IPN_Handler::handle_notification_ipn_v2() method. I could maybe have used the woocommerce_amazon_pa_ipn_notification_order filter, but the function throws an exception if the $order_id is non-numeric -- ours are alphanumeric.

Adding this filter allows me to use a function internal to the Sequential Order Numbers plugin to fetch the proper order ID and pass it back to the IPN handler.

Please consider adding this small change so I don't have to manually change it whenever you update!

Thanks!
--et

All Submissions:

  • Does your code follow the Extendables standards?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?

Changes proposed in this Pull Request:

Closes # .

How to test the changes in this Pull Request:

Other information:

  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

Changelog entry

Enter a summary of all changes on this Pull Request. This will appear in the changelog if accepted.

I'm using the `woocommerce_amazon_pa_update_checkout_session_payload` filter to send a custom order number (from the WooCommerce Sequential Order Numbers Pro plugin) to the API when the order is created. This works for sending the order to the Amazon API, and capturing the order works, but it breaks IPN updates because the API sends the sequential order number. I needed a way to filter the `$order_id` that is being processed in the `WC_Amazon_Payments_Advanced_IPN_Handler::handle_notification_ipn_v2()` method. I could maybe have used the `woocommerce_amazon_pa_ipn_notification_order` filter, but the function throws an exception if the `$order_id` is non-numeric -- ours are alphanumeric.

Adding this filter allows me to use a function internal to the Sequential Order Numbers plugin to fetch the proper order ID and pass it back to the IPN handler.

Please consider adding this small change so I don't have to manually change it whenever you update! 

Thanks!
--et
@eriktdesign
Copy link
Author

Can someone take a look at this please?

@dpanta94
Copy link
Contributor

Hey @eriktdesign, a filter that serves this purpose has been introduced already in the plugin's latest version. Please have a look in this PR which introduces it.

Thank you!

@eriktdesign
Copy link
Author

Thanks @dpanta94, that was extremely timely!

For anyone else who needs this plugin to work with the Sequential Order Numbers Pro plugin, here's a mini-plugin you can use to patch your custom order numbers into Seller Central -- just create a PHP file in your plugins directory with this code and activate.

<?php
/**
 * Plugin Name: WooCommerce Amazon Pay Sequential Order Numbers Compatibility
 * Description: Adds filters for compatibility between Sequential Order Numbers and Amazon Pay for WooCommerce
 * Version:     1.0.0
 * Author:      Erik Teichmann
 * Text Domain: apson
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * Filter the merchant reference ID sent to Amazon
 * 
 * @hooked woocommerce_amazon_pa_merchant_metadata_reference_id - 10
 *
 * @param int $order_id Order ID of the current order
 * @return string Order number from the Sequential Order Numbers plugin
 */
function apson_outgoing_order_number( $order_id ) {
	$order = wc_get_order( $order_id );
	return $order->get_order_number();
}
add_filter( 'woocommerce_amazon_pa_merchant_metadata_reference_id', 'apson_outgoing_order_number', 10, 1 );

/**
 * Translate the merchant ref ID (order number) back to the database order_id
 *
 * @hooked woocommerce_amazon_pa_merchant_metadata_reference_id_reverse - 10
 * 
 * @param string $order_number Merchant reference ID passed back from Amazon
 * @return int Order ID for this order in the database
 */
function apson_incoming_order_number( $order_number ) {
	$order_id = wc_seq_order_number_pro()->find_order_by_order_number( $order_number );
	if ( ! empty( $order_id ) ) return $order_id;
	else return $order_number;
}
add_filter( 'woocommerce_amazon_pa_merchant_metadata_reference_id_reverse', 'apson_incoming_order_number', 10, 1 );

Disclaimer: I'm not responsible if this breaks your site etc. etc.

@eriktdesign eriktdesign closed this Jun 8, 2022
@eriktdesign eriktdesign deleted the patch-1 branch June 8, 2022 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants