Skip to content

Commit

Permalink
feat: Show Klarna widget
Browse files Browse the repository at this point in the history
  • Loading branch information
acasazza committed Apr 12, 2022
1 parent 888465b commit 638c252
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 80,
"semi": false,
"singleQuote": true,
"trailingComma": "es5"
}
44 changes: 36 additions & 8 deletions src/components/KlarnaPayment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,43 @@ export default function KlarnaPayment({
locale = 'auto',
...p
}: KlarnaPaymentProps) {
const { paymentSource } = useContext(PaymentMethodContext)
const loaded = useExternalScript('https://x.klarnacdn.net/kp/lib/v1/api.js')
console.log('loaded', loaded)
const [klarna, setKlarna] = useState<any>()
const {
containerClassName,
templateCustomerSaveToWallet,
fonts = [],
...divProps
} = p
useEffect(() => {
if (loaded && window?.Klarna !== undefined) {
setKlarna(window.Klarna)
}
}, [loaded, window.Klarna])
if (klarna && clientToken) {
// @ts-ignore
const [first] = paymentSource?.payment_methods
klarna.Payments.init({
client_token: clientToken,
})
klarna.Payments.load(
{
container: '#klarna-payments-container',
payment_method_category: first?.indentifier,
},
{},
(res: any) => {
console.log('res', res)
}
)
}
console.log('Klarna', klarna, clientToken)
// const [isLoaded, setIsLoaded] = useState(false)
// const [stripe, setStripe] = useState(null)
// const {
// containerClassName,
// templateCustomerSaveToWallet,
// fonts = [],
// ...divProps
// } = p
return null
return (
<div className={containerClassName} {...divProps}>
<div id="klarna-payments-container"></div>
</div>
)
}
9 changes: 4 additions & 5 deletions src/components/gateways/KlarnaGateway.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ export default function KlarnaGateway(props: KlarnaGateway) {
const locale = order?.language_code

if (!readonly && payment?.id !== currentPaymentMethodId) return null

// @ts-ignore
const clientToken = paymentSource?.client_token
const stripeConfig = config
? getPaymentConfig<'stripePayment'>(paymentResource, config)
const klarnaConfig = config
? getPaymentConfig<'klarnaPayment'>(paymentResource, config)
: {}
const customerPayments =
!isEmpty(payments) && payments
Expand Down Expand Up @@ -76,7 +75,7 @@ export default function KlarnaGateway(props: KlarnaGateway) {
templateCustomerSaveToWallet={templateCustomerSaveToWallet}
clientToken={clientToken}
locale={locale}
{...stripeConfig}
{...klarnaConfig}
/>
</Fragment>
)
Expand All @@ -87,7 +86,7 @@ export default function KlarnaGateway(props: KlarnaGateway) {
show={show}
clientToken={clientToken}
locale={locale}
{...stripeConfig}
{...klarnaConfig}
/>
) : (
loaderComponent
Expand Down

0 comments on commit 638c252

Please sign in to comment.