Skip to content

Commit

Permalink
feat: Normalize donation events (#2299)
Browse files Browse the repository at this point in the history
* feat: add item before marking as active so we can identify donation

* feat: use same hooks for all platforms

* feat: add donation_order_processed event and remove debug
  • Loading branch information
leogermani authored Feb 27, 2023
1 parent c13e741 commit 2624d53
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
58 changes: 34 additions & 24 deletions includes/data-events/listeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ function( $provider, $email, $lists_added, $lists_removed, $result ) {
);

/**
* For when there's a new donation through WooCommerce.
* For when there's a new donation processed through WooCommerce.
*/
Data_Events::register_listener(
'newspack_donation_order_processed',
'donation_new',
'donation_order_processed',
function( $order_id, $product_id ) {
$order = \wc_get_order( $order_id );
if ( ! $order ) {
Expand All @@ -134,14 +134,40 @@ function( $order_id, $product_id ) {
}
);


/**
* For when a Subscription is confirmed.
*/
Data_Events::register_listener(
'woocommerce_subscription_status_updated',
'donation_subscription_new',
function( $subscription, $status_to, $status_from ) {
if ( 'active' !== $status_to || 'pending' !== $status_from ) {
return;
}
$product_id = Donations::get_order_donation_product_id( $subscription->get_id() );
if ( ! $product_id ) {
return;
}
return [
'user_id' => $subscription->get_customer_id(),
'email' => $subscription->get_billing_email(),
'subscription_id' => $subscription->get_id(),
'amount' => (float) $subscription->get_total(),
'currency' => $subscription->get_currency(),
'recurrence' => get_post_meta( $product_id, '_subscription_period', true ),
'platform' => Donations::get_platform_slug(),
];
}
);

/**
* For when there's a new donation through the Stripe platform.
* For when there's a new donation confirmed
*/
Data_Events::register_listener(
'newspack_new_donation_woocommerce',
'woocommerce_order_status_pending_to_completed',
'donation_new',
function( $order, $client_id ) {
$order_id = $order->get_id();
function( $order_id, $order ) {
$product_id = Donations::get_order_donation_product_id( $order_id );
if ( ! $product_id ) {
return;
Expand All @@ -152,32 +178,16 @@ function( $order, $client_id ) {
'amount' => (float) $order->get_total(),
'currency' => $order->get_currency(),
'recurrence' => \get_post_meta( $product_id, '_subscription_period', true ),
'platform' => 'stripe',
'platform' => Donations::get_platform_slug(),
'platform_data' => [
'order_id' => $order_id,
'product_id' => $product_id,
'client_id' => $client_id,
'client_id' => $order->get_meta( NEWSPACK_CLIENT_ID_COOKIE_NAME ),
],
];
}
);

/**
* For when there's a new donation subscription.
*
* This will be fetched from a new donation, so we're hooking into the 'donation_new' dispatch.
*/
Data_Events::register_listener(
'newspack_data_event_dispatch_donation_new',
'donation_subscription_new',
function( $timestamp, $data ) {
if ( ! in_array( $data['recurrence'], [ 'month', 'year' ] ) ) {
return;
}
return $data;
}
);

/**
* For when a WooCommerce Subscription is cancelled.
*/
Expand Down
8 changes: 5 additions & 3 deletions includes/reader-revenue/class-woocommerce-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,16 +665,18 @@ public static function create_transaction( $order_data ) {
}
self::add_wc_stripe_gateway_metadata( $subscription, $wc_subscription_payload );
self::add_universal_order_data( $subscription, $order_data );

// Mint a new item – subscription is a new WC order.
$item = self::get_donation_order_item( $frequency, $order_data['amount'] );
$subscription->add_item( $item );

if ( false === $stripe_subscription_id ) {
$subscription->add_order_note( __( 'This subscription was created via Newspack.', 'newspack' ) );
} else {
/* translators: %s - donation frequency */
$subscription->add_order_note( sprintf( __( 'Newspack subscription with frequency: %s. The recurring payment and the subscription will be handled in Stripe, so you\'ll see "Manual renewal" as the payment method in WooCommerce.', 'newspack' ), $frequency ) );
$subscription->update_status( 'active' ); // Settings status via method (not in wcs_create_subscription), to make WCS recalculate dates.
}
// Mint a new item – subscription is a new WC order.
$item = self::get_donation_order_item( $frequency, $order_data['amount'] );
$subscription->add_item( $item );
$subscription->calculate_totals();

if ( false === $stripe_subscription_id ) {
Expand Down

0 comments on commit 2624d53

Please sign in to comment.