Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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