Php 在其他方法中调用函数
我试图在$this->mollie->payments->create方法中调用testFunction(),但出现错误: 致命错误:未捕获异常“Mollie_API_exception”,消息为“执行API调用(请求)时出错:数量低于最小值”。 这意味着$this->testFunction()返回0 我做了以下测试:Php 在其他方法中调用函数,php,function,class,methods,payment-gateway,Php,Function,Class,Methods,Payment Gateway,我试图在$this->mollie->payments->create方法中调用testFunction(),但出现错误: 致命错误:未捕获异常“Mollie_API_exception”,消息为“执行API调用(请求)时出错:数量低于最小值”。 这意味着$this->testFunction()返回0 我做了以下测试: $test = new gtpMollieGateway(); echo $test->testFunction(); 这给出了一个正确的值(我结账时计算的金额) 这意
$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;
}
}
}
计算我的金额的类:
// 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的原因,这就是发生错误的原因。这似乎是您的支付网关错误,所以请检查您的支付集成指南。@hardiksolanki他说当
金额为0时会发生此错误。您可能没有定义$\u会话['shopping\u cart']['total\u price']。您是否可以执行var_转储($_会话)并检查会话中是否存在这种情况?此外,您的耦合太多,请尝试重新考虑您的code@Igor我的课时安排好了。我已经通过给出正确值的测试(见我的问题)对此进行了检查。所以这不是问题所在。耦合是什么意思?我的意思是,例如,gtpCheckoutData直接读取$\会话变量,最好在构造函数中注入$\会话提供的值,或者为此提供一些接口。但这一切都无关紧要,只是让我们更难找出问题所在:)get_option('gtp_主题设置')['gtp_tax']返回的值不是零吗?谢谢你的回答,但这是我在问题中已经说过的。