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

Checkout payment #1

Merged
merged 7 commits into from
Feb 13, 2020
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
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"vendor": "vtex",
"name": "base-store-component",
"name": "checkout-payment",
"version": "0.2.2",
"title": "IO Base Component",
"description": "IO Base component",
"defaultLocale": "pt-BR",
"builders": {
"messages": "1.x",
"store": "0.x",
"react": "3.x",
"docs": "0.x"
"docs": "0.x",
"store": "0.x"
},
"mustUpdateAt": "2019-04-02",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion react/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"browser": true,
"es6": true,
"jest": true
},
"rules": {
"@typescript-eslint/no-non-null-assertion": "off"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless there is good reason, I recommend removing this ESlint override

}
}
}
49 changes: 0 additions & 49 deletions react/Example.tsx

This file was deleted.

71 changes: 71 additions & 0 deletions react/Payment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React, { useState, useRef, useEffect, useCallback } from 'react'
import { useSSR } from 'vtex.render-runtime'

if (window && window.document) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
var postRobot = require('post-robot')
arturpimentel marked this conversation as resolved.
Show resolved Hide resolved
}

interface Card {
cardNumber: string
cardName: string
cardDate: string
cardCvv: string
}

const IFRAME_APP_VERSION = '0.1.1'
const LOCAL_IFRAME_DEVELOPMENT = false

let iframeURL = `https://io.vtexpayments.com.br/card-form-ui/${IFRAME_APP_VERSION}/index.html`

if (LOCAL_IFRAME_DEVELOPMENT) {
const PORT = 3000
iframeURL = `https://checkoutio.vtexlocal.com.br:${PORT}/`
}

const Payment: React.FC = () => {
const [cardData, setCardData] = useState<Card | null>(null)
const iframeRef = useRef<HTMLIFrameElement>(null)

const isSSR = useSSR()

const setupIframe = useCallback(() => {
const stylesheetsUrls = Array.from(
document.head.querySelectorAll<HTMLLinkElement>('link[rel=stylesheet]')
).map(link => link.href)

postRobot.send(iframeRef.current!.contentWindow, 'setup', {
arturpimentel marked this conversation as resolved.
Show resolved Hide resolved
stylesheetsUrls,
})
}, [])

useEffect(() => {
const listener = postRobot.on('card', ({ data }: { data: Card }) => {
setCardData(data)
return {
status: 'ok',
}
})
return () => listener.cancel()
}, [])

if (isSSR) {
return null
}

return (
<div>
<iframe
title="card-form-ui"
width="400px"
height="300px"
src={iframeURL}
onLoad={() => setupIframe()}
ref={iframeRef}
/>
{cardData && <p>{JSON.stringify(cardData)}</p>}
</div>
)
}

export default Payment
7 changes: 5 additions & 2 deletions react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
"dependencies": {
"@vtex/css-handles": "^1.0.0",
"classnames": "^2.2.6",
"post-robot": "^10.0.31",
"ramda": "^0.26.1",
"react": "^16.8.3",
"react-apollo": "^3.1.3",
"react-dom": "^16.8.3",
"react-intl": "^2.7.2"
"react-iframe": "^1.8.0",
"react-intl": "3.9.1"
},
"devDependencies": {
"@types/classnames": "^2.2.7",
Expand All @@ -24,7 +27,7 @@
"eslint-config-vtex-react": "^4.1.0",
"prettier": "^1.18.2",
"tslint-eslint-rules": "^5.4.0",
"typescript": "3.5.2"
"typescript": "3.7.3"
},
"version": "0.2.2"
}
2 changes: 1 addition & 1 deletion react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
"typeAcquisition": {
"enable": false
}
}
}
1 change: 1 addition & 0 deletions react/typings/post-robot.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'post-robot'
2 changes: 1 addition & 1 deletion react/typings/vtex.render-runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ declare module 'vtex.render-runtime' {

export const ChildBlock: ComponentType<ChildBlockProps>
export const useChildBlock = function({ id: string }): object {}

export const Helmet: ReactElement
export const Link: ReactType
export const NoSSR: ReactElement
Expand All @@ -35,4 +34,5 @@ declare module 'vtex.render-runtime' {
export const withRuntimeContext: <TOriginalProps extends {}>(
Component: ComponentType<TOriginalProps & RenderContextProps>
) => ComponentType<TOriginalProps>
export const useSSR: () => boolean
}
Loading