Jos*_*ere 4 php square square-connect
我可以按照github上的示例使用事务API成功收费.执行收费如下:
$result = $transaction_api->charge($access_token, $location_id, $request_body);
echo "<pre>";
print_r($result);
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)
这是输出:
SquareConnect\Model\ChargeResponse Object
(
[errors:protected] =>
[transaction:protected] => SquareConnect\Model\Transaction Object
(
[id:protected] => REMOVED FROM POST
[location_id:protected] => REMOVED FROM POST
[created_at:protected] => 2016-04-30T23:42:33Z
[tenders:protected] => Array
(
[0] => SquareConnect\Model\Tender Object
(
[id:protected] => REMOVED FROM POST
[location_id:protected] => REMOVED FROM POST
[transaction_id:protected] => 02d1d965-51fd-5023-68f5-0fcd148a263b
[created_at:protected] => 2016-04-30T23:42:33Z
[note:protected] => Online Transaction
[amount_money:protected] => SquareConnect\Model\Money Object
(
[amount:protected] => 6000
[currency:protected] => USD
)
[processing_fee_money:protected] =>
[customer_id:protected] =>
[type:protected] => CARD
[card_details:protected] => SquareConnect\Model\TenderCardDetails Object
(
[status:protected] => CAPTURED
[card:protected] => SquareConnect\Model\Card Object
(
[id:protected] =>
[card_brand:protected] => VISA
[last_4:protected] => 5858
[exp_month:protected] =>
[exp_year:protected] =>
[cardholder_name:protected] =>
[billing_address:protected] =>
)
[entry_method:protected] => KEYED
)
[cash_details:protected] =>
)
)
[refunds:protected] =>
[reference_id:protected] =>
[product:protected] => EXTERNAL_API
)
)
Run Code Online (Sandbox Code Playgroud)
我的问题是,虽然有些地方(比如这里)表明我应该从充电方法中取回一个数组,但我得到一个ChargeResponse对象.
在此对象中是一个事务对象,其中包含我希望在事务完成后显示给客户的所有相关信息,但它受到保护,因此尝试从此返回的对象中回显事务id,created_at时间或金额将失败.
我确定我做错了什么,但我迷失了如何从ChargeResponse对象捕获属性,以便我可以用它做有用的事情.
例如,我试过了
echo($result->transaction['id']);
Run Code Online (Sandbox Code Playgroud)
但我得到的是:
致命错误:无法访问受保护的财产
这甚至可能不是尝试这样的事情的正确方法,所以我完全乐于接受建议.
我设法弄清楚必须使用包含在对象中的getTransaction方法来获取可用的属性形式.
$transaction = $result->getTransaction();
Run Code Online (Sandbox Code Playgroud)
然后你可以得到你想要的房产:
$transactionID = $transaction["tenders"][0]["transaction_id"];
Run Code Online (Sandbox Code Playgroud)
我很生气,因为我没有在文档中的任何地方遇到过这个问题(事实上,对整个docs.connect.squareup.com的谷歌搜索没有找到对getTransaction的单一引用).当我尝试使用其他一些黑客工作将原始ChargeResponse对象重新解析为数组时,我不得不偶然发现它.
无论如何,很高兴这个解决了.想把这个留给别人.
| 归档时间: |
|
| 查看次数: |
1032 次 |
| 最近记录: |