【问题标题】:WHMCS custom function - Stripe payment gateway errorWHMCS 自定义功能 - 条纹支付网关错误
【发布时间】:2020-03-29 02:53:41
【问题描述】:

我正在尝试在 WHMCS 中添加自定义功能,通过创建文件 addstripecard.php,该想法基于给定的客户令牌(类似于 cus_1GPkTFJEegxX3y3c123456),然后尝试将其附加到卡上。从 API 访问自定义函数。

首先,我尝试使用以下命令访问 Stripe 库:

$gateway = new \WHMCS\Module\Gateway();
$gateway->load("stripe");

PS:代码取自https://github.com/WHMCSCare/WHMCS-7.8.0-decoded/blob/e7446479de49a28c8801d4c0c95f4cae22dcff33/modules/gateways/stripe/lib/StripeController.php

但是,运行上述命令后,我收到了以下错误消息:

FastCGI sent in stderr: "PHP message: [WHMCS Application] ERROR: TypeError: Argument 2 passed to WHMCS\Api\ApplicationSupport\Http\ResponseFactory::factory() must be of the type array, string given,

called in /www/xxxxxx.xxx/billing/vendor/whmcs/whmcs-foundation/lib/Api/ApplicationSupport/Route/Middleware/HandleProcessor.php on line 0 

and defined in /www/xxxxxx.xxx/billing/vendor/whmcs/whmcs-foundation/lib/Api/ApplicationSupport/Http/ResponseFactory.php:0

Stack trace: #0 /www/xxxxxx.xxx/billing/vendor/whmcs/whmcs-foundation/lib/Api/ApplicationSupport/Route/Middleware/HandleProcessor.php(0): WHMCS\Api\ApplicationSupport\Http\ResponseFactory::factory(Object(WHMCS\Api\ApplicationSupport\Http\ServerRequest), '{"legacyGateway...') #1 /www/xxxxxx.xxx/billing/vendor/whmcs/whmcs-foundation/lib/Route/Middleware/Strategy/DelegatingMiddlewareTrait.php(0): WHMCS\Api\ApplicationSupport\Route\Middleware\HandleProcessor->_process(Object(WHMCS\Api\ApplicationSupport\Http\ServerRequest), Object(Middlewares\Utils\Delegate)) #2 /www/dat" while reading response header from upstream, client: 172.81.129.13, 

server: xxxxxx.xxx.net, 

request: "POST /billing/includes/api.php HTTP/1.1",

upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:",

host: "xxxxxx.xxx"

完整代码:

<?php

if (!defined("WHMCS")) {
    die("This file cannot be access directly!");
}

function addStripeCard()
{
    // card parameters
    $cardNumber       = $_POST['cardnumber'];
    $cardExpiryDate   = $_POST['cardexpirydate'];
    $remoteToken      = $_POST['remotetoken'];
    $billingContactId = $_POST['billingcontactid'];
    $description      = $_POST['description'];
    $cardType         = $_POST['cardtype'];
    $cardStartDate    = $_POST['cardstartdate'];
    $cardIssueNumber  = $_POST['cardissuenumber'];
    $clientId         = $_POST['clientid'];

    try {
        $gateway = new \WHMCS\Module\Gateway();

        $gateway->load("stripe");

        return json_encode($gateway);
    } catch (Exception $e) {
        $error = [
            'result'  => 'error',
            'message' => $e->getMessage(),
            'file'    => $e->getFile(),
            'line'    => $e->getLine(),
        ];

        return $error;
    }
}

try {
    $data = addStripeCard();

    $apiresults = $data;

} catch (Exception $e) {
    $error = [
        'result'  => 'error',
        'message' => $e->getMessage(),
        'file'    => $e->getFile(),
        'line'    => $e->getLine(),
    ];

    $apiresults = $error;
}

有人知道发生了什么吗?我已经尝试了很多次谷歌搜索,但找不到任何答案。

【问题讨论】:

    标签: php php-7 whmcs


    【解决方案1】:

    我找到了答案。

    当向客户端返回响应时,我们至少应该遵循这种格式:

    $apiresults = array('result' => $result, 'message' => $message);
    

    它可以扩展为:

    $apiresults = array('result' => $result, 'message' => $message, 'data' => $data);
    

    所以我将上面的代码修改为:

    <?php
    
    if (!defined("WHMCS")) {
        die("This file cannot be access directly!");
    }
    
    function addStripeCard()
    {
        // card parameters
        $cardNumber       = $_POST['cardnumber'];
        $cardExpiryDate   = $_POST['cardexpirydate'];
        $remoteToken      = $_POST['remotetoken'];
        $billingContactId = $_POST['billingcontactid'];
        $description      = $_POST['description'];
        $cardType         = $_POST['cardtype'];
        $cardStartDate    = $_POST['cardstartdate'];
        $cardIssueNumber  = $_POST['cardissuenumber'];
        $clientId         = $_POST['clientid'];
    
        try {
            $gateway = new \WHMCS\Module\Gateway();
    
            $gateway->load("stripe");
    
            return json_encode($gateway);
        } catch (Exception $e) {
            $error = [
                'result'  => 'error',
                'message' => $e->getMessage(),
                'file'    => $e->getFile(),
                'line'    => $e->getLine(),
            ];
    
            return $error;
        }
    }
    
    try {
        $data = addStripeCard();
    
        $apiresults = array('result' => 'success', 'message' => 'success message', 'data' => $data);
    } catch (Exception $e) {
        $error = [
            'result'  => 'error',
            'message' => $e->getMessage(),
            'file'    => $e->getFile(),
            'line'    => $e->getLine(),
        ];
    
        $apiresults = $error;
    }
    

    错误消失后,访问自定义函数的 API 端点会收到 200 响应。

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 2021-10-08
      • 2020-10-14
      • 2022-08-19
      • 1970-01-01
      • 2014-12-06
      • 2017-03-02
      • 2011-09-08
      • 2017-08-05
      相关资源
      最近更新 更多