Skip to content

Commit

Permalink
Add Affiliate for WooCommerce support on WooPay (#7291)
Browse files Browse the repository at this point in the history
  • Loading branch information
alefesouza authored Nov 9, 2023
1 parent 5426408 commit cbcb806
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 16 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-woopay-affiliate-for-woocommerce-support
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Add Affiliate for WooCommerce support on WooPay.
48 changes: 47 additions & 1 deletion includes/woopay/class-woopay-adapted-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WooPay_Adapted_Extensions extends IntegrationRegistry {
/**
* Initializa WC Blocks regitered integrations.
*/
public function __construct() {
public function init() {
do_action( 'woocommerce_blocks_checkout_block_registration', $this );
}

Expand Down Expand Up @@ -171,9 +171,41 @@ public function get_extension_data() {
];
}

if ( $this->is_affiliate_for_woocommerce_enabled() ) {
/**
* @psalm-suppress UndefinedFunction
*/
$extension_data[ 'affiliate-for-woocommerce' ] = [
'affiliate-user' => afwc_get_referrer_id(),
];
}

return $extension_data;
}

/**
* Update order extension data after finishing
* an order on WooPay, this usually is needed
* for extensions which uses cookies when an
* order is finished.
*
* @param int $order_id The successful WooPay order.
*/
public function update_order_extension_data( $order_id ) {
if ( ! empty( $_GET['affiliate'] ) && // phpcs:ignore WordPress.Security.NonceVerification
$this->is_affiliate_for_woocommerce_enabled()
) {
$affiliate_id = (int) wc_clean( wp_unslash( $_GET['affiliate'] ) ); // phpcs:ignore WordPress.Security.NonceVerification

// phpcs:ignore
/**
* @psalm-suppress UndefinedClass
*/
$affiliate_api = \AFWC_API::get_instance();
$affiliate_api->track_conversion( $order_id, $affiliate_id, '', [ 'is_affiliate_eligible' => true ] );
}
}

/**
* Get WC Blocks registered integrations.
*
Expand All @@ -190,4 +222,18 @@ public function register( IntegrationInterface $integration ) {

return true;
}

/**
* Check if Affiliate for WooCommerce is enabled and
* its functions used on WCPay are available.
*
* @return boolean
*/
public function is_affiliate_for_woocommerce_enabled() {
return defined( 'AFWC_PLUGIN_FILE' ) &&
function_exists( 'afwc_get_referrer_id' ) &&
class_exists( 'AFWC_API' ) &&
method_exists( 'AFWC_API', 'get_instance' ) &&
method_exists( 'AFWC_API', 'track_conversion' );
}
}
22 changes: 13 additions & 9 deletions includes/woopay/class-woopay-session.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class WooPay_Session {
public static function init() {
add_filter( 'determine_current_user', [ __CLASS__, 'determine_current_user_for_woopay' ], 20 );
add_filter( 'woocommerce_session_handler', [ __CLASS__, 'add_woopay_store_api_session_handler' ], 20 );
add_action( 'woocommerce_order_payment_status_changed', [ __CLASS__, 'remove_order_customer_id_on_requests_with_verified_email' ] );
add_action( 'woocommerce_order_payment_status_changed', [ __CLASS__, 'woopay_order_payment_status_changed' ] );
add_action( 'woopay_restore_order_customer_id', [ __CLASS__, 'restore_order_customer_id_from_requests_with_verified_email' ] );

register_deactivation_hook( WCPAY_PLUGIN_FILE, [ __CLASS__, 'run_and_remove_woopay_restore_order_customer_id_schedules' ] );
Expand Down Expand Up @@ -159,25 +159,29 @@ public static function get_user_id_from_cart_token() {
}

/**
* Prevent set order customer ID on requests with
* Update order data for extensions which uses cookies,
* also prevent set order customer ID on requests with
* email verified to skip the login screen on the TYP.
* After 10 minutes, the customer ID will be restored
* and the user will need to login to access the TYP.
*
* @param \WC_Order $order_id The order ID being updated.
* @param int $order_id The order ID being updated.
*/
public static function remove_order_customer_id_on_requests_with_verified_email( $order_id ) {
$woopay_verified_email_address = self::get_woopay_verified_email_address();

if ( null === $woopay_verified_email_address ) {
public static function woopay_order_payment_status_changed( $order_id ) {
if ( ! self::is_woopay_enabled() ) {
return;
}

if ( ! self::is_woopay_enabled() ) {
if ( ! self::is_request_from_woopay() || ! self::is_store_api_request() ) {
return;
}

if ( ! self::is_request_from_woopay() || ! self::is_store_api_request() ) {
$woopay_adapted_extensions = new WooPay_Adapted_Extensions();
$woopay_adapted_extensions->update_order_extension_data( $order_id );

$woopay_verified_email_address = self::get_woopay_verified_email_address();

if ( null === $woopay_verified_email_address ) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions tests/unit/woopay/test-class-woopay-adapted-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function( $integration_registry ) {

$this->test_user = self::factory()->user->create_and_get();
$this->woopay_adapted_extensions = new WooPay_Adapted_Extensions();
$this->woopay_adapted_extensions->init();
}

public function test_get_adapted_extensions_data_without_enable_adapted_extensions() {
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/woopay/test-class-woopay-session.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function test_get_user_id_from_cart_token_with_verified_user_store_api_to
$this->assertEquals( WooPay_Session::get_user_id_from_cart_token(), $verified_user->ID );
}

public function test_remove_order_customer_id_on_requests_with_verified_email_with_verified_user_store_api_token_without_adapted_extensions() {
public function test_woopay_order_payment_status_changed_with_verified_user_store_api_token_without_adapted_extensions() {
$verified_user = self::factory()->user->create_and_get();

$woopay_store_api_token = WooPay_Store_Api_Token::init();
Expand All @@ -167,14 +167,14 @@ public function test_remove_order_customer_id_on_requests_with_verified_email_wi
$order = \WC_Helper_Order::create_order( $verified_user->ID );
$order->set_billing_email( $verified_user->user_email );
$order->save();
WooPay_Session::remove_order_customer_id_on_requests_with_verified_email( $order->get_Id() );
WooPay_Session::woopay_order_payment_status_changed( $order->get_Id() );

$updated_order = wc_get_order( $order->get_id() );
$this->assertEmpty( $updated_order->get_meta( 'woopay_merchant_customer_id' ) );
$this->assertEquals( $updated_order->get_customer_id(), $verified_user->ID );
}

public function test_remove_order_customer_id_on_requests_with_verified_email_with_verified_user_store_api_token_with_non_matching_order_billing_email() {
public function test_woopay_order_payment_status_changed_with_verified_user_store_api_token_with_non_matching_order_billing_email() {
$verified_user = self::factory()->user->create_and_get();

$woopay_store_api_token = WooPay_Store_Api_Token::init();
Expand All @@ -188,14 +188,14 @@ public function test_remove_order_customer_id_on_requests_with_verified_email_wi
$order = \WC_Helper_Order::create_order( $verified_user->ID );
$order->set_billing_email( '[email protected]' );
$order->save();
WooPay_Session::remove_order_customer_id_on_requests_with_verified_email( $order->get_id() );
WooPay_Session::woopay_order_payment_status_changed( $order->get_id() );

$updated_order = wc_get_order( $order->get_id() );
$this->assertEmpty( $updated_order->get_meta( 'woopay_merchant_customer_id' ) );
$this->assertEquals( $updated_order->get_customer_id(), $verified_user->ID );
}

public function test_remove_order_customer_id_on_requests_with_verified_email_with_verified_user_store_api_token() {
public function test_woopay_order_payment_status_changed_with_verified_user_store_api_token() {
$verified_user = self::factory()->user->create_and_get();

$woopay_store_api_token = WooPay_Store_Api_Token::init();
Expand All @@ -209,7 +209,7 @@ public function test_remove_order_customer_id_on_requests_with_verified_email_wi
$order = \WC_Helper_Order::create_order( $verified_user->ID );
$order->set_billing_email( $verified_user->user_email );
$order->save();
WooPay_Session::remove_order_customer_id_on_requests_with_verified_email( $order->get_id() );
WooPay_Session::woopay_order_payment_status_changed( $order->get_id() );

$updated_order = wc_get_order( $order->get_id() );
$this->assertEquals( $updated_order->get_meta( 'woopay_merchant_customer_id' ), $verified_user->ID );
Expand Down

0 comments on commit cbcb806

Please sign in to comment.