1

尝试制作一个允许使用多个支付网关的组件(是的,我意识到以前已经做过)。我创建了一个“PaymentsComponent”,以及一些扩展它的网关特定组件。

问题是,我无法让在父组件中设置/初始化的变量在子组件中可访问。它们是可访问的,但只能使用默认值,而不是初始化后的值。我错过了什么?

在控制器内:

private function processPayment($requestData) {
    $this->Payments = $this->Components->load('Payments');
    $this->Payments->initialize($this);
    debug($this->Payments->apiSecretKey); // shows: '123456789'
    $result = $this->Payments->charge($requestData);
    //...
}

支付组件

<?php
App::uses('Component', 'Controller');
class PaymentsComponent extends Component {

    public $apiSecretKey = null;

    public function initialize() {
        $this->apiSecretKey = Configure::read('PaymentGateway.secret_key');
    }

    public function charge($data) {
        $result = $this->Authnet->charge($data);
    }
    // ...
}

Authorize.net 组件

class AuthNetComponent extends PaymentsComponent {
    public $components = array('Session');
    public function charge($data) {
        debug($this->apiSecretKey); // shows: null
        // ...
    }
    // ...
}
4

0 回答 0