I have created a payment module, following the devdocs approach by creating virtual types, request builder, transfer factory, the client (using Zend client - curl adapter) until validation handler for capture command.
The 3rd party payment gateway will redirect the customer to their own gateway page to do the debit process. Unfortunately, in the header, there is no location URL, since the 1st gateway page redirects to another page via javascript submit and adding some additional post data on the top of the post data send by the Zend client curl adapter.
After successful, the customer will be redirected back to Magento 2 store at specified URL (controller) with some response post data.
My current issues are since no location in the header, Zend client unable to do a redirect, thus I have to process the redirection to 3rd party gateway and halted the process in the middle from finishing the rest of client method placeRequest() up to response handler and validation. Now, since I have no choice but to receive the response data from the controller, I am not sure how to continue back from client up to validation and response handler.
From my gateway client, please check the comment section in the code:
namespace Afham\Fpx\Gateway\Http\Client;
use Magento\Framework\HTTP\ZendClientFactory;
use Magento\Payment\Gateway\Http\ClientInterface;
use Magento\Payment\Gateway\Http\ConverterInterface;
use Magento\Payment\Gateway\Http\TransferInterface;
use Magento\Payment\Gateway\Http\ClientException;
use Psr\Log\LoggerInterface;
class TransactionSale implements ClientInterface
{
protected $clientFactory;
protected $converter;
protected $logger;
public function __construct(
ZendClientFactory $clientFactory,
LoggerInterface $logger,
ConverterInterface $converter = null
){
$this->clientFactory = $clientFactory;
$this->converter = $converter;
$this->logger = $logger;
}
public function placeRequest(TransferInterface $transferObject)
{
$log = [
'request' => $transferObject->getBody(),
'request_uri' => $transferObject->getUri()
];
$result = [];
/** @var ZendClient $client */
$client = $this->clientFactory->create();
$client->setConfig($transferObject->getClientConfig());
$client->setMethod($transferObject->getMethod());
switch($transferObject->getMethod()) {
case \Zend_Http_Client::GET:
$client->setParameterGet($transferObject->getBody());
break;
case \Zend_Http_Client::POST:
$client->setParameterPost($transferObject->getBody());
break;
default:
throw new \LogicException(
sprintf(
'Unsupported HTTP method %s',
$transferObject->getMethod()
)
);
}
$client->setHeaders($transferObject->getHeaders());
$client->setUrlEncodeBody($transferObject->shouldEncode());
$client->setUri($transferObject->getUri());
try {
$response = $client->request();
/** This is where it stops, I am planning to output the $response
since curl returns us the html (checked from xdebug) with
automatic javascript redirect to another page at gateway side
and additional post data on the top of previous one.
After customer done at gateway, the gateway will send back the
results post data to webstore (magento 2 controller). My plan is
to continue from here at the controller side, so that it will
run response validation and response handler, which it will mark
the order to processing
**/
$result = $this->converter
? $this->converter->convert($response->getBody())
: [$response->getBody()];
$log['response'] = $result;
} catch (\Zend_Http_Client_Exception $e) {
throw new \Magento\Payment\Gateway\Http\ClientException(
__($e->getMessage())
);
} catch (\Magento\Payment\Gateway\Http\ConverterException $e) {
throw $e;
} finally {
$this->logger->debug($log);
}
return $result;
}
Please advise, I have been in a circle for days, less sleep and its catching the dateline..
thanks
UPDATE: Found the answer. I will post later when I am free. Its just me that I dont understand the command gateway flow