/
checkout.js
156 lines (144 loc) · 4.06 KB
/
checkout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { getCheckoutSession, createPayment, getPublicApiKey } from "./api.js"
async function initCheckout () {
// get checkout session from merchan back
const { checkout_session: checkoutSession, country: countryCode } = await getCheckoutSession()
// get api key
const publicApiKey = await getPublicApiKey()
// start Yuno SDK
const yuno = Yuno.initialize(publicApiKey)
/**
* checkout configuration
*/
const loader = document.getElementById('loader')
let isPaying = false
yuno.startCheckout({
checkoutSession,
// element where the SDK will be mount on
elementSelector: '#root',
/**
* country can be one of CO, BR, CL, PE, EC, UR, MX
*/
countryCode,
/**
* language can be one of es, en, pt
*/
language: 'es',
/**
* Hide or show the Yuno loading/spinner page
* default is true
*/
showLoading: true,
/**
* Enable this will keep showing the loading component until either hideLoader() or
* continuePayment() are called.
* Default is true
*/
keepLoader: true,
/**
*
* @param { isLoading: boolean, type: 'DOCUMENT' | 'ONE_TIME_TOKEN' } data
*/
onLoading: (args) => {
if (!isPaying) {
loader.style.display = 'none'
}
},
/**
* Where the forms a shown
* default { type: 'modal' }
*/
renderMode: {
/**
* type can be one of `modal` or `element`
* default modal
*/
type: 'modal',
/**
* element where the form will be rendered
* only needed if type is element
*/
elementSelector: '#form-element',
},
/**
* API card
*/
card: {
/**
* mode render card can be step or extends
* default extends
*/
type: "extends",
/**
* you can edit card form styles
* only you should write css then it will be injected into the iframe
* example
* `@import url('https://fonts.googleapis.com/css2?family=Luckiest+Guy&display=swap');
* .Yuno-front-side-card__name-label {
* color: red !important;
* font-family: 'Luckiest Guy' !important;
* }`
*/
styles: '',
},
/**
* Use external SDKs buttons like PayPal, Paga con Rappi
*/
externalPaymentButtons: {
paypal: {
elementSelector: '#paypal',
}
},
/**
* calback is called when one time token is created,
* merchant should create payment back to back
* @param { oneTimeToken: string } data
*/
async yunoCreatePayment(oneTimeToken) {
loader.style.display = 'block'
isPaying = true
await createPayment({ oneTimeToken, checkoutSession })
/**
* call only if the SDK needs to continue the payment flow
*/
yuno.continuePayment()
},
/**
* callback is called when user selects a payment method
* @param { {type: 'BANCOLOMBIA_TRANSFER' | 'PIX' | 'ADDI' | 'NU_PAY' | 'MERCADO_PAGO_CHECKOUT_PRO', name: string} } data
*/
yunoPaymentMethodSelected(data) {
console.log('onPaymentMethodSelected', data)
},
/**
*
* @param {'READY_TO_PAY' | 'CREATED' | 'SUCCEEDED' | 'REJECTED' | 'CANCELLED' | 'ERROR' | 'DECLINED' | 'PENDING' | 'EXPIRED' | 'VERIFIED' | 'REFUNDED'} data
*/
yunoPaymentResult(data) {
console.log('yunoPaymentResult', data)
/**
* call if you set `keepLoader = true` and you want to hide the loader
*/
yuno.hideLoader()
},
/**
* @param { error: 'CANCELED_BY_USER' | any }
*/
yunoError: (error) => {
console.log('There was an error', error)
/**
* call if you set `keepLoader = true` and you want to hide the loader
*/
yuno.hideLoader()
},
})
/**
* mount checkout in browser DOM
*/
yuno.mountCheckout()
// start payment when user clicks on merchant payment button
const PayButton = document.querySelector('#button-pay')
PayButton.addEventListener('click', () => {
yuno.startPayment()
})
}
window.addEventListener('load', initCheckout)