Question

I have to make a new payment module which redirect to a third party payment gateway.

When the customer click the PAY button the POST parameters (order details) need to be sent to the gateway. (And then after the successful/unsuccsessful payment the customer needs to be redirected to a success/cancel page respectively.)

According to the instructions given by the gateway provider An easy way to implement this functionality is to insert fields of type hidden at the e-shop payment form. These fields would contain values about the merchant, amount, payment details etc. For example:

<form action="https://www.gateway.com/.../" method="post" ID="Form1">
<input type="hidden" id="AmountToPay" name="AmountToPay" value="amount"/>
<input type="hidden" id="AmountCurrency" name="AmountCurrency" value="currency"/>
<input type="hidden" id="Details1" name="Details1" value="details"/>
<input type="hidden" id="Details2" name="Details2" value="orderID"/>
<input type="hidden" id="PayToMerchant" name="PayToMerchant" value="merchantID"/>
<input type="hidden" id="MerchantName" name="MerchantName" value="merchant"/>
<input type="hidden" id="PaymentOKURL" name="PaymentOKURL" value="https://shop.com/e-shopOK.html"/>
<input type="hidden" id="PaymentFailURL" name="PaymentFailURL" value="https://eshop.com/e-shopCancel.html"/>
</form>

Since my knowledge in Magento is somewhat limited, I'm having trouble translating this into a working magento module.

While researching I found several posible solutions, but I'm not sure which one is the right one (and how to implement them):

1.

$client = new Varien_Http_Client('http://www.example.com/');
$client->setMethod(Varien_Http_Client::POST);
$client->setParameterPost('name', $name);
$client->setParameterPost('address', $address);
//more parameters
try{
    $response = $client->request();
    if ($response->isSuccessful()) {
        echo $response->getBody();
    }
} catch (Exception $e) {
}

2.

public function automatic(Varien_Event_Observer $observer) {
    $orderIds = $observer->getEvent()->getOrderIds();
    if (empty($orderIds) || !is_array($orderIds)) {
        return;
    }
    foreach ($orderIds as $eachOrderId) {
        $order = Mage::getModel('sales/order')->load($eachOrderId);
        //right our third party code
        client = new Zend_Http_Client();
        $client->setUri('http://eshop.com');
        $client->>setParameterPost(array(
        'api_key' => 'yourapi',
        'apikey' => 'xxxx',
        'message' => $message,
        'order_id' => $order_id,
        ...//more params
        ));
        $response = $client->request('POST');
        // Yet another way of preforming a POST request
        $client->setMethod(Zend_Http_Client::POST);
        if (!$response->isSuccessful()) {
            Mage::log($response);
        }
    }
    return $this;
}

etc etc.

I know it is not a very specific question, but any guidelines of how I could achieve this the right way?

EDIT

Following some tutorials I created some payment module which currently looks like this: Payment Gateway

Regards...

Was it helpful?

Solution

<?php $_order = new Mage_Sales_Model_Order();
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$_order->loadByIncrementId($orderId);

$amountToPay = $_order->getBaseGrandTotal();
?>

<form action='https://www.gateway.com/.../' method='post'>
<input id='AmountToPay' name='AmountToPay' value="<?php echo $amountToPay; ?>" type='hidden' />
<input id='Details2' name='Details2' value='<?php echo $orderId; ?>' type='hidden' />
</form>

and so on with the rest of the form fields...

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top