【问题标题】:Call function in other method其他方法调用函数
【发布时间】:2015-01-22 01:18:29
【问题描述】:

我试图在 $this->mollie->payments->create 方法中调用 testFunction(),但出现错误:

致命错误:未捕获异常“Mollie_API_Exception”,并显示消息“执行 API 调用(请求)时出错:数量低于最小值。”

所以这意味着 $this->testFunction() 返回 0。

我做了以下测试:

$test = new gtpMollieGateway();
echo $test->testFunction();

这给出了一个正确的值(我结帐的计算金额)。

所以这意味着我在 $this->mollie->payments->create 方法中调用 testFunction() 有问题

我的支付代码:

// Create payment
class gtpMollieGateway {
    public $mollie, $price;

    function __construct() {
       $this->mollie    = new Mollie_API_Client;
       $this->mollie->setApiKey( 'myapikey' );
       $this->price         = new gtpCheckoutData();

       add_action( 'init', array( $this, 'gtpCreatePayment' ) );
    }

    private function testFunction() {
        return $this->price->getPrice( 'inclusive' );
    }

    public function gtpCreatePayment() {
       if( isset( $_POST['checkout_submit'] ) ) {
           $payment     = $this->mollie->payments->create( array(
                // Here is the problem
                'amount'        => $this->testFunction(),
           ));
           header( 'Location: ' . $payment->getPaymentUrl() );
       }
    }
}

$_mollie = new gtpMollieGateway;

计算我的金额的类:

class gtpCheckoutData {
    private $tax, $price;

    public function __construct() {
        $this->tax      = get_option( 'gtp_theme_settings' );
        $this->tax      = $this->tax['gtp_tax'] / 100;

        if( isset( $_SESSION['shopping_cart'] ) ) {
            $this->price    = $_SESSION['shopping_cart']['total_price'] + $_SESSION['shopping_cart']['shipping_price'];
            $this->shipping = $_SESSION['shopping_cart']['shipping_price'];
        }
    }

    public function getPrice( $type ) {

        if( isset( $type ) ) {
            switch( $type ) {
                case 'exclusive' : 
                    $totalPrice = $this->price;
                    break;
                case 'tax' :
                    $totalPrice = $this->price * $this->tax;
                    break;  
                case 'inclusive' :
                    $totalPrice = $this->price * ( $this->tax + 1 );
                    break;
            }
            return $totalPrice;
        }
    }
}

【问题讨论】:

  • 您的支付网关似乎有误,请查看您的支付集成指南。
  • @hardiksolanki 他说amount 为 0 时会发生此错误
  • 您可能没有定义 $_SESSION['shopping_cart']['total_price']。你可以做 var_dump($_SESSION) 并检查它是否存在于会话中?另外你有太多的耦合,尝试重构你的代码
  • @Igor 我的会话已设置。我已经通过我的测试(见我的问题)检查了这一点,它给出了正确的值。所以这不是问题。耦合是什么意思?
  • 我的意思是,例如 gtpCheckoutData 直接读取 $_SESSION 变量可能会更好地注入 $_SESSION 在构造函数中提供的值或为此提供一些接口。但这一切都无关紧要,只是让我们更难找出问题所在:) get_option( 'gtp_theme_settings' )['gtp_tax'] 返回的值是否不为零?

标签: php function class methods payment-gateway


【解决方案1】:

您的函数 testFunction() 正在调用函数函数 getPrice( $type ) 并且函数 getPrice( $type ) 返回 0,因此可能是由于金额为 0,您从外部 api(支付网关 api)收到此错误。

【讨论】:

  • 感谢您的回答,但这就是我在问题中已经说过的。
【解决方案2】:

经过长时间的搜索和尝试,我发现了问题。我的会话没有在我的插件文档中开始。 Wordpress 在functions.php 之前加载插件,我的会话是在functions.php 中启动的。

所以这就是我的值为 0 的原因,这就是发生错误的原因。

【讨论】:

    猜你喜欢
    • 2011-08-18
    • 2017-10-26
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多