Skip to content

Commit 0982521

Browse files
committed
Adicionando arquivos referente a pagamento
1 parent 030fd23 commit 0982521

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

definicao-projeto.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Lab OneWeb",
3+
"host": "lab.oneweb.com.br",
4+
"user": "oneweb",
5+
"home-page": "https://lab.oneweb.com.br/",
6+
"version": "1.0.0",
7+
"remote-web": "[email protected]:lab.oneweb.com.br",
8+
"dir": "~/lab.oneweb.com.br/"
9+
}

payment-api/index.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="pt-BR">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Payment API</title>
8+
9+
</head>
10+
<body>
11+
<button>Pagar!</button>
12+
13+
<script src="https://storage.googleapis.com/prshim/v1/payment-shim.js" />
14+
<script src="payment.js" />
15+
</body>
16+
</html>

payment-api/payment.js

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
function onBuyClicked(event) {
2+
if (!window.PaymentRequest) {
3+
return;
4+
}
5+
// Payment Request API is available.
6+
// Stop the default anchor redirect.
7+
event.preventDefault();
8+
9+
var supportedInstruments = [{
10+
supportedMethods: [
11+
'visa', 'mastercard', 'amex', 'discover', 'maestro',
12+
'diners', 'jcb', 'unionpay', 'bitcoin'
13+
]
14+
}];
15+
16+
var details = {
17+
displayItems: [{
18+
label: 'Original donation amount',
19+
amount: { currency: 'USD', value: '65.00' }
20+
}, {
21+
label: 'Friends and family discount',
22+
amount: { currency: 'USD', value: '-10.00' }
23+
}],
24+
total: {
25+
label: 'Total due',
26+
amount: { currency: 'USD', value : '55.00' }
27+
}
28+
};
29+
30+
var options = {
31+
requestShipping: true,
32+
requestPayerEmail: true,
33+
requestPayerPhone: true,
34+
requestPayerName: true
35+
};
36+
37+
// Initialization
38+
var request = new PaymentRequest(supportedInstruments, details, options);
39+
40+
// When user selects a shipping address
41+
request.addEventListener('shippingaddresschange', e => {
42+
e.updateWith(((details, addr) => {
43+
var shippingOption = {
44+
id: '',
45+
label: '',
46+
amount: { currency: 'USD', value: '0.00' },
47+
selected: true
48+
};
49+
// Shipping to US is supported
50+
if (addr.country === 'US') {
51+
shippingOption.id = 'us';
52+
shippingOption.label = 'Standard shipping in US';
53+
shippingOption.amount.value = '0.00';
54+
details.total.amount.value = '55.00';
55+
// Shipping to JP is supported
56+
} else if (addr.country === 'JP') {
57+
shippingOption.id = 'jp';
58+
shippingOption.label = 'International shipping';
59+
shippingOption.amount.value = '10.00';
60+
details.total.amount.value = '65.00';
61+
// Shipping to elsewhere is unsupported
62+
} else {
63+
// Empty array indicates rejection of the address
64+
details.shippingOptions = [];
65+
return Promise.resolve(details);
66+
}
67+
// Hardcode for simplicity
68+
if (details.displayItems.length === 2) {
69+
details.displayItems[2] = shippingOption;
70+
} else {
71+
details.displayItems.push(shippingOption);
72+
}
73+
details.shippingOptions = [shippingOption];
74+
75+
return Promise.resolve(details);
76+
})(details, request.shippingAddress));
77+
});
78+
79+
// When user selects a shipping option
80+
request.addEventListener('shippingoptionchange', e => {
81+
e.updateWith(((details) => {
82+
// There should be only one option. Do nothing.
83+
return Promise.resolve(details);
84+
})(details));
85+
});
86+
87+
// Show UI then continue with user payment info
88+
request.show().then(result => {
89+
// POST the result to the server
90+
return fetch('/payment-api/pay', {
91+
method: 'POST',
92+
credentials: 'include',
93+
headers: {
94+
'Content-Type': 'application/json'
95+
},
96+
body: JSON.stringify(result.toJSON())
97+
}).then(res => {
98+
// Only if successful
99+
if (res.status === 200) {
100+
return res.json();
101+
} else {
102+
throw 'Failure';
103+
}
104+
}).then(response => {
105+
// You should have received a JSON object
106+
if (response.success == true) {
107+
return result.complete('success');
108+
} else {
109+
return result.complete('fail');
110+
}
111+
}).then(() => {
112+
console.log('Thank you!',
113+
result.shippingAddress.toJSON(),
114+
result.methodName,
115+
result.details.toJSON());
116+
}).catch(() => {
117+
return result.complete('fail');
118+
});
119+
}).catch(function(err) {
120+
console.error('Uh oh, something bad happened: ' + err.message);
121+
});
122+
}
123+
124+
// Assuming an anchor is the target for the event listener.
125+
document.querySelector('button').addEventListener('click', onBuyClicked);

0 commit comments

Comments
 (0)