在其他方法中调用函数


Call function in other method

我正在尝试在$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;
        }
    }
}
您的函数 testFunction() 正在调用函数函数 getPrice( $type )

和函数 getPrice( $type ) 返回 0,所以可能是因为 0 金额您从外部 api(支付网关 api)收到此错误。

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

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