Below is an example code for "Paytrail_Module_Rest.php", a set of classes for interacting with a rest api for a payment gateway. Some of the classes can be instantiated ahead of time such as (Paytrail_Module_rest which holds credentials), but some need to be instantiated with information only available in the controller, (such as Paytrail_Module_Rest_Payment_S1 which sets payment details such as price)
Can anyone suggest a clean way of injecting it into slim3? I can't see any good way of doing it with the standard container injection methods.
$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();
( credentials listed here are public test credentials )
Add the stuff that doesn't change to the container like the module and the 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');
};
And then you either can instantiate the payment every time you need or add a helper class like an adapter:
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;
}
}
Then add the adapter also to the container:
$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]
);
};