Skip to content

Commit

Permalink
fix(popups): use new Campaigns method for creating donation events on…
Browse files Browse the repository at this point in the history
… new orders (#1794)

* fix(popups): use new Campaigns method for creating donation events on new orders

* refactor: use action hook instead of calling Campaigns methods directly

* chore: remove unneeded condition

* refactor: use a hook for WooCommerce donations, too

* fix: add client_id to contact info passed from Stripe
  • Loading branch information
dkoo authored Jul 22, 2022
1 parent 717b5b8 commit 49dc14c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function get_lists() {
*/
public function add_contact( $contact, $list_id ) {
if ( $this->is_configured() ) {
return \Newspack_Newsletters::add_contact( $contact, $list_id );
return \Newspack_Newsletters_Subscription::add_contact( $contact, $list_id );
}
}

Expand Down
72 changes: 36 additions & 36 deletions includes/reader-revenue/class-stripe-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ public static function receive_webhook( $request ) {
$metadata = $payment['metadata'];
$customer = self::get_customer_by_id( $payment['customer'] );
$amount_normalised = self::normalise_amount( $payment['amount'], $payment['currency'] );
$client_id = isset( $customer['metadata']['clientId'] ) ? $customer['metadata']['clientId'] : null;

$referer = '';
if ( isset( $metadata['referer'] ) ) {
Expand Down Expand Up @@ -313,47 +314,46 @@ public static function receive_webhook( $request ) {
$stripe_data = self::get_stripe_data();
if ( ! empty( $stripe_data['newsletter_list_id'] ) && isset( $customer['metadata']['newsletterOptIn'] ) && 'true' === $customer['metadata']['newsletterOptIn'] ) {
$newsletters_configuration_manager = Configuration_Managers::configuration_manager_class_for_plugin_slug( 'newspack-newsletters' );
// Note: With Mailchimp, this is adding the contact as 'pending' - the subscriber has to confirm.
$newsletters_configuration_manager->add_contact(
[
'email' => $customer['email'],
'name' => $customer['name'],
'metadata' => [
'donation_date' => gmdate( 'Y-m-d', $payment['created'] ),
'donation_amount' => $amount_normalised,
'donation_frequency' => $frequency,
'donation_recurring' => 'once' !== $frequency,
],

$contact = [
'email' => $customer['email'],
'name' => $customer['name'],
'metadata' => [
'donation_date' => gmdate( 'Y-m-d', $payment['created'] ),
'donation_amount' => $amount_normalised,
'donation_frequency' => $frequency,
'donation_recurring' => 'once' !== $frequency,
],
$stripe_data['newsletter_list_id']
);
];

if ( ! empty( $client_id ) ) {
$contact['client_id'] = $client_id;
}

// Note: With Mailchimp, this is adding the contact as 'pending' - the subscriber has to confirm.
$newsletters_configuration_manager->add_contact( $contact, $stripe_data['newsletter_list_id'] );
$was_customer_added_to_mailing_list = true;
}

// Update data in Campaigns plugin.
if ( isset( $customer['metadata']['clientId'] ) && class_exists( 'Newspack_Popups_Segmentation' ) ) {
$client_id = $customer['metadata']['clientId'];
if ( ! empty( $client_id ) ) {
$donation_data = [
'stripe_id' => $payment['id'],
'stripe_customer_id' => $customer['id'],
'date' => $payment['created'],
'amount' => $amount_normalised,
'frequency' => $frequency,
];
$client_update = [
'donation' => $donation_data,
];
if ( $was_customer_added_to_mailing_list ) {
$client_update['email_subscription'] = [
'email' => $customer['email'],
];
}
\Newspack_Popups_Segmentation::update_client_data(
$client_id,
$client_update
);
}
if ( ! empty( $client_id ) ) {
$donation_data = [
'stripe_id' => $payment['id'],
'stripe_customer_id' => $customer['id'],
'date' => $payment['created'],
'amount' => $amount_normalised,
'frequency' => $frequency,
];

/**
* When a new Stripe transaction occurs that can be associated with a client ID,
* fire an action with the client ID and the relevant donation info.
*
* @param string $client_id Client ID.
* @param array $donation_data Info about the transaction.
* @param string|null $newsletter_email If the user signed up for a newsletter as part of the transaction, the subscribed email address. Otherwise, null.
*/
do_action( 'newspack_new_donation_stripe', $client_id, $donation_data, $was_customer_added_to_mailing_list ? $customer['email'] : null );
}

// Send custom event to GA.
Expand Down
10 changes: 9 additions & 1 deletion includes/reader-revenue/class-woocommerce-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,15 @@ public static function create_transaction( $order_data ) {
$order->add_meta_data( '_stripe_currency', $order_data['currency'] );

if ( ! empty( $order_data['client_id'] ) ) {
$order->add_meta_data( NEWSPACK_CLIENT_ID_COOKIE_NAME, $order_data['client_id'] );
/**
* When a new order is created that can be associated with a client ID,
* fire an action with the client ID and the relevant order info.
*
* @param WC_Order $order Donation order.
* @param string $client_id Client ID.
* @param string|null $newsletter_email If the user signed up for a newsletter as part of the transaction, the subscribed email address. Otherwise, null.
*/
do_action( 'newspack_new_donation_woocommerce', $order, $order_data['client_id'] );
}

$has_user_id = ! empty( $order_data['user_id'] );
Expand Down

0 comments on commit 49dc14c

Please sign in to comment.