Posted on

Custom Payment Gateway – How to Call Capture from a Controller and pass parameter

So I’m creating a custom payment gateway module for my company, my company has a rest API connected to our back-office.
I don’t know if I have the best approach for this, but I did it this way:

1 Client places an order in Magento store

2 Authorization Request is executed – What authorization does is a call to our API this call will create a payment order in our back-office, if the request is successful we add some additional information to the payment object in Magento and set a transaction ID, and change order status if needed.

3 An after place order observer is executed and redirects to our payment website, so the client can pay, if successful returns to Magento store Custom Controller

4 This controller get some parameters from URL like order ID and uses this ID to get an order object from where I try to call $payment->capture(); but this where it gets messy.

So my idea is to call $payment->capture(); and inside capture I will make a call to our API and check if the payment has been paid and redirect to check out Success page with a custom block. But my problem is when I call $payment->capture() the capture request doesn’t “retrieve” my order object. Which is lead to null order inside capture request.

So is there a way to pass my order from my Controller to Capture Request Gateway?

This is my controller execute function, this controller extendes Action

public function execute()
{
//Get Order from request
$orderId = $this->getRequest()->getParam(‘order’);

//Order Object
$this->order = $this->_objectManager->create(‘MagentoSalesModelOrder’)->loadByIncrementId($orderId);

//Debugger
$this->debugger->log(“[Order ID] “.json_encode($orderId));

//Debugger
$this->debugger->log(“[Order payment reference] “.json_encode($this->order->getPayment()->getAdditionalInformation(‘referencia’)));

//Payment object associated to order
$payment = $this->order->getPayment();

try{
$payment->capture(); // I call capture gateway method here, but I want to pass my order to capture request gateway
}catch (PaymentException $e){
$this->debugger->log(“[Capture Call] Erro trying to execute capture call”);
}

}

I know this is probably not the best implementation for this case, and maybe it could be done it simpler with just controllers and models, but I want it to have some hierarchy & organization like on Magento docs, for future developers to be able to understand the concept. The thing that gets me frustrated is Magento docs I feel like they didn’t put the effort in those, they just give you “the ropes” and depreciated example codes that are specific to a certain company like “Braintree & PayPal”. Thank you in advance!

Leave a Reply

Your email address will not be published.