在一个文字游戏在Facebook上作为Canvas应用程序托管我想卖一个消耗品“1年VIP身份”,让玩家临时访问某些区域游戏 - 通过使用Facebook Payments Lite (serverless)。付款精简版(无服务器):第一次购买的作品,但第二次总是失败
我的JavaScript代码显示收费对话框然后通过signed_request到我的PHP脚本 -
在我的画布应用程序的JavaScript代码:
function buyVip() {
var obj = {
method: "pay",
action: "purchaseiap",
product_id: "test1"
};
FB.ui(obj, function(data) {
$.post("/payment-lite.php",
{ signed_request: data.signed_request })
.done(function(data) {
location.reload();
});
});
}
我的PHP脚本/支付,精简版.php:
const APP_SECRET = 'XXXXXXX';
$request = parse_signed_request($_POST['signed_request'], APP_SECRET);
error_log(print_r($request, TRUE));
// TODO validate $request and set the user VIP status in the game database
function parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), TRUE);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
error_log('Unknown algorithm. Expected HMAC-SHA256');
return NULL;
}
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = TRUE);
if ($sig !== $expected_sig) {
error_log('Bad Signed JSON signature!');
return NULL;
}
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
在应用仪表盘 - >网络付款我添加了一个测试用户和测试产品,“产品ID” test1
和欧元0.01的价格:
最后我登录为测试用户和按在应用程序调用buyVip
方法的按钮 - 导致收费对话框出现:
然后在服务器日志的I看到payment.php
脚本成功地叫:
[30-Jul-2017 14:34:20 Europe/Berlin] Array
(
[algorithm] => HMAC-SHA256
[amount] => 0.01
[app_id] => 376218039240910
[currency] => EUR
[issued_at] => 1501418059
[payment_id] => 1084810821649513
[product_id] => test1
[purchase_time] => 1501418057
[purchase_token] => 498440660497153
[quantity] => 1
[status] => completed
)
然而,当我尝试后,同样的程序,收费对话框出现,但随后的错误
按 购买按钮后失败There Was a Problem Processing Your Payment: Sorry, but we're having trouble processing your payment. You have not been charged for this transaction. Please try again.
并在浏览器控制台我看1383001 Unknown错误代码:
{error_code: 1383001, error_message: "There Was a Problem Processing Your Payment: Sorry…n charged for this transaction. Please try again."}
请问这是什么意思,为什么先买请求成功了,但后来失败了?
在我的应用程序中,我当然会在成功购买后隐藏“购买VIP状态”按钮一年,但我仍然想知道,这里发生了什么。
同样在未来,我想在我的游戏中销售像“硬币”这样的消费品虚拟商品,然后多次购买应该成功。
UPDATE:
我试图consume购买加入下面的代码我payment.php
(使用APP_ID|APP_SECRET而不是要求用户访问令牌)到:
$post = [
'access_token' => APP_ID . '|' . APP_SECRET,
];
$ch = curl_init('https://graph.facebook.com/498440660497153/consume');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close($ch);
error_log(print_r($response, TRUE));
但不幸的是获得错误:
{"error":{"message":"Unsupported post request. Object with ID '498440660497153' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api","type":"GraphMethodException","code":100,"fbtrace_id":"HDusTBubydJ"}}
谢谢阿列克谢!有没有办法在我的payment.php脚本中使用服务器端**上的购买**?请参阅我的问题中的UPDATE。作为解决方法,我是否应该将用户访问令牌作为'developer_payload'传递给应用程序? –
只需将access_token传递给脚本即可。查看我的答案更新。 –