付款回调返回错误:json_decoding返回值后,应用程序返回值无效

时间:2012-10-29 21:39:35

标签: facebook json callback payment

我试图在我的Facebook应用中设置一个带有积分的购买。

FB.ui()功能正常,付款回拨网址和公司信息已设置完毕。我支付回调的PHP脚本就像facebook开发者的例子一样。

但是付费对话框不会显示确认对话框,FB.ui()回调会返回错误1383046和错误消息"抱歉,但我们在处理您的付款时遇到问题。您没有为此次交易付费。"

它发生在payments_get_items方法下,因此我无法测试提醒方法。

我已经阅读了很多关于相同错误代码的帖子,但不知道我的问题是否可能来自我未参加的其他细节

这是我的php脚本:

<?php

$app_secret = '***********************';

error_log($_POST['signed_request']);
// Validate request is from Facebook and parse contents for use.
$request = parse_signed_request($_POST['signed_request'], $app_secret);


// Get request type.

$request_type = $_POST['method'];

error_log($request_type, 0);

// Setup response.
$response = '';

if ($request_type == 'payments_get_items') {
// Get order info from Pay Dialog's order_info.
// Assumes order_info is a JSON encoded string.
 $order_info = json_decode($request['credits']['order_info'], true);

 // Get item id.
  $item_id = $order_info['item_id'];

  // Simulutates item lookup based on Pay Dialog's order_info.
  if ($item_id == 'real_item_id') {

$item = array(
  'title' => 'Title of my item',
  'description' => 'Description if my item',
  // Price must be denominated in credits.
  'price' => 5,
  'image_url' => 'my image url',
  'product_url' => 'my image url',
);

// Construct response.
$response = array(
           'content' => array (
               0 => $item,
               ),

            'method' => $request_type 
            );
// Response must be JSON encoded.

$response = json_encode($response);
error_log($response);
 }

} else if ($request_type == "payments_status_update") {
 // Get order details.
  $order_details = json_decode($request['credits']['order_details'], true);

  // Determine if this is an earned currency order.
 $item_data = json_decode($order_details['items'][0]['data'], true);
 $earned_currency_order = (isset($item_data['modified'])) ?
                         $item_data['modified'] : null;

 // Get order status.
  $current_order_status = $order_details['status'];

  if ($current_order_status == 'placed') {
    // Fulfill order based on $order_details unless...

    if ($earned_currency_order) {
     // Fulfill order based on the information below...
     // URL to the application's currency webpage.
     $product = $earned_currency_order['product'];
     // Title of the application currency webpage.
    $product_title = $earned_currency_order['product_title'];
    // Amount of application currency to deposit.
    $product_amount = $earned_currency_order['product_amount'];
    // If the order is settled, the developer will receive this
    // amount of credits as payment.
    $credits_amount = $earned_currency_order['credits_amount'];
   }

  $next_order_status = 'settled';

  // Construct response.
   $response = array(
              'content' => array(
                             'status' => $next_order_status,
                             'order_id' => $order_details['order_id']
                           ),
              'method' => $request_type
            );
// Response must be JSON encoded.
$response = json_encode($response);

} else if ($current_order_status == 'disputed') {
  // 1. Track disputed item orders.
  // 2. Investigate user's dispute and resolve by settling or refunding the order.
  // 3. Update the order status asychronously using Graph API.

  } else if ($current_order_status == 'refunded') {
// Track refunded item orders initiated by Facebook. No need to respond.

  } else if ($current_order_status == 'settled') {

  // Verify that the order ID corresponds to a purchase you've fulfilled, then…

  // Get order details.
   $order_details = json_decode($request['credits']['order_details'], true);

   // Construct response.
   $response = array(
              'content' => array(
                             'status' => 'settled',
                             'order_id' => $order_details['order_id']
                           ),
              'method' => $request_type
            );
  // Response must be JSON encoded.
  $response = json_encode($response);

 } else {
   // Track other order statuses.

 }
  }

// Send response.
 echo $response;

  // These methods are documented here:
  // https://developers.facebook.com/docs/authentication/signed_request/
   function parse_signed_request($signed_request, $secret) {
    list($encoded_sig, $payload) = explode('.', $signed_request, 2);

 // decode the data
 $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;
 }

 // check sig
 $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
 if ($sig !== $expected_sig) {
  error_log('Bad Signed JSON signature!');
   return null;
  }

 error_log( print_r($data, true) );
 return $data;
}

function base64_url_decode($input) {
 return base64_decode(strtr($input, '-_', '+/'));
 }

 ?>

查看error_log doc,调用FB.ui()函数时会调用两次回调(如果pay对话框没有收到响应,则会发生回调)。但是在每次调用时,作为var $ response发送的json对象都是正确的。

我的应用程序托管在Heroku上。 Payments Callback首先在亚马逊的付费服务器上运行。之后我创建了一个免费的网络托管帐户,在那里上传了脚本,并将付款回调网址指向它,但错误仍然存​​在。

我还需要考虑其他方面吗?

提前致谢!

注意:我在这篇文章中省略了网址和内容,但回调脚本有真实的信息;)

0 个答案:

没有答案