json_decode()在WHMCS回调文件的Payment Gateway插件中不起作用

时间:2018-01-08 20:55:28

标签: php json payment-gateway whmcs

我在使用whmcs时遇到了一个有趣的挑战。

我正在编写一个插件,将支付网关集成到whmcs安装中。

我已完成插件的第一部分,即添加付款按钮,触发付款网关模式并方便付款。

付款成功或失败后,支付网关会使用看起来像这样的get参数重定向回whmcs回调网址。

https://www.domainname.com/whmcs/modules/gateways/callback/gateway.php?resp=urlencoded-json-string

来自支付网关的确切样本回复 https://www.domainname.com/whmcs/modules/gateways/callback/gateway.php?resp=%7B%22name%22%3A%22vbvcomplete%22%2C%22data%22%3A%7B%22data%22%3A%7B%22status%22%3A%22successful%22%2C%22txRef%22%3A%22316_1514108957%22%2C%22amount%22%3A%222%22%7D%7D%2C%22respcode%22%3A%2200%22%2C%22tx%22%3A%7B%22id%22%3A61282%2C%22txRef%22%3A%22316_1514108957%22%2C%22orderRef%22%3A%22URF_1514109007270_1408935%22%2C%22flwRef%22%3A%22FLW-MOCK-6ef5fba0e6d6eaf49b367f3728bf7901%22%2C%22redirectUrl%22%3A%22https%3A%2F%2Fwww.domainname.com%2Fwhmcs%2Fmodules%2Fgateways%2Fcallback%2Fwhmcs.php%3Finvoiceid%3D316%22%2C%22device_fingerprint%22%3A%22bb6c9abc3306a4902ed9fcfda80ff51d%22%2C%22settlement_token%22%3Anull%2C%22cycle%22%3A%22one-time%22%2C%22amount%22%3A2%2C%22charged_amount%22%3A2%2C%22appfee%22%3A0.1288961038961039%2C%22merchantfee%22%3A0%2C%22merchantbearsfee%22%3A1%2C%22chargeResponseCode%22%3A%2200%22%2C%22raveRef%22%3A%22RV31514109005971CBADF3EFDF%22%2C%22chargeResponseMessage%22%3A%22Success-Pending-otp-validation%22%2C%22authModelUsed%22%3A%22VBVSECURECODE%22%2C%22currency%22%3A%22USD%22%2C%22IP%22%3A%22129.56.12.21%22%2C%22narration%22%3A%22FLW-PBF+CARD+Transaction+%22%2C%22status%22%3A%22successful%22%2C%22vbvrespmessage%22%3A%22Approved.+Successful%22%2C%22authurl%22%3A%22http%3A%2F%2Fflw-pms-dev.eu-west-1.elasticbeanstalk.com%2Fmockvbvpage%3Fref%3DFLW-MOCK-6ef5fba0e6d6eaf49b367f3728bf7901%26code%3D00%26message%3DApproved.+Successful%26receiptno%3DRN1514109007311%22%2C%22vbvrespcode%22%3A%2200%22%2C%22acctvalrespmsg%22%3Anull%2C%22acctvalrespcode%22%3A%22RN1514109007311%22%2C%22paymentType%22%3A%22card%22%2C%22paymentPlan%22%3Anull%2C%22paymentPage%22%3Anull%2C%22paymentId%22%3A%222%22%2C%22fraud_status%22%3A%22ok%22%2C%22charge_type%22%3A%22normal%22%2C%22is_live%22%3A0%2C%22createdAt%22%3A%222017-12-24T09%3A50%3A07.000Z%22%2C%22updatedAt%22%3A%222017-12-24T09%3A50%3A28.000Z%22%2C%22deletedAt%22%3Anull%2C%22customerId%22%3A10575%2C%22AccountId%22%3A907%2C%22customer%22%3A%7B%22id%22%3A10575%2C%22phone%22%3Anull%2C%22fullName%22%3A%22Anonymous+customer%22%2C%22customertoken%22%3Anull%2C%22email%22%3A%22customeremail%40gmail.com%22%2C%22createdAt%22%3A%222017-12-24T09%3A50%3A06.000Z%22%2C%22updatedAt%22%3A%222017-12-24T09%3A50%3A06.000Z%22%2C%22deletedAt%22%3Anull%2C%22AccountId%22%3A907%7D%2C%22chargeToken%22%3A%7B%22user_token%22%3A%2207924%22%2C%22embed_token%22%3A%22flw-t0-9a321f32b799b489796ffc44a63a5762-m03k%22%7D%7D%2C%22respmsg%22%3A%22Approved.+Successful%22%7D

接下来要做的就是收集$ _GET ['resp']参数并对其进行解码

<?php
// Require libraries needed for gateway module functions.
require_once __DIR__ . '/../../../init.php';
require_once __DIR__ . '/../../../includes/gatewayfunctions.php';
require_once __DIR__ . '/../../../includes/invoicefunctions.php';


 $response = $_GET['resp'];

 $response = json_decode($response);
 // and access the values like
 $response->tx->amount //10.00

然而,Json_decode失败并返回null,就像json格式不正确一样。

经过进一步调查。

我意识到,如果在非whmcs安装上测试这个精确的url字符串,json_decode工作正常,我可以作为对象或关联数组访问数据。

当我从网关回调文件中删除下面的行时。它工作正常。似乎是whmcs应用程序正在搞乱json函数。

    // Require libraries needed for gateway module functions.
    require_once __DIR__ . '/../../../init.php';

但支付网关回调无法使用必要的标头,尤其是init.php文件。

我如何处理这种情况并超越它。

感谢。

1 个答案:

答案 0 :(得分:0)

好的,我想通了@GerardVanHelden的建议,WHMCS可能会弄乱$ _GET值。

所以我只是在require_once语句之前收集$ _GET值。 和json_decode正常工作。

干杯。