Skip to content

Commit

Permalink
Purchases: Reduxify notices in pyment method forms (#49541)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Feb 9, 2021
1 parent 3d3e36d commit 227d59b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
34 changes: 23 additions & 11 deletions client/me/purchases/components/payment-method-form/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { camelCase, kebabCase, debounce } from 'lodash';
/**
* Internal dependencies
*/
import notices from 'calypso/notices';
import {
handleRenewNowClick,
isRenewable,
shouldAddPaymentSourceInsteadOfRenewingNow,
} from 'calypso/lib/purchases';
import wpcomFactory from 'calypso/lib/wp';
import { infoNotice, successNotice } from 'calypso/state/notices/actions';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';

const wpcom = wpcomFactory.undocumented();
Expand All @@ -28,6 +28,7 @@ export async function saveOrUpdateCreditCard( {
formFieldValues,
stripeConfiguration,
parseTokenFromResponse,
reduxDispatch,
} ) {
const token = await getTokenForSavingCard( {
formFieldValues,
Expand All @@ -42,6 +43,7 @@ export async function saveOrUpdateCreditCard( {
translate,
saveStoredCard,
stripeConfiguration,
reduxDispatch,
} );
}

Expand All @@ -53,6 +55,7 @@ export async function saveOrUpdateCreditCard( {
token,
translate,
stripeConfiguration,
reduxDispatch,
} );
}

Expand All @@ -71,15 +74,23 @@ export async function getTokenForSavingCard( {
return token;
}

async function saveCreditCard( { token, translate, saveStoredCard, stripeConfiguration } ) {
async function saveCreditCard( {
token,
translate,
saveStoredCard,
stripeConfiguration,
reduxDispatch,
} ) {
const additionalData = stripeConfiguration
? { payment_partner: stripeConfiguration.processor_id }
: {};
await saveStoredCard( { token, additionalData } );
recordTracksEvent( 'calypso_purchases_add_new_payment_method' );
notices.success( translate( 'Card added successfully' ), {
persistent: true,
} );
reduxDispatch(
successNotice( translate( 'Card added successfully' ), {
isPersistent: true,
} )
);
}

export async function updateCreditCard( {
Expand All @@ -90,6 +101,7 @@ export async function updateCreditCard( {
token,
translate,
stripeConfiguration,
reduxDispatch,
} ) {
const cardDetails = kebabCaseFormFields( formFieldValues );
const updatedCreditCardApiParams = getParamsForApi(
Expand All @@ -107,14 +119,14 @@ export async function updateCreditCard( {
recordTracksEvent( 'calypso_purchases_save_new_payment_method' );

let noticeMessage = response.success;
let noticeOptions = { persistent: true };
let noticeOptions = { isPersistent: true };

if ( purchaseIsRenewable && shouldAddPaymentSourceInsteadOfRenewingNow( purchase ) ) {
noticeMessage = translate( 'Your credit card details were successfully updated.' );
noticeOptions = {
persistent: true,
isPersistent: true,
};
notices.success( noticeMessage, noticeOptions );
reduxDispatch( successNotice( noticeMessage, noticeOptions ) );
return;
}

Expand All @@ -128,13 +140,13 @@ export async function updateCreditCard( {
handleRenewNowClick( purchase, siteSlug );
closeFunction();
},
persistent: true,
isPersistent: true,
};
notices.info( noticeMessage, noticeOptions );
reduxDispatch( infoNotice( noticeMessage, noticeOptions ) );
return;
}

notices.success( noticeMessage, noticeOptions );
reduxDispatch( successNotice( noticeMessage, noticeOptions ) );
}

export function getParamsForApi( cardDetails, cardToken, stripeConfiguration, extraParams = {} ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export function PaymentMethodForm( {
formFieldValues,
stripeConfiguration,
parseTokenFromResponse: parseStripeToken,
reduxDispatch,
} );
successCallback();
} catch ( error ) {
Expand Down

0 comments on commit 227d59b

Please sign in to comment.