Skip to content

Commit

Permalink
feat: if donation via a prompt, add prompt ID to Stripe payment metad…
Browse files Browse the repository at this point in the history
…ata (#1253)

* feat: if donation via a prompt, add prompt ID to Stripe payment metadata

* test: fix failing test

* fix: ensure we're targeting the right .newspack-popups

Co-authored-by: Adam Boro <adam@adamboro.com>

Co-authored-by: Adam Boro <adam@adamboro.com>
  • Loading branch information
dkoo and adekbadek authored Aug 26, 2022
1 parent 368e856 commit b8cb0e9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public function register_routes() {
'payment_method_id' => [
'sanitize_callback' => 'sanitize_text_field',
],
'origin' => [
'sanitize_callback' => 'sanitize_text_field',
],
],
'permission_callback' => '__return_true',
],
Expand Down Expand Up @@ -123,6 +126,7 @@ public function api_process_donation( $request ) {

$frequency = $request->get_param( 'frequency' );
$full_name = $request->get_param( 'full_name' );
$origin = $request->get_param( 'origin' );

$user_id = self::$current_user_id;

Expand Down Expand Up @@ -156,6 +160,7 @@ public function api_process_donation( $request ) {
'newsletterOptIn' => $request->get_param( 'newsletter_opt_in' ),
'userId' => $user_id,
'current_page_url' => \wp_get_referer(),
'origin' => $origin,
],
'payment_metadata' => $payment_metadata,
'payment_method_id' => $request->get_param( 'payment_method_id' ),
Expand Down
1 change: 1 addition & 0 deletions src/blocks/donate/streamlined/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ describe( 'Streamlined Donate block processing', () => {
newsletter_opt_in: false,
clientId: 'amp-123',
payment_method_id: 'pm_123',
origin: null,
},
},
'post'
Expand Down
10 changes: 10 additions & 0 deletions src/blocks/donate/streamlined/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ export const processStreamlinedElements = ( parentElement = document ) =>
}
}
const formValues = utils.getDonationFormValues( formElement );
const promptOrigin = formElement.closest( 'amp-layout.newspack-popup' );

// If the donation originated from a Campaigns prompt, append the prompt ID to the event label.
const origin =
promptOrigin && promptOrigin.hasAttribute( 'amp-access' )
? promptOrigin.getAttribute( 'amp-access' )
: null;

const apiRequestPayload = {
captchaToken,
tokenData: token,
Expand All @@ -117,8 +125,10 @@ export const processStreamlinedElements = ( parentElement = document ) =>
frequency: formValues.donation_frequency,
newsletter_opt_in: Boolean( formValues.newsletter_opt_in ),
clientId: formValues.cid,
origin,
...requestPayloadOverrides,
};

const chargeResultData = await utils.sendAPIRequest( '/donate', apiRequestPayload );

// Error handling.
Expand Down

0 comments on commit b8cb0e9

Please sign in to comment.