将 sessionId javascript 函数调用到另一个函数中
我有一个用于万事达卡“托管结帐集成”结帐集成的 html 页面,但我的 sessionId 有问题 我如何将 javascript 函数的结果调用到另一个 javascript 函数中 我想从这个函数“sessionId”调用
function getSessionID(){
axios({
method: 'POST',
url: 'https://banquemisr.gateway.mastercard.com/api/rest/version/61/merchant/xxxxx_xxx/session',
data: JSON.stringify({
apiOperation: 'CREATE_CHECKOUT_SESSION',
interaction: {
operation : "PURCHASE",
},
order : {
amount: function() {
//Dynamic calculation of amount
return realAmount + commission;
},
currency: "EGP",
description : "Ordered goods",
id: idValue,
},
}),
auth: {
username: "merchant.xxxx_xxx",
password: "xxxxxxxxxxxxxxxxxxxxxxxxx"
},
crossOrigin: null,
}).then(function (response) {
console.log('Authenticated');
localStorage.setItem('sessionId',response.data.session.id)
});
这个函数,
Checkout.configure({
session: {
id : sessionId;
},
interaction: {
operation : "PURCHASE",
merchant: {
name: "xxxxx_xxx",
address: {
line1: clientAddress1,
line2: clientAddress2
}
}
}
});
我尝试通过
return getSessionID()
但不是单词来获取它,任何想法
我的完整代码页:
<!doctype html>
<html>
<head>
<title>xxxxx</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://banquemisr.gateway.mastercard.com/checkout/version/61/checkout.js" data-error="errorCallback" data-cancel="cancelCallback"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/javascript">
/* ############################# Config */
function session_id() {
return /SESS\w*ID=([^;]+)/i.test(document.cookie) ? RegExp.$1 : false;
}
//Type Your commission that will be added to the real amount
let commission = 0;
//Start amount
let realAmount = 0;
//currency
let currency = "EGP";
//Goods Item name and details
let itemDescription = "xxxxx";
//Store Name
let clietnName = document.title;//"Store Name";
//Store address 1 & 2
let clientAddress1 = "200 Sample St";
let clientAddress2 = "1234 Example Townt";
//cancel page redirect
let cancelURL = 'https://google.com';
//payment id
let idValue = Math.random().toString(36).substr(2, 9);
let str = "merchant.xxxxx:xxxxx";
let enc = window.btoa(str);
console.log(enc);
getSessionID();
/* ########################### End config */
function errorCallback(error) {
console.log(JSON.stringify(error));
alert(JSON.stringify(error));
}
function cancelCallback() {
console.log('Payment cancelled');
window.href = cancelURL;
}
Checkout.configure({
session: {
id : return getSessionID();
},
interaction: {
operation : "PURCHASE",
merchant: {
name: "xxxxx",
address: {
line1: clientAddress1,
line2: clientAddress2
}
}
}
});
async function getSessionID() {
const response = await axios({
method: "POST",
url: "https://banquemisr.gateway.mastercard.com/api/rest/version/61/merchant/xxxxx/session",
data: JSON.stringify({
apiOperation: "CREATE_CHECKOUT_SESSION",
interaction: {
operation: "PURCHASE",
},
order: {
amount: function () {
//Dynamic calculation of amount
return realAmount + commission;
},
currency: "EGP",
description: "Ordered goods",
id: idValue,
},
}),
auth: {
username: "merchant.xxxxx",
password: "xxxxx",
},
crossOrigin: null,
});
return response.data.session.id;
}
async function foo(){
const sessionID = await getSessionID();
}
function makePayment()
{
realAmount = parseFloat(document.getElementById('amount').value);
document.getElementById('amount').disabled = true;
document.getElementById('pay').disabled = true;
document.getElementById('cancel').disabled = true;
Checkout.showPaymentPage();
return false;
}
</script>
</head>
<style>
.card-header .icons .fa-cc-discover{
color: #027878;
}
.card-header .icons .fa-cc-amex{
color: #ef6903;
}
label.labelamount{
color: #ef6903;
}
.card-body label{
font-size: 14px;
}
.submitpay{
color: #fff;
background-color: #ef6903;
width: 83%;
margin: auto;
border: 2px solid #ef6903;
border-radius: 5px;
}
.icons{
text-align: right;
}
.inputamout{
width: 85%;
}
body{
background: url("http://www.rimallytravel.com/wp-content/themes/altair/bg.jpg") no-repeat fixed center;
}
.imgbq{
margin: auto;
}
.imglogo{
width: 100%;
}
</style>
<body class="bg-danger pt-5">
<div class="container">
<div class="row">
<div class="col-md-4 offset-md-4 col-10 offset-1 pl-0 pr-0">
<div class="card1">
<img class="imglogo" src="http://www.rimallytravel.com/wp-content/uploads/2017/10/ww.png" alt="logo" >
<br /><br /><br />
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 offset-md-4 col-10 offset-1 pl-0 pr-0">
<div class="card">
<div class="card-header">
<div class="row">
<div class="col-md-6 col-12 pt-2">
<h6 class="m-0"><strong>Payment Details</strong></h6>
</div>
<div class="col-md-6 col-12 icons">
<img class="imgcredit" src="http://www.rimallytravel.com/wp-content/uploads/2017/10/Credit-Card-Visa-And-Master-Card-PNG-File-300x65.png" alt="banq" width="170" >
</div>
</div>
</div>
<div class="card-body">
<form onsubmit="return makePayment()">
<div class="col-md-12 col-12">
<div class="form-group">
<label class="labelamount" for="exampleInputamount"><strong>Amount</strong></label>
</br>
<input class="inputamout" type="number" min="1" step="0.01" id="amount" style="text-align: center;" required> EGP
</div>
</div>
</div>
<input class="submitpay" type="submit" id="pay" value="Pay Now">
<br />
<img class="imgbq" src="http://www.rimallytravel.com/wp-content/themes/altair/bq.png" alt="banq" width="125" >
<br />
<input type="hidden" id="cancel" onclick="cancelCallback()" value="Cancel" >
</form>
</div>
</div>
</div>
</div>
</div>
<script>
document.getElementById('amount').value = realAmount;
</script>
</body>
</html>
i have a html page for Checkout Integration of mastercard "Hosted Checkout Integration" , but i have a Problem with sessionId
how can i call result of javascript function into another javascript function
i want call from this function "sessionId"
function getSessionID(){
axios({
method: 'POST',
url: 'https://banquemisr.gateway.mastercard.com/api/rest/version/61/merchant/xxxxx_xxx/session',
data: JSON.stringify({
apiOperation: 'CREATE_CHECKOUT_SESSION',
interaction: {
operation : "PURCHASE",
},
order : {
amount: function() {
//Dynamic calculation of amount
return realAmount + commission;
},
currency: "EGP",
description : "Ordered goods",
id: idValue,
},
}),
auth: {
username: "merchant.xxxx_xxx",
password: "xxxxxxxxxxxxxxxxxxxxxxxxx"
},
crossOrigin: null,
}).then(function (response) {
console.log('Authenticated');
localStorage.setItem('sessionId',response.data.session.id)
});
into this function
Checkout.configure({
session: {
id : sessionId;
},
interaction: {
operation : "PURCHASE",
merchant: {
name: "xxxxx_xxx",
address: {
line1: clientAddress1,
line2: clientAddress2
}
}
}
});
i try to get it by
return getSessionID()
but not words , any Thoughts
my full code page :
<!doctype html>
<html>
<head>
<title>xxxxx</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://banquemisr.gateway.mastercard.com/checkout/version/61/checkout.js" data-error="errorCallback" data-cancel="cancelCallback"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/javascript">
/* ############################# Config */
function session_id() {
return /SESS\w*ID=([^;]+)/i.test(document.cookie) ? RegExp.$1 : false;
}
//Type Your commission that will be added to the real amount
let commission = 0;
//Start amount
let realAmount = 0;
//currency
let currency = "EGP";
//Goods Item name and details
let itemDescription = "xxxxx";
//Store Name
let clietnName = document.title;//"Store Name";
//Store address 1 & 2
let clientAddress1 = "200 Sample St";
let clientAddress2 = "1234 Example Townt";
//cancel page redirect
let cancelURL = 'https://google.com';
//payment id
let idValue = Math.random().toString(36).substr(2, 9);
let str = "merchant.xxxxx:xxxxx";
let enc = window.btoa(str);
console.log(enc);
getSessionID();
/* ########################### End config */
function errorCallback(error) {
console.log(JSON.stringify(error));
alert(JSON.stringify(error));
}
function cancelCallback() {
console.log('Payment cancelled');
window.href = cancelURL;
}
Checkout.configure({
session: {
id : return getSessionID();
},
interaction: {
operation : "PURCHASE",
merchant: {
name: "xxxxx",
address: {
line1: clientAddress1,
line2: clientAddress2
}
}
}
});
async function getSessionID() {
const response = await axios({
method: "POST",
url: "https://banquemisr.gateway.mastercard.com/api/rest/version/61/merchant/xxxxx/session",
data: JSON.stringify({
apiOperation: "CREATE_CHECKOUT_SESSION",
interaction: {
operation: "PURCHASE",
},
order: {
amount: function () {
//Dynamic calculation of amount
return realAmount + commission;
},
currency: "EGP",
description: "Ordered goods",
id: idValue,
},
}),
auth: {
username: "merchant.xxxxx",
password: "xxxxx",
},
crossOrigin: null,
});
return response.data.session.id;
}
async function foo(){
const sessionID = await getSessionID();
}
function makePayment()
{
realAmount = parseFloat(document.getElementById('amount').value);
document.getElementById('amount').disabled = true;
document.getElementById('pay').disabled = true;
document.getElementById('cancel').disabled = true;
Checkout.showPaymentPage();
return false;
}
</script>
</head>
<style>
.card-header .icons .fa-cc-discover{
color: #027878;
}
.card-header .icons .fa-cc-amex{
color: #ef6903;
}
label.labelamount{
color: #ef6903;
}
.card-body label{
font-size: 14px;
}
.submitpay{
color: #fff;
background-color: #ef6903;
width: 83%;
margin: auto;
border: 2px solid #ef6903;
border-radius: 5px;
}
.icons{
text-align: right;
}
.inputamout{
width: 85%;
}
body{
background: url("http://www.rimallytravel.com/wp-content/themes/altair/bg.jpg") no-repeat fixed center;
}
.imgbq{
margin: auto;
}
.imglogo{
width: 100%;
}
</style>
<body class="bg-danger pt-5">
<div class="container">
<div class="row">
<div class="col-md-4 offset-md-4 col-10 offset-1 pl-0 pr-0">
<div class="card1">
<img class="imglogo" src="http://www.rimallytravel.com/wp-content/uploads/2017/10/ww.png" alt="logo" >
<br /><br /><br />
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 offset-md-4 col-10 offset-1 pl-0 pr-0">
<div class="card">
<div class="card-header">
<div class="row">
<div class="col-md-6 col-12 pt-2">
<h6 class="m-0"><strong>Payment Details</strong></h6>
</div>
<div class="col-md-6 col-12 icons">
<img class="imgcredit" src="http://www.rimallytravel.com/wp-content/uploads/2017/10/Credit-Card-Visa-And-Master-Card-PNG-File-300x65.png" alt="banq" width="170" >
</div>
</div>
</div>
<div class="card-body">
<form onsubmit="return makePayment()">
<div class="col-md-12 col-12">
<div class="form-group">
<label class="labelamount" for="exampleInputamount"><strong>Amount</strong></label>
</br>
<input class="inputamout" type="number" min="1" step="0.01" id="amount" style="text-align: center;" required> EGP
</div>
</div>
</div>
<input class="submitpay" type="submit" id="pay" value="Pay Now">
<br />
<img class="imgbq" src="http://www.rimallytravel.com/wp-content/themes/altair/bq.png" alt="banq" width="125" >
<br />
<input type="hidden" id="cancel" onclick="cancelCallback()" value="Cancel" >
</form>
</div>
</div>
</div>
</div>
</div>
<script>
document.getElementById('amount').value = realAmount;
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用 async-await,就像您当前的
getSessionID()
所做的那样不返回任何值。试试这个:
You need to use async-await, as your current
getSessionID()
does not return any value.Try this:
您可以按照@agrawal-d的建议在
getSessionId
中使用async/await
,但如果您正确返回axios wait,它也会返回一个承诺。但是,该函数不再是异步的,因此您需要适当地调用它。
试试这个
You can use
async/await
ingetSessionId
as suggested by @agrawal-d but if you return axios right await it also returns a promise.However, that function is not async anymore so u need to call it appropriately.
Try this