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

fix(fxa-payments-server): capture productId with PayPal payments #12566

Merged
merged 1 commit into from
Apr 20, 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 @@ -13,11 +13,10 @@ const defaultApiClientOverrides = {
const Subject = ({
disabled = false,
apiClientOverrides = defaultApiClientOverrides,
currencyCode = 'USD',
customer = CUSTOMER,
idempotencyKey = '',
refreshSubmitNonce = () => {},
priceId = PLAN.plan_id,
selectedPlan = PLAN,
newPaypalAgreement = false,
postSubscriptionAttemptPaypalCallback = linkTo('routes/Product', 'success'),
setSubscriptionError = () => {},
Expand All @@ -26,11 +25,10 @@ const Subject = ({
}: PickPartial<
PaypalButtonProps,
| 'disabled'
| 'currencyCode'
| 'customer'
| 'idempotencyKey'
| 'refreshSubmitNonce'
| 'priceId'
| 'selectedPlan'
| 'newPaypalAgreement'
| 'postSubscriptionAttemptPaypalCallback'
| 'setSubscriptionError'
Expand All @@ -41,11 +39,10 @@ const Subject = ({
{...{
disabled,
apiClientOverrides,
currencyCode,
customer,
idempotencyKey,
refreshSubmitNonce,
priceId,
selectedPlan,
newPaypalAgreement,
postSubscriptionAttemptPaypalCallback,
setSubscriptionError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import { CUSTOMER, PLAN } from '../../lib/mock-data';

const Subject = ({
disabled = false,
currencyCode = 'USD',
customer = CUSTOMER,
idempotencyKey = '',
refreshSubmitNonce = () => {},
priceId = PLAN.plan_id,
selectedPlan = PLAN,
newPaypalAgreement = false,
postSubscriptionAttemptPaypalCallback = () => {},
setSubscriptionError = () => {},
Expand All @@ -21,11 +20,10 @@ const Subject = ({
}: PickPartial<
PaypalButtonProps,
| 'disabled'
| 'currencyCode'
| 'customer'
| 'idempotencyKey'
| 'refreshSubmitNonce'
| 'priceId'
| 'selectedPlan'
| 'newPaypalAgreement'
| 'postSubscriptionAttemptPaypalCallback'
| 'setSubscriptionError'
Expand All @@ -35,11 +33,10 @@ const Subject = ({
<PaypalButton
{...{
disabled,
currencyCode,
customer,
idempotencyKey,
refreshSubmitNonce,
priceId,
selectedPlan,
newPaypalAgreement,
postSubscriptionAttemptPaypalCallback,
setSubscriptionError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { useCallback, useEffect, useState } from 'react';
import ReactDOM from 'react-dom';

import * as apiClient from '../../lib/apiClient';
import { Customer } from '../../store/types';
import { Customer, Plan } from '../../store/types';
import { SubscriptionCreateAuthServerAPIs } from '../../routes/Product/SubscriptionCreate';
import { PaymentUpdateAuthServerAPIs } from '../../routes/Subscriptions/PaymentUpdateForm';
import classNames from 'classnames';
Expand All @@ -19,15 +19,14 @@ declare var paypal: {
};

export type PaypalButtonProps = {
currencyCode: string;
customer: Customer | null;
disabled: boolean;
idempotencyKey: string;
refreshSubmitNonce: () => void;
postSubscriptionAttemptPaypalCallback: (() => void) | (() => Promise<void>);
beforeCreateOrder?: () => Promise<void>;
setSubscriptionError: Function;
priceId?: string;
selectedPlan: Plan;
newPaypalAgreement?: boolean;
apiClientOverrides?: Partial<
SubscriptionCreateAuthServerAPIs | PaymentUpdateAuthServerAPIs
Expand Down Expand Up @@ -56,21 +55,26 @@ export const PaypalButtonBase =
: null;

export const PaypalButton = ({
currencyCode,
customer,
disabled,
idempotencyKey,
refreshSubmitNonce,
postSubscriptionAttemptPaypalCallback,
beforeCreateOrder,
setSubscriptionError,
priceId,
selectedPlan,
newPaypalAgreement,
apiClientOverrides,
setTransactionInProgress,
ButtonBase = PaypalButtonBase,
promotionCode,
}: PaypalButtonProps) => {
const {
currency: currencyCode,
plan_id: priceId,
product_id: productId,
} = selectedPlan;

const createOrder = useCallback(async () => {
try {
if (beforeCreateOrder) {
Expand Down Expand Up @@ -125,6 +129,7 @@ export const PaypalButton = ({
idempotencyKey,
// @ts-ignore Doesn't like that the existence check for priceId is stored in isNewSubscription
priceId,
productId,
token,
promotionCode,
});
Expand Down Expand Up @@ -152,9 +157,9 @@ export const PaypalButton = ({
customer,
idempotencyKey,
newPaypalAgreement,
priceId,
refreshSubmitNonce,
postSubscriptionAttemptPaypalCallback,
selectedPlan,
setSubscriptionError,
setTransactionInProgress,
promotionCode,
Expand Down
2 changes: 2 additions & 0 deletions packages/fxa-payments-server/src/lib/apiClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,12 @@ describe('API requests', () => {
const params = {
idempotencyKey: 'idk-8675309',
priceId: 'price_12345',
productId: 'product_2a',
...MOCK_CHECKOUT_TOKEN,
};
const metricsOptions = {
planId: params.priceId,
productId: params.productId,
paymentProvider: 'paypal',
};

Expand Down
2 changes: 2 additions & 0 deletions packages/fxa-payments-server/src/lib/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export async function apiGetPaypalCheckoutToken(params: {
export async function apiCapturePaypalPayment(params: {
idempotencyKey: string;
priceId: string;
productId: string;
token?: string;
promotionCode?: string;
}): Promise<{
Expand All @@ -275,6 +276,7 @@ export async function apiCapturePaypalPayment(params: {
}> {
const metricsOptions: Amplitude.EventProperties = {
planId: params.priceId,
productId: params.productId,
caugner marked this conversation as resolved.
Show resolved Hide resolved
paymentProvider: 'paypal',
promotionCode: params.promotionCode,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ export const Checkout = ({
<div className="paypal-button">
<PaypalButton
beforeCreateOrder={beforePaypalCreateOrder}
currencyCode={selectedPlan.currency}
customer={null}
disabled={
!checkboxSet ||
Expand All @@ -385,7 +384,7 @@ export const Checkout = ({
}
idempotencyKey={submitNonce}
newPaypalAgreement={true}
priceId={selectedPlan.plan_id}
selectedPlan={selectedPlan}
refreshSubmitNonce={refreshSubmitNonce}
postSubscriptionAttemptPaypalCallback={
postSubscriptionAttemptPaypalCallback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export const SubscriptionCreate = ({
await apiCapturePaypalPayment({
idempotencyKey,
priceId,
productId: selectedPlan.product_id,
});
refreshSubscriptions();
} catch (error) {
Expand All @@ -193,7 +194,7 @@ export const SubscriptionCreate = ({
setInProgress(false);
refreshSubmitNonce();
},
[setInProgress, refreshSubmitNonce, refreshSubscriptions]
[setInProgress, refreshSubmitNonce, refreshSubscriptions, selectedPlan]
);

const onSubmit = getPaymentProviderMappedVal<
Expand Down Expand Up @@ -253,11 +254,10 @@ export const SubscriptionCreate = ({
<PaypalButton
disabled={!checkboxSet}
apiClientOverrides={apiClientOverrides}
currencyCode={selectedPlan.currency}
customer={customer}
idempotencyKey={submitNonce}
refreshSubmitNonce={refreshSubmitNonce}
priceId={selectedPlan.plan_id}
selectedPlan={selectedPlan}
newPaypalAgreement={true}
postSubscriptionAttemptPaypalCallback={
refreshSubscriptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,6 @@ export const PaymentUpdateForm = ({
.set('year', Number(exp_year))
.format('MMMM YYYY');

const currencyCode = plan!.currency;
const planId = plan!.plan_id;

return (
<div className="settings-unit">
<div className="payment-update" data-testid="payment-update">
Expand Down Expand Up @@ -250,11 +247,10 @@ export const PaymentUpdateForm = ({
<div className="paypal-button">
<PaypalButton
disabled={false}
currencyCode={currencyCode}
customer={customer}
idempotencyKey={submitNonce}
refreshSubmitNonce={refreshSubmitNonce}
priceId={planId}
selectedPlan={plan!}
newPaypalAgreement={false}
postSubscriptionAttemptPaypalCallback={refreshSubscriptions}
setSubscriptionError={setPaymentError}
Expand Down