-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1647 from joincivil/nickreynolds/add-card
Add/Remove Card via Account Page Payment Methods Tab
- Loading branch information
Showing
9 changed files
with
402 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/components/src/Payments/PaymentsStripeCardComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as React from "react"; | ||
import styled from "styled-components"; | ||
import { PaymentInputLabel } from "./PaymentsStyledComponents"; | ||
import { InputValidationUI, InputStripeValidationUI, StripeElement, InputErrorMessage } from "./PaymentsInputValidationUI"; | ||
import { PaymentEmailPrepopulatedText, PaymentEmailConfirmationText, AddCardEmailConfirmationText } from "./PaymentsTextComponents"; | ||
import { CardElement } from "react-stripe-elements"; | ||
|
||
const StripeWrapper = styled.div` | ||
margin: 20px 0 0; | ||
max-width: 500px; | ||
width: 100%; | ||
`; | ||
|
||
interface PaymentsStripeCardComponentProps { | ||
email: string; | ||
wasEmailPrepopulated: boolean; | ||
emailState: string; | ||
nameState: string; | ||
cardInfoState: string; | ||
displayStripeErrorMessage: string; | ||
showAddCardText?: boolean; | ||
handleOnBlur(event: any): void; | ||
handleStripeChange(event: any): void; | ||
} | ||
|
||
export const PaymentsStripeCardComponent: React.FunctionComponent<PaymentsStripeCardComponentProps> = props => { | ||
const { email, wasEmailPrepopulated, emailState, nameState, cardInfoState, displayStripeErrorMessage, handleOnBlur, handleStripeChange, showAddCardText } = props; | ||
return ( | ||
<StripeWrapper> | ||
{wasEmailPrepopulated && <PaymentEmailPrepopulatedText email={email} />} | ||
{!wasEmailPrepopulated && ( | ||
<> | ||
<PaymentInputLabel>Email</PaymentInputLabel> | ||
<InputValidationUI inputState={emailState}> | ||
<input | ||
defaultValue={email} | ||
id="email" | ||
name="email" | ||
type="email" | ||
maxLength={254} | ||
onBlur={() => handleOnBlur(event)} | ||
/> | ||
{showAddCardText && <AddCardEmailConfirmationText />} | ||
{!showAddCardText && <PaymentEmailConfirmationText />} | ||
</InputValidationUI> | ||
</> | ||
)} | ||
<PaymentInputLabel>Name on card</PaymentInputLabel> | ||
<InputValidationUI inputState={nameState}> | ||
<input id="name" name="name" onBlur={() => handleOnBlur(event)} required /> | ||
</InputValidationUI> | ||
<PaymentInputLabel>Card information</PaymentInputLabel> | ||
<InputStripeValidationUI inputState={cardInfoState}> | ||
<StripeElement inputState={cardInfoState}> | ||
<CardElement | ||
id="card-info" | ||
style={{ base: { fontSize: "13px" } }} | ||
onChange={handleStripeChange} | ||
/> | ||
</StripeElement> | ||
{displayStripeErrorMessage !== "" && ( | ||
<InputErrorMessage>{displayStripeErrorMessage}</InputErrorMessage> | ||
)} | ||
</InputStripeValidationUI> | ||
</StripeWrapper> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./AvatarLogin"; | ||
export * from "./Payments"; | ||
export * from "./PaymentsModal"; | ||
export * from "./PaymentsStripeCardComponent"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
packages/dapp/src/components/Dashboard/Account/AccountAddCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import * as React from "react"; | ||
import { PaymentsStripeCardComponent, Button, buttonSizes, InvertedButton } from "@joincivil/components"; | ||
import { isValidEmail } from "@joincivil/utils"; | ||
import { injectStripe, ReactStripeElements } from "react-stripe-elements"; | ||
import ApolloClient from "apollo-client"; | ||
import { CREATE_PAYMENT_METHOD } from "@joincivil/components/src/Payments/queries"; | ||
import styled from "styled-components"; | ||
|
||
export interface AccountAddCardProps extends ReactStripeElements.InjectedStripeProps { | ||
userEmail: string; | ||
userChannelID: string; | ||
apolloClient: ApolloClient<any>; | ||
handleCancel(): void; | ||
handleAdded(): void; | ||
} | ||
|
||
export interface AccountAddCardState { | ||
email: string; | ||
emailState: string; | ||
name: string; | ||
nameState: string; | ||
cardInfoState: string; | ||
wasEmailPrepopulated: boolean; | ||
displayStripeErrorMessage: string; | ||
addCardDisabled: boolean; | ||
} | ||
|
||
enum INPUT_STATE { | ||
EMPTY = "empty", | ||
VALID = "valid", | ||
INVALID = "invalid", | ||
} | ||
|
||
const ButtonDiv = styled.div` | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: flex-end; | ||
`; | ||
|
||
const SaveButton = styled(Button)` | ||
margin-left: 10px; | ||
`; | ||
|
||
class AccountAddCard extends React.Component<AccountAddCardProps, AccountAddCardState> { | ||
|
||
constructor(props: AccountAddCardProps) { | ||
super(props); | ||
this.state = { | ||
email: props.userEmail || "", | ||
wasEmailPrepopulated: props.userEmail ? true : false, | ||
emailState: this.props.userEmail ? INPUT_STATE.VALID : INPUT_STATE.EMPTY, | ||
name: "", | ||
nameState: INPUT_STATE.EMPTY, | ||
cardInfoState: INPUT_STATE.EMPTY, | ||
displayStripeErrorMessage: "", | ||
addCardDisabled: false, | ||
}; | ||
} | ||
|
||
public render(): JSX.Element { | ||
return ( | ||
<div> | ||
<PaymentsStripeCardComponent | ||
email={this.state.email} | ||
emailState={this.state.emailState} | ||
name={this.state.name} | ||
nameState={this.state.nameState} | ||
cardInfoState={this.state.cardInfoState} | ||
wasEmailPrepopulated={this.state.wasEmailPrepopulated} | ||
displayStripeErrorMessage={this.state.displayStripeErrorMessage} | ||
handleOnBlur={this.handleOnBlur} | ||
handleStripeChange={this.handleStripeChange} | ||
showAddCardText={true} | ||
/> | ||
<ButtonDiv> | ||
<InvertedButton onClick={this.props.handleCancel} size={buttonSizes.SMALL}>Cancel</InvertedButton> | ||
<SaveButton onClick={this.handleAddCard} disabled={this.state.addCardDisabled} size={buttonSizes.SMALL}>Save</SaveButton> | ||
</ButtonDiv> | ||
</div> | ||
); | ||
} | ||
|
||
private handleAddCard = async () => { | ||
try { | ||
this.setState({addCardDisabled: true}); | ||
const result = await (this.props.stripe as any).createPaymentMethod({ | ||
type: "card", | ||
card: (this.props as any).elements.getElement("card"), | ||
billing_details: { | ||
name: this.state.name, | ||
email: this.state.email, | ||
}, | ||
}); | ||
|
||
const paymentMethodID = result.paymentMethod.id; | ||
|
||
const paymentMethodVariables = { | ||
input: { | ||
paymentMethodID, | ||
emailAddress: this.state.email, | ||
payerChannelID: this.props.userChannelID, | ||
}, | ||
}; | ||
|
||
const paymentMethodResult = await this.props.apolloClient.mutate({ | ||
mutation: CREATE_PAYMENT_METHOD, | ||
variables: paymentMethodVariables, | ||
}); | ||
if (paymentMethodResult.error) { | ||
console.error(paymentMethodResult.error); | ||
this.setState({ addCardDisabled: false }); | ||
} | ||
this.props.handleAdded(); | ||
this.setState({addCardDisabled: false}); | ||
} catch (err) { | ||
this.setState({addCardDisabled: false}); | ||
} | ||
} | ||
|
||
private handleOnBlur = (event: any) => { | ||
const state = event.target.id; | ||
const value = event.target.value; | ||
|
||
switch (state) { | ||
case "email": | ||
const validEmail = isValidEmail(value); | ||
validEmail | ||
? this.setState({ email: value, emailState: INPUT_STATE.VALID }) | ||
: this.setState({ emailState: INPUT_STATE.INVALID }); | ||
break; | ||
case "name": | ||
const validName = value !== ""; | ||
validName | ||
? this.setState({ name: value, nameState: INPUT_STATE.VALID }) | ||
: this.setState({ nameState: INPUT_STATE.INVALID }); | ||
break; | ||
default: | ||
break; | ||
} | ||
}; | ||
|
||
private handleStripeChange = (event: any) => { | ||
const stripeElements = document.querySelectorAll(".StripeElement"); | ||
let displayStripeErrorMessage = ""; | ||
|
||
if (event.error) { | ||
displayStripeErrorMessage = event.error.message; | ||
} | ||
|
||
stripeElements.forEach(element => { | ||
const classList = element.classList; | ||
if (classList.contains("StripeElement--invalid")) { | ||
this.setState({ cardInfoState: INPUT_STATE.INVALID, displayStripeErrorMessage }); | ||
} else if (classList.contains("StripeElement--empty")) { | ||
this.setState({ cardInfoState: INPUT_STATE.EMPTY, displayStripeErrorMessage }); | ||
} else { | ||
this.setState({ cardInfoState: INPUT_STATE.VALID, displayStripeErrorMessage }); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
export default injectStripe(AccountAddCard); |
Oops, something went wrong.