下面是" Paytrail_Module_Rest.php"的示例代码,这是一组用于与支付网关的rest api进行交互的类。有些类可以提前实例化,例如(Paytrail_Module_rest保存凭证),但有些类需要使用控制器中仅提供的信息进行实例化(例如Paytrail_Module_Rest_Payment_S1设置付款细节,如价格)
有人能建议将其注入slim3吗?我无法通过标准容器注入方法看到任何好方法。
$urlset = new\App\Service\Paytrail\Paytrail_Module_Rest_Urlset(
"https://www.demoshop.com/sv/success", // return address for successful payment
"https://www.demoshop.com/sv/failure", // return address for failed payment
"https://www.demoshop.com/sv/notify", // address for payment confirmation from Paytrail server
"" // pending url not in use
);
$orderNumber = '1';
$price = 99.00;
$payment = new \App\Service\Paytrail\Paytrail_Module_Rest_Payment_S1($orderNumber, $urlset, $price);
$payment->setLocale('en_US');
$module = new \App\Service\Paytrail\Paytrail_Module_Rest(13466, '6pKF4jkv97zmqBJ3ZL8gUw5DfT2NMQ');
try {
$result = $module->processPayment($payment);
}
catch (\App\Service\Paytrail\Paytrail_Exception $e) {
die('Error in creating payment to Paytrail service:'. $e->getMessage());
}
echo $result->getUrl();
(此处列出的凭据是公共测试凭据)
答案 0 :(得分:1)
将未更改的内容添加到容器中,如模块和urlset thingy
$container[\App\Service\Paytrail\Paytrail_Module_Rest_Urlset::class] = function($c) {
return new \App\Service\Paytrail\Paytrail_Module_Rest_Urlset(
"https://www.demoshop.com/sv/success", // return address for successful payment
"https://www.demoshop.com/sv/failure", // return address for failed payment
"https://www.demoshop.com/sv/notify", // address for payment confirmation from Paytrail server
"" // pending url not in use
);
};
$container[\App\Service\Paytrail\Paytrail_Module_Rest::class] = function($c) {
return new \App\Service\Paytrail\Paytrail_Module_Rest(13466, '6pKF4jkv97zmqBJ3ZL8gUw5DfT2NMQ');
};
然后您可以在每次需要时实例化付款或添加辅助类(如适配器):
class PaymentAdapter {
public function __construct(
\App\Service\Paytrail\Paytrail_Module_Rest $module,
\App\Service\Paytrail\Paytrail_Module_Rest_Urlset $urlset)
{
$this->module = $module;
$this->urlset = $urlset;
}
function createAndProcessPayment($orderNumber, $price)
{
$payment = new \App\Service\Paytrail\Paytrail_Module_Rest_Payment_S1($orderNumber, $this->urlset, $price);
$payment->setLocale('en_US');
try {
$result = $module->processPayment($payment);
}
catch (\App\Service\Paytrail\Paytrail_Exception $e) {
die('Error in creating payment to Paytrail service:'. $e->getMessage());
}
return $result;
}
}
然后将适配器也添加到容器中:
$container[\yournamespace\PaymentAdapter::class] = function($c) {
return new \yournamespace\PaymentAdapter(
$c[\App\Service\Paytrail\Paytrail_Module_Rest::class],
$c[\App\Service\Paytrail\Paytrail_Module_Rest_Urlset::class]
);
};