创建会话以便使用卡进行支付
创建一个 session
,以便使用我们的 Web SDK 来保护您客户的卡详情。
完整集成示例代码
Copied!
<section class="container">
<section class="card">
<form class="checkout" id="card-form">
<label class="label" for="card-pan">Card number <span class="type"></span></label>
<section id="card-pan" class="field"></section>
<section class="col-2">
<section class="col">
<label class="label" for="card-expiry">Expiry date</label>
<section id="card-expiry" class="field"></section>
</section>
<section class="col">
<label class="label" for="card-cvv">CVV</label>
<section id="card-cvv" class="field"></section>
</section>
</section>
<button class="submit" type="submit">Pay Now</button>
</form>
<button class="clear" id="clear">Clear</button>
</section>
</section>
<script src="https://try.access.worldpay.com/access-checkout/v1/checkout.js"></script>
// For production change to "https://access.worldpay.com/access-checkout/v1/checkout.js"
<section class="container"> <section class="card"> <form class="checkout" id="card-form"> <label class="label" for="card-pan">Card number <span class="type"></span></label> <section id="card-pan" class="field"></section> <section class="col-2"> <section class="col"> <label class="label" for="card-expiry">Expiry date</label> <section id="card-expiry" class="field"></section> </section> <section class="col"> <label class="label" for="card-cvv">CVV</label> <section id="card-cvv" class="field"></section> </section> </section> <button class="submit" type="submit">Pay Now</button> </form> <button class="clear" id="clear">Clear</button> </section> </section> <script src="https://try.access.worldpay.com/access-checkout/v1/checkout.js"></script> // For production change to "https://access.worldpay.com/access-checkout/v1/checkout.js"
body {
font: 11px/22px sans-serif;
text-transform: uppercase;
background-color: #f7f7f7;
color: black;
}
.container {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.card {
position: relative;
background: white;
padding: 40px 30px;
top: -30px;
width: 100%;
max-width: 300px;
border-radius: 12px;
box-shadow: 3px 3px 60px 0px rgba(0, 0, 0, 0.1);
}
.card .checkout .col-2 {
display: flex;
}
.card .checkout .col-2 .col:first-child {
margin-right: 15px;
}
.card .checkout .col-2 .col:last-child {
margin-left: 15px;
}
.card .checkout .label .type {
color: green;
}
.card .checkout.visa .label .type:before {
content: "(visa)";
}
.card .checkout.mastercard .label .type:before {
content: "(master card)";
}
.card .checkout.amex .label .type:before {
content: "(american express)";
}
.card .checkout .field {
height: 40px;
border-bottom: 1px solid lightgray;
}
.card .checkout .field#card-pan {
margin-bottom: 30px;
}
.card .checkout .field.is-onfocus {
border-color: black;
}
.card .checkout .field.is-empty {
border-color: orange;
}
.card .checkout .field.is-invalid {
border-color: red;
}
.card .checkout .field.is-valid {
border-color: green;
}
.card .checkout .submit {
background: red;
position: absolute;
cursor: pointer;
left: 50%;
bottom: -60px;
width: 200px;
margin-left: -100px;
color: white;
outline: 0;
font-size: 14px;
border: 0;
border-radius: 30px;
text-transform: uppercase;
font-weight: bold;
padding: 15px 0;
transition: background 0.3s ease;
}
.card .checkout.is-valid .submit {
background: green;
}
.clear {
background: grey;
position: absolute;
cursor: pointer;
left: 50%;
bottom: -120px;
width: 200px;
margin-left: -100px;
color: white;
outline: 0;
font-size: 14px;
border: 0;
border-radius: 30px;
text-transform: uppercase;
font-weight: bold;
padding: 15px 0;
transition: background 0.3s ease;
}
body { font: 11px/22px sans-serif; text-transform: uppercase; background-color: #f7f7f7; color: black; } .container { height: 100vh; display: flex; justify-content: center; align-items: center; } .card { position: relative; background: white; padding: 40px 30px; top: -30px; width: 100%; max-width: 300px; border-radius: 12px; box-shadow: 3px 3px 60px 0px rgba(0, 0, 0, 0.1); } .card .checkout .col-2 { display: flex; } .card .checkout .col-2 .col:first-child { margin-right: 15px; } .card .checkout .col-2 .col:last-child { margin-left: 15px; } .card .checkout .label .type { color: green; } .card .checkout.visa .label .type:before { content: "(visa)"; } .card .checkout.mastercard .label .type:before { content: "(master card)"; } .card .checkout.amex .label .type:before { content: "(american express)"; } .card .checkout .field { height: 40px; border-bottom: 1px solid lightgray; } .card .checkout .field#card-pan { margin-bottom: 30px; } .card .checkout .field.is-onfocus { border-color: black; } .card .checkout .field.is-empty { border-color: orange; } .card .checkout .field.is-invalid { border-color: red; } .card .checkout .field.is-valid { border-color: green; } .card .checkout .submit { background: red; position: absolute; cursor: pointer; left: 50%; bottom: -60px; width: 200px; margin-left: -100px; color: white; outline: 0; font-size: 14px; border: 0; border-radius: 30px; text-transform: uppercase; font-weight: bold; padding: 15px 0; transition: background 0.3s ease; } .card .checkout.is-valid .submit { background: green; } .clear { background: grey; position: absolute; cursor: pointer; left: 50%; bottom: -120px; width: 200px; margin-left: -100px; color: white; outline: 0; font-size: 14px; border: 0; border-radius: 30px; text-transform: uppercase; font-weight: bold; padding: 15px 0; transition: background 0.3s ease; }
(function () {
var form = document.getElementById("card-form");
var clear = document.getElementById("clear");
Worldpay.checkout.init(
{
id: "your-checkout-id",
form: "#card-form",
fields: {
pan: {
selector: "#card-pan",
placeholder: "4444333322221111"
},
cvv: {
selector: "#card-cvv",
placeholder: "123"
},
expiry: {
selector: "#card-expiry",
placeholder: "MM/YY"
}
},
styles: {
"input": {
"color": "black",
"font-weight": "bold",
"font-size": "20px",
"letter-spacing": "3px"
},
"input#pan": {
"font-size": "24px"
},
"input.is-valid": {
"color": "green"
},
"input.is-invalid": {
"color": "red"
},
"input.is-onfocus": {
"color": "black"
}
},
accessibility: {
ariaLabel: {
pan: "my custom aria label for pan input",
expiry: "my custom aria label for expiry input",
cvv: "my custom aria label for cvv input"
},
lang: {
locale: "en-GB"
},
title: {
enabled: true,
pan: "my custom title for pan",
expiry: "my custom title for expiry date",
cvv: "my custom title for security code"
}
},
acceptedCardBrands: ["amex", "diners", "discover", "jcb", "maestro", "mastercard", "visa"]
},
function (error, checkout) {
if (error) {
console.error(error);
return;
}
form.addEventListener("submit", function (event) {
event.preventDefault();
checkout.generateSessionState(function (error, sessionState) {
if (error) {
console.error(error);
return;
}
// session state for card details
alert(sessionState);
});
});
clear.addEventListener("click", function(event) {
event.preventDefault();
checkout.clearForm(function() {
// trigger desired behaviour on cleared form
console.log('Form successfully cleared');
});
});
}
);
})();
(function () { var form = document.getElementById("card-form"); var clear = document.getElementById("clear"); Worldpay.checkout.init( { id: "your-checkout-id", form: "#card-form", fields: { pan: { selector: "#card-pan", placeholder: "4444333322221111" }, cvv: { selector: "#card-cvv", placeholder: "123" }, expiry: { selector: "#card-expiry", placeholder: "MM/YY" } }, styles: { "input": { "color": "black", "font-weight": "bold", "font-size": "20px", "letter-spacing": "3px" }, "input#pan": { "font-size": "24px" }, "input.is-valid": { "color": "green" }, "input.is-invalid": { "color": "red" }, "input.is-onfocus": { "color": "black" } }, accessibility: { ariaLabel: { pan: "my custom aria label for pan input", expiry: "my custom aria label for expiry input", cvv: "my custom aria label for cvv input" }, lang: { locale: "en-GB" }, title: { enabled: true, pan: "my custom title for pan", expiry: "my custom title for expiry date", cvv: "my custom title for security code" } }, acceptedCardBrands: ["amex", "diners", "discover", "jcb", "maestro", "mastercard", "visa"] }, function (error, checkout) { if (error) { console.error(error); return; } form.addEventListener("submit", function (event) { event.preventDefault(); checkout.generateSessionState(function (error, sessionState) { if (error) { console.error(error); return; } // session state for card details alert(sessionState); }); }); clear.addEventListener("click", function(event) { event.preventDefault(); checkout.clearForm(function() { // trigger desired behaviour on cleared form console.log('Form successfully cleared'); }); }); } ); })();
创建您的结账表
创建表格时,您必须使用独特的标识符和字段配置。然后,您必须将默认高度应用于字段。
以下是您如何为字段配置设置独特标识符的示例:
HTML
Copied!
<form class="checkout" id="card-form">
<label class="label" for="card-pan">Card number <span class="type"></span></label>
<section id="card-pan" class="field"></section>
<section class="col-2">
<section class="col">
<label class="label" for="card-expiry">Expiry date</label>
<section id="card-expiry" class="field"></section>
</section>
<section class="col">
<label class="label" for="card-cvv">CVV</label>
<section id="card-cvv" class="field"></section>
</section>
</section>
<button class="submit" type="submit">Pay Now</button>
</form>
<form class="checkout" id="card-form"> <label class="label" for="card-pan">Card number <span class="type"></span></label> <section id="card-pan" class="field"></section> <section class="col-2"> <section class="col"> <label class="label" for="card-expiry">Expiry date</label> <section id="card-expiry" class="field"></section> </section> <section class="col"> <label class="label" for="card-cvv">CVV</label> <section id="card-cvv" class="field"></section> </section> </section> <button class="submit" type="submit">Pay Now</button> </form>
以及您如何设置结账页面布局的示例:
CSS
Copied!
.card .checkout .col-2 {
display: flex;
}
.card .checkout .col-2 .col:first-child {
margin-right: 15px;
}
.card .checkout .col-2 .col:last-child {
margin-left: 15px;
}
.card .checkout .field {
height: 40px;
border-bottom: 1px solid lightgray;
}
.card .checkout .field#card-pan {
margin-bottom: 30px;
}
.card .checkout .col-2 { display: flex; } .card .checkout .col-2 .col:first-child { margin-right: 15px; } .card .checkout .col-2 .col:last-child { margin-left: 15px; } .card .checkout .field { height: 40px; border-bottom: 1px solid lightgray; } .card .checkout .field#card-pan { margin-bottom: 30px; }
初始化 SDK
一旦包含了用于获取 SDK 的
请使用 Worldpay.checkout.init
方法来进行。
在初始化 SDK 时,您必须提供 AccessCheckoutIdentity
、独特表格标识符以及字段配置。
可选配置
您还可以提供您的:
示例 - 初始化 SDK(必需参数/配置)
JavaScript
Copied!
(function () {
var form = document.getElementById("card-form");
var clear = document.getElementById("clear");
Worldpay.checkout.init(
{
id: "your-checkout-id",
form: "#card-form",
fields: {
pan: {
selector: "#card-pan",
placeholder: "4444333322221111"
},
cvv: {
selector: "#card-cvv",
placeholder: "123"
},
expiry: {
selector: "#card-expiry",
placeholder: "MM/YY"
}
},
styles: {
"input": {
"color": "black",
"font-weight": "bold",
"font-size": "20px",
"letter-spacing": "3px"
},
"input#pan": {
"font-size": "24px"
},
"input.is-valid": {
"color": "green"
},
"input.is-invalid": {
"color": "red"
},
"input.is-onfocus": {
"color": "black"
}
},
accessibility: {
ariaLabel: {
pan: "my custom aria label for pan input",
expiry: "my custom aria label for expiry input",
cvv: "my custom aria label for cvv input"
},
lang: {
locale: "en-GB"
},
title: {
enabled: true,
pan: "my custom title for pan",
expiry: "my custom title for expiry date",
cvv: "my custom title for security code"
}
},
acceptedCardBrands: ["amex", "diners", "discover", "jcb", "maestro", "mastercard", "visa"]
},
function (error, checkout) {
if (error) {
console.error(error);
return;
}
form.addEventListener("submit", function (event) {
event.preventDefault();
checkout.generateSessionState(function (error, sessionState) {
if (error) {
console.error(error);
return;
}
// session state for card details
alert(sessionState);
});
});
clear.addEventListener("click", function(event) {
event.preventDefault();
checkout.clearForm(function() {
// trigger desired behaviour on cleared form
console.log('Form successfully cleared');
});
});
}
);
})();
(function () { var form = document.getElementById("card-form"); var clear = document.getElementById("clear"); Worldpay.checkout.init( { id: "your-checkout-id", form: "#card-form", fields: { pan: { selector: "#card-pan", placeholder: "4444333322221111" }, cvv: { selector: "#card-cvv", placeholder: "123" }, expiry: { selector: "#card-expiry", placeholder: "MM/YY" } }, styles: { "input": { "color": "black", "font-weight": "bold", "font-size": "20px", "letter-spacing": "3px" }, "input#pan": { "font-size": "24px" }, "input.is-valid": { "color": "green" }, "input.is-invalid": { "color": "red" }, "input.is-onfocus": { "color": "black" } }, accessibility: { ariaLabel: { pan: "my custom aria label for pan input", expiry: "my custom aria label for expiry input", cvv: "my custom aria label for cvv input" }, lang: { locale: "en-GB" }, title: { enabled: true, pan: "my custom title for pan", expiry: "my custom title for expiry date", cvv: "my custom title for security code" } }, acceptedCardBrands: ["amex", "diners", "discover", "jcb", "maestro", "mastercard", "visa"] }, function (error, checkout) { if (error) { console.error(error); return; } form.addEventListener("submit", function (event) { event.preventDefault(); checkout.generateSessionState(function (error, sessionState) { if (error) { console.error(error); return; } // session state for card details alert(sessionState); }); }); clear.addEventListener("click", function(event) { event.preventDefault(); checkout.clearForm(function() { // trigger desired behaviour on cleared form console.log('Form successfully cleared'); }); }); } ); })();
参数 | 必需 | 描述 |
---|---|---|
id | 您的 Access Checkout 身份。 e.g. f7a25ad0-0224-46c2-9102-2229aaadbfec | |
form | 您的独特表格编号选择器。 | |
styles | 您可选的样式对象配置。 | |
fields | 包含您的字段配置的对象。 | |
selector | 给定字段的编号选择器。 | |
placeholder | 为客户显示应在字段中输入的信息类型的可选文本预览。 | |
accessibility | 您可选的可访问性对象配置。 | |
cardBrands | 您希望接受的卡品牌的可选阵列。 | |
enablePanFormatting | 若调用一个可选参数,即可启用 PAN 格式设置行为。 |
在上述 JavaScript 示例中,会返回 sessionState
并包含您的客户卡详情的加密有效负载。
更多信息:单击sessionState
的更多信息。
使用您发送给session
来接收 Token,您可以将其用于
重要信息:session
具有 1 分钟的使用寿命,并且您只能使用一次。如果未在该时间内创建 Token,您就必须创建新的 session
。
清除表格
SDK 还可揭示允许重置 SDK 的 clearForm
方法。无需重新加载 SDK 即可将所有字段和类别重置回其原始状态。
clearForm
方法可接受在整个表格被成功清除时调用的回调。
客户行为可能通常会触发此操作。例如:
- 单击清除表格按钮
- 成功接收会话
这样可让客户在无需重新加载 SDK 的情况下输入多组卡详情。
移除 SDK
您可采用remove
方法,从页面中移除 SDK。利用此法可移除由 SDK 添加到此页面中的所有字段。
浏览器支持
- 小版本 1.7.0 不支持 Firefox、Edge Legacy 和 Chrome >= 91
- 小版本 1.7.1 不支持 Edge Legacy 和 Chrome >= 91
- 小版本 1.7.2 不支持 Chrome >= 91(问题已在 1.7.3 中解决)
我们在所有其他版本上支持主流浏览器的所有最新版本。
后续步骤