Javascript 如何比较谷歌钱包中的两个OrderID?
我正在用PHP和HTML/JavaScript在一个网站上实现Google Wallet for Digital Goods。我在沙箱环境中成功实现了钱包交易。到目前为止,一切顺利 现在,为了确保购买是一个安全的交易,我想检查successHandler中返回的orderId是否等于通过服务器上我的回发文件中的Google帖子收到的orderId 我对PHP有一定的了解,对Javascript了解较少。在研究了StackOverflow中的所有Google Wallet条目并反复阅读Google Wallet商家设置页面后,我仍然无法找出正确的代码来比较successHandler中的orderId和回发文件中的orderId。在我看来,successHandler的orderId(我猜可以写成Javascript 如何比较谷歌钱包中的两个OrderID?,javascript,php,android-pay,Javascript,Php,Android Pay,我正在用PHP和HTML/JavaScript在一个网站上实现Google Wallet for Digital Goods。我在沙箱环境中成功实现了钱包交易。到目前为止,一切顺利 现在,为了确保购买是一个安全的交易,我想检查successHandler中返回的orderId是否等于通过服务器上我的回发文件中的Google帖子收到的orderId 我对PHP有一定的了解,对Javascript了解较少。在研究了StackOverflow中的所有Google Wallet条目并反复阅读Google
result.response.orderId
,我在其他StackOverflow答案中看到过),是用Javascript定义的,而回发文件中的orderId(我在这里称之为回发orderId
)是用PHP定义的
- 你如何比较这两者?一个是Javascript变量,另一个是PHP变量
- 我想比较这些变量的最佳位置是successHandler函数。但是如何将一个PHP变量从回发文件(在该文件中称为
)获取到另一个文件中使用的Javascript函数中呢$orderId
<?php
include ('sessionstart.inc');
require_once 'generate_token.php';
echo "
<!DOCTYPE html>
<html>
<head><meta name='viewport' content='width=device-width, initial-scale=1,maximum-scale=1'>
<title>Digital Goods Application</title>
<script src='https://sandbox.google.com/checkout/inapp/lib/buy.js'></script>
<script type='text/javascript'>
//Success handler
var successHandler = function(result){
if (result.reponse.orderId == postback-orderId) {
if (window.console != undefined) {
console.log('Purchase completed successfully.');
}
}
}
//Failure handler
var failureHandler = function(result){
if (window.console != undefined) {
console.log('Purchase did not complete.');
}
}
function purchase(jwt_value) {
runDemoButton = document.getElementById('runDemoButton')
google.payments.inapp.buy({
'jwt': jwt_value,
'success': successHandler,
'failure': failureHandler
});
return false;
}
</script>
</head>
<body>
<div>
<p>Buy 5 Search Credits to continue searching The Clock Register.</p><br />
<button id='runDemoButton' value='buy' class='buttons' onclick='purchase(\"$jwtToken\");'><b>Buy</b></button>
</div>
</body>
</html>";
?>
我不是一名PHP开发人员,因此这更像是“概念性的”,而不是具体的-
总体思路是,您将以某种方式保存从(服务器端)POST
从Google(也称为“PHP变量”)收到的数据,例如数据库
然后,您可以选择下一步要做什么(进行比较)-
- 在执行x之前,可能需要在成功处理程序中对服务器(获取持久化数据)进行ajax调用,以验证/比较“javascript变量/orderId”和“Php变量/orderId”
- 也许在成功处理程序中重定向并将“javascript orderId”作为某个值(查询字符串、POST数据等)发送给执行上述验证/比较的目标(从服务器/db),然后执行x
因此,它是关于保存数据以供以后验证的
嗯
更新(以上免责声明适用):)
我在回发文件中将$orderId设置为会话,如下所示:$\u会话['orderId']=$orderId;但是,当我检查$\u SESSION['orderid']的值时,它是空的
如果我理解正确,您正在从Google的服务器端回发设置一个会话变量
,这意味着请求/会话没有“绑定”到浏览器/客户端。这是一个服务器端的帖子——支付过程发生的浏览器并不知道——它根本没有参与回发(在这种情况下,“客户端”是一些谷歌机器)
换句话说,浏览器不知道(单独的/服务器端的)“会话”,它既没有“sessionID”(cookie),也没有匹配的“sessionID”(记住,当您设置会话变量时,是“某个Google机器”发出了请求)
Hth….您必须记住,页面的呈现(以及购买操作)发生在服务器发送/接收回发之前很久,因此,页面中不能有回发医嘱ID。您必须执行以下操作:
回发url目标文件/脚本应记录服务器上接收的数据(在文件或数据库中)
您需要在服务器上编写另一个php文件/脚本,该文件/脚本可以从上面1中的日志中检索数据,并进行比较以检查successHandler中的orderID是否已被记录
成功处理程序必须调用上面2中的脚本,至少使用orderID作为参数。(不要担心ajax调用,它只是javascript)
这看起来可能很复杂,很长,但可以用不到20行代码(php和javascript/ajax)实现
编辑
1.更新了postback.php以包含日志记录
<?php
require_once 'JWT.php';
$encoded_jwt = $_POST['jwt'];
$decodedJWT = JWT::decode($encoded_jwt, $sellerSecret);
$orderId = $decodedJWT->response->orderId;
header("HTTP/1.0 200 OK");
echo $orderId;
$logfile = "logfile.txt";
if (!file_exists($logfile)) {
touch($logfile);
chmod($logfile, 0777);
}
$orderIDS = file($logfile); //reads file into array
$orderIDS[] = $orderId; //append new orderID to array
file_put_contents($logfile, $orderIDS); //save the new array to file
?>
另外,在处理程序中,确保您拥有指向IDSearch.php的web资源的绝对路径@EdSF:I将回发文件中的$orderId
设置为会话,如下所示:$\u会话['orderId']=$orderId但是,当我检查$\u SESSION['orderid']的值时,它是空的。我不明白,因为我可以看到$orderId有一个值(我通过电子邮件从回发文件中向自己发送了$orderId的值)。您建议先将$orderId写入数据库。我同意这应该是可行的(还没有尝试过),但是我很困惑为什么我不能将它写入一个会话变量中,而这个变量应该更快,更少地与数据库进行交互。不过,你的建议对我帮助很大!你完全正确。我与SESSIONs合作的想法毫无意义。。。感谢上帝,你能让我重回正轨。非常感谢你的解释。我现在正努力按照你和EdSF的建议去做。如果我陷入困境,我希望我可以向您展示我所做的事情以及我陷入困境的地方。@user2187593-我已经更新了答案,并给出了一些如何实现这一点的代码示例。
<?php
require_once 'JWT.php';
$encoded_jwt = $_POST['jwt'];
$decodedJWT = JWT::decode($encoded_jwt, $sellerSecret);
$orderId = $decodedJWT->response->orderId;
header("HTTP/1.0 200 OK");
echo $orderId;
$logfile = "logfile.txt";
if (!file_exists($logfile)) {
touch($logfile);
chmod($logfile, 0777);
}
$orderIDS = file($logfile); //reads file into array
$orderIDS[] = $orderId; //append new orderID to array
file_put_contents($logfile, $orderIDS); //save the new array to file
?>
<?php
$orderSearch = $_GET['orderID'];
$logfile = "logfile.txt";
if (file_exists($logfile)) {
$orderIDS = file($logfile); //reads file into array
echo (in_array($orderSearch, $orderIDS)); // either true or false is returned
} else {
echo 'Error';
}
?>
var successHandler = function (result) {
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (window.console != undefined) {
console.log(xmlhttp.responseText);
}
}
}
xmlhttp.open("GET", "IDSearch.php?orderID=" + result.reponse.orderId, true);
xmlhttp.send();
}