Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom payment gateway to Social Engine

I solved this by adding the payment code inside the Engine_Payment_Gateway_MyGateway class:

Once the user confirms on the SocialEngine page that they want to pay, the method processTransaction() inside the mentioned class is called and the user is redirected to the PSP's payment secure page. Once they are done with the payment, i.e. paid successfully or failed or canceled the transaction, they PSP's page redirects them to the page we had sent to it earlier as a parameter called callBackUrl. There, you will receive PSP-specific parameters which helps you decide whether the payment was successful and to ask the PSP with another SOAP call to confirm the payment and then optionally ask it to settle (deposit money ASAP into the seller's account):

Add to processTransaction():

        $data = array();
        $rawData = $transaction->getRawData();

        //Save order ID for later
        $this->_orderId = $rawData['vendor_order_id'];
        $this->_grandTotal = $rawData['AMT'];


        $client = new nusoap_client('https://example.com/pgwchannel/services/pgw?wsdl');
        $namespace = 'http://interfaces.core.sw.example.com/';


        // Check for an error
        $err = $client->getError();
        if ($err) {
            echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
            die();
        }


        /* Set variables */

//Get price from SEAO 
//$order_ids = Engine_Api::_()->getDbTable('orders','sitestoreproduct')->getOrderIds($this->parent_id);
//$price = Engine_Api::_()->getDbTable('orders','sitestoreproduct')->getGrandTotal($this->parent_id);


        $terminalId = '1111111';
        $userName = 'username';
        $userPassword = '1111111';
        $orderId = $rawData['vendor_order_id'];

        $amount = $rawData['AMT'];
        $localDate = date("Y") . date("m") . date("d");
        $localTime = date("h") . date("i") . date("s");
        $additionalData = $rawData['return_url'];
        $callBackUrl = 'https://example.com/pgateway/pay/callback';
        $payerId = '0';







        /* Define parameters array   */

        $parameters = array(
            'terminalId' => $terminalId,
            'userName' => $userName,
            'userPassword' => $userPassword,
            'orderId' => $orderId,
            'amount' => $amount,
            'localDate' => $localDate,
            'localTime' => $localTime,
            'additionalData' => $additionalData,
            'callBackUrl' => $callBackUrl,
            'payerId' => $payerId
        );



        $result = $client->call('bpPayRequest', $parameters, $namespace);



        if ($client->fault) {
            echo '<h2>Fault</h2><pre>';
            print_r($result);
            echo '</pre>';
            die();
        } else { //Check for errors
            $error = $client->getError();
            if ($error) {
                echo "An error occurred: ";
                print_r($error);
                die();
            } else {
                //break the code
                $resultSegmts = explode(',', $result);
                $ResCode = $resultSegmts [0];


                if ($ResCode == "0") {
                    //Notify admin of the order                    
                    echo '<h3>Redirecting you to the payment page. Please wait...</h3><br/>';
                    echo '<script language="javascript" type="text/javascript">
                      postRefId("' . $resultSegmts[1] . '");
                    </script>';
                } elseif ($ResCode == "25") {
                    echo "<h3>Purchase successful</h3>";
                } else {
                    echo "<h3>PSP response is: $ResCode</h3>";
                } 
            }
        }

Add to your callBack action:

    $this->view->message = 'This is callback action for PayController';
    $RefId = $_POST['RefId'];
    $ResCode = $_POST['ResCode'];
    $saleOrderId = $_POST['SaleOrderId'];
    $saleReferenceId = $_POST['SaleReferenceId'];

    $this->_orderId = $saleOrderId;

        $this->view->RefId = $RefId;
        $this->view->saleOlderId = $saleOrderId;
        $this->view->saleReferenceId = $saleReferenceId;
    }
    if ($ResCode == "0") {
        try {
            $client = new nusoap_client('https://example.com/pgwchannel/services/pgw?wsdl');
        } catch (Exception $e) {
            die($e->getMessage());
        }

        $namespace = 'http://interfaces.core.sw.example.com/';
        $terminalId = "111111";
        $userName = "username";
        $userPassword = "11111111";


        $parameters = array(
            'terminalId' => $terminalId,
            'userName' => $userName,
            'userPassword' => $userPassword,
            'orderId' => $saleOrderId,
            'saleOrderId' => $saleOrderId,
            'saleReferenceId' => $saleReferenceId
        );
        $resVerify = $client->call('bpVerifyRequest', $parameters, $namespace);



        if ($resVerify->fault) { //Check for fault 
            echo "<h1>Fault: </h1>";
            print_r($result);
            die();
        } else { //No fault: check for errors now 
            $err = $client->getError();
            if ($err) {
                echo "<h1>Error: " . $err . " </h1>";
            } else {
                if ($resVerify == "0") {//Check verification response: if 0, then purchase was successful. 
                    echo "<div class='center content green'>Payment successful. Thank you for your order.</div>";
                    $this->view->message = $this->_translate('Thanks for your purchase.');
                    $this->dbSave(); //update database table
                } else
                    echo "<script language='javascript' type='text/javascript'>alert(    'Verification Response: " . $resVerify . "');</script>";
            }
        }

        //Note that we need to send bpSettleRequest to PSP service to request settlement once we have verified the payment 
        if ($resVerify == "0") {
            // Update table, Save RefId
            //Create parameters array for settle
            $this->sendEmail();
            $this->sendSms();

            $resSettle = $client->call('bpSettleRequest', $parameters, $namespace);
            //Check for fault 
            if ($resSettle->fault) {
                echo "<h1>Fault: </h1><br/><pre>";
                print_r($resSettle);
                echo "</pre>";
                die();
            } else { //No fault in bpSettleRequest result 
                $err = $client->getError();
                if ($err) {
                    echo "<h1>Error: </h1><pre>" . $err . "</pre>";
                    die();
                } else {
                    if ($resSettle == "0" || $resSettle == "45") {//Settle request successful 
                        // echo "<script language='javascript' type='text/javascript'>alert('Payment successful');</script>";
                    }
                }
            }
        }
    } else {
        echo "<div class='center content error'>Payment failed. Please try again later.</div> ";
        // log error in app
        // Update table, log the error
        // Show proper message to user
    }
    $returnUrl = 'https://example.com/stores/products'; //Go to store home for now. Later I'll set this to the last page 
    echo "<div class='center'>";
    echo "<form action=$returnUrl method='POST'>";
    echo "<input class='center' id='returnstore' type='submit' value='Return to store'/>";
    echo "</form>";
    echo "</div>";