【发布时间】: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