我已经在我的网站上使用了 PayPal,因为它使用 php header 重定向来提交页面并将我重定向到 paypal 购物车。他们不支持 ajax,如果您使用 ajax 搜索 PayPal,您会发现看起来很便宜的购物车,但最终仍然会重定向到 paypal,这非常愚蠢。
另一方面,根据我的研究,Google 电子钱包不支持提交表单以打开 Google 电子钱包购物车页面的旧方法。
我想让它们都在没有 ajax 的情况下工作,因为我希望它们都使用相同的表单代码。
无论如何都可以在没有此 AJAX javascript 的情况下使用 Google 电子钱包 https://sandbox.google.com/checkout/inapp/lib/buy.js
我怎么能做这样的事情
<form method="POST" action="https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/123456789123456" accept-charset="utf-8">
上面的代码是 Google Checkout 在停用它并强制所有人改用 Google 电子钱包之前使用的代码。
我知道他们现在正试图让 AJAX 的一切正常工作,但他们必须对旧的表单方法有某种遗留支持,对吧?
我知道新的 Google 电子钱包要求您生成某种 token ,它是一种使用基于 sha256 哈希的消息身份验证代码包进行 Base64 编码的 token ,该 token 还具有您设置的到期日期。
https://code.google.com/p/iap-php/source/browse/#git%2Fiap-php%2Fsrc
Google 电子钱包示例代码:http://iap-php.appspot.com/
但我看不出有任何方法可以将其与网站重定向一起使用,我可能会继续尝试,如果我嗅探数据包我无法弄清楚它是否全部加密可能是 SSL。我也不明白 buy.js 是如何工作的,但我发现它使用了 https://sandbox.google.com/checkout,我可能会以某种方式将其制作成一种形式?我生成 token 等没有问题。因为我的表单首先重定向到我自己的脚本,该脚本设置重定向到 Paypal 购物车或谷歌结帐。
到目前为止,这是我获得代码的方式
<form method="POST" action="checkout.php" id="buyForm">
All Fields in here
submit button value="Paypal"
submit button value="Google"
</form>
我的 checkout.php 使用类似
<?php
if(isset($_POST['submit']) && $_POST['submit'] == "PayPal") {
//Generates all the variables for posting the redirect to PayPal
$query['cmd'] = '_xclick';
//Lots of other $query['stuff'] = 'otherstuff'; here
// Prepare query string
$query_string = http_build_query($query,'','&');
header('Location: https://www.paypal.com/cgi-bin/webscr?' . $query_string);
} else if(isset($_POST['submit']) && $_POST['submit'] == "Google") {
//Do the same code as for paypal... with header redirect (no ajax bs)
//I guessed the below of course it doesn't work
header('Location: https://sandbox.google.com/checkout?' . $query_string);
}
?>
最佳答案
我稍后会更新这个答案,仍在努力它似乎有效。
使用 jQuery 的解决方案
我无法使用 jquery .submit()
函数,因为该表单有 2 个按钮,一个必须使用 paypal 正常工作,另一个必须使用此 jquery 代码停止,该代码专注于按钮Google 电子钱包提交。
这是您放置在 HTML/PHP 页面上的 Google 电子钱包 API。
<script src="https://sandbox.google.com/checkout/inapp/lib/buy.js"></script>
//Prevents Button go to Checkout Google from going to checkout.php, because it's ajax
$('#buyNowGoogle').click(function (event) {
$("input[name='paymentMethod']").remove(); //so it doesn't use paypal by accident.
$(this).parent().append('<input type="hidden" name="paymentMethod" value="Google"/>');
$.ajax({
type: $("#buyForm").attr('method'),
url: $("#buyForm").attr('action'),
data: $("#buyForm").serialize(),
success: function (data) {
google.payments.inapp.buy({
jwt: data,
success: function () {
$.dialog("show", "Your Order was processed successfully!",
"Bought with Google Wallet");
},
failure: function (result) {
$.dialog("show", "Your Order was cancelled!", "Buying with Google Wallet");
ga('send', 'event', 'Google Fail', result.response.errorType);
}
});
},
error: function (x, t, m) {
ga('send', 'event', 'Google Wallet Error', x + ' ' + t + ' ' + m);
if (t === "timeout") {
$.dialog("show",
"You have timed out, try clicking Buy With Google Wallet again or check your internet, if this problem keeps happening contact us by email or msn at sales@highgamer.com",
"Timeout error while buying with Google Wallet");
} else {
$.dialog("show",
"You have encountered a Google Wallet error, check your internet, if this problem keeps happening contact us by email or msn at sales@highgamer.com",
m);
}
}
});
event.preventDefault(); //prevent form from getting submitted.
});
checkout.php 看起来像这样
<?php
if (isset($_POST['paymentMethod']) && $_POST['paymentMethod'] == "Google") {
//JWT class to encode/decode payload into JWT format. */
include_once "googlecheckout/JWT.php";
//Get payload of the product.
include_once "googlecheckout/payload.php";
$sellerIdentifier = SellerInfo::$issuerId;
$sellerSecretKey = SellerInfo::$secretKey;
$payload = new Payload();
$payload->SetIssuedAt(time());
$payload->SetExpiration(time()+3600);
$payload->AddProperty("name", "Piece of Cake");
$payload->AddProperty("description",
"Virtual chocolate cake to fill your virtual tummy");
$payload->AddProperty("price", "10.50");
$payload->AddProperty("currencyCode", "USD");
$payload->AddProperty("sellerData",
"user_id:1224245,offer_code:3098576987,affiliate:aksdfbovu9j");
// Creating payload of the product.
$Token = $payload->CreatePayload($sellerIdentifier);
// Encoding payload into JWT format.
$jwtToken = JWT::encode($Token, $sellerSecretKey);
echo $jwtToken;
} else if (isset($_POST['paymentMethod']) && $_POST['paymentMethod'] == "PayPal") {
//Do paypal code.
}
?>
checkout.php 输出为表单参数生成的 token ,我强制 Google 电子钱包 ajax 按钮调用自动显示 Google 电子钱包购物车。
关于php - 如何在没有 Ajax 的情况下使用 Google Wallet 而不是像 Paypal 一样提交表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23944170/