Skip to content

Commit

Permalink
feat: Add popup info to donations (#2300)
Browse files Browse the repository at this point in the history
* feat: add popup info to stripe donations

* feat: add popup info to WC checkout

* test: fix test

* feat: rename referer metadata
  • Loading branch information
leogermani authored Feb 24, 2023
1 parent ab407d4 commit 7ea800b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
23 changes: 22 additions & 1 deletion includes/class-donations.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static function init() {
add_filter( 'pre_option_woocommerce_enable_guest_checkout', [ __CLASS__, 'disable_guest_checkout' ] );
add_action( 'woocommerce_check_cart_items', [ __CLASS__, 'handle_cart' ] );
add_filter( 'amp_skip_post', [ __CLASS__, 'should_skip_amp' ], 10, 2 );
add_action( 'woocommerce_checkout_create_order_line_item', [ __CLASS__, 'checkout_create_order_line_item' ], 10, 4 );
}
}

Expand Down Expand Up @@ -653,7 +654,9 @@ function ( $item ) {
0,
[],
[
'nyp' => (float) \WC_Name_Your_Price_Helpers::standardize_number( $donation_value ),
'nyp' => (float) \WC_Name_Your_Price_Helpers::standardize_number( $donation_value ),
'referer' => $referer,
'newspack_popup_id' => filter_input( INPUT_GET, 'newspack_popup_id', FILTER_SANITIZE_NUMBER_INT ),
]
);
}
Expand Down Expand Up @@ -685,6 +688,24 @@ function ( $item ) {
exit;
}

/**
* Add cart metadata to the order line item.
*
* @param \WC_Order_Item_Product $item The cart item.
* @param string $cart_item_key The cart item key.
* @param array $values The cart item values.
* @param \WC_Order $order The order.
* @return void
*/
public static function checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
if ( ! empty( $values['newspack_popup_id'] ) ) {
$order->add_meta_data( '_newspack_popup_id', $values['newspack_popup_id'] );
}
if ( ! empty( $values['referer'] ) ) {
$order->add_meta_data( '_newspack_referer', $values['referer'] );
}
}

/**
* Create the donation page prepopulated with CTAs for the subscriptions.
*
Expand Down
8 changes: 8 additions & 0 deletions includes/reader-revenue/class-woocommerce-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ private static function add_universal_order_data( $order, $order_data ) {
$order->add_meta_data( NEWSPACK_CLIENT_ID_COOKIE_NAME, $order_data['client_id'] );
}

if ( ! empty( $order_data['referer'] ) ) {
$order->add_meta_data( '_newspack_referer', $order_data['referer'] );
}

if ( ! empty( $order_data['newspack_popup_id'] ) ) {
$order->add_meta_data( '_newspack_popup_id', $order_data['newspack_popup_id'] );
}

if ( ! empty( $order_data['user_id'] ) ) {
$order->set_customer_id( $order_data['user_id'] );
}
Expand Down
2 changes: 2 additions & 0 deletions includes/reader-revenue/stripe/class-stripe-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,8 @@ public static function create_wc_transaction_payload( $customer, $payment ) {
'client_id' => $customer['metadata']['clientId'],
'user_id' => $customer['metadata']['userId'],
'subscribed' => self::has_customer_opted_in_to_newsletters( $customer ),
'referer' => $payment['referer'] ?? null,
'newspack_popup_id' => $payment['newspack_popup_id'] ?? null,
];
}

Expand Down
18 changes: 9 additions & 9 deletions includes/reader-revenue/stripe/class-stripe-webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,19 +267,19 @@ public static function receive_webhook( $request ) {
$client_id = isset( $customer['metadata']['clientId'] ) ? $customer['metadata']['clientId'] : null;
$origin = isset( $customer['metadata']['origin'] ) ? $customer['metadata']['origin'] : null;

$referer = '';
if ( isset( $metadata['referer'] ) ) {
$referer = $metadata['referer'];
}

$frequency = Stripe_Connection::get_frequency_of_payment( $payment );

$referer = $metadata['referer'] ?? '';
$newspack_popup_id = $metadata['newspack_popup_id'] ?? '';
if ( $payment['invoice'] ) {
$invoice = Stripe_Connection::get_invoice( $payment['invoice'] );
if ( ! \is_wp_error( $invoice ) && isset( $invoice['metadata']['referer'] ) ) {
$referer = $invoice['metadata']['referer'];
if ( ! \is_wp_error( $invoice ) ) {
$referer = $invoice['metadata']['referer'] ?? $referer;
$newspack_popup_id = $invoice['metadata']['newspack_popup_id'] ?? $newspack_popup_id;
}
}
$payment['referer'] = $referer;
$payment['newspack_popup_id'] = $newspack_popup_id;

$frequency = Stripe_Connection::get_frequency_of_payment( $payment );

// Update data in Newsletters provider.
$was_customer_added_to_mailing_list = false;
Expand Down
4 changes: 4 additions & 0 deletions tests/unit-tests/stripe-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ public static function test_stripe_wc_transaction_payload() {
'invoice' => 'in_123',
'created' => 1234567890,
'balance_transaction' => 'txn_123',
'referer' => 'sample_referer',
'newspack_popup_id' => 123,
];
self::assertEquals(
Stripe_Connection::create_wc_transaction_payload( $customer, $payment ),
Expand All @@ -175,6 +177,8 @@ public static function test_stripe_wc_transaction_payload() {
'client_id' => 'abc123',
'user_id' => 42,
'subscribed' => null,
'referer' => 'sample_referer',
'newspack_popup_id' => 123,
]
);
}
Expand Down

0 comments on commit 7ea800b

Please sign in to comment.