интеграция платежного шлюза payfast с помощью codeigniter


Я новичок в интеграции платежного шлюза PayFast в Codeigniter.

Вот мой код, который генерирует форму:

Form

<form action="https://sandbox.payfast.co.za/eng/process" method="post">
    <input id="merchant_id" name="merchant_id" value="ID" type="hidden">
    <input id="merchant_key" name="merchant_key" value="KEY" type="hidden">
    <input id="return_url" name="return_url" value="RETURNURL" type="hidden">
    <input id="cancel_url" name="cancel_url" value="CANCELURL" type="hidden">
    <input id="notify_url" name="notify_url" value="http://website.com/deal/itn" type="hidden">
    <input id="name_first" name="name_first" value="test" type="hidden">
    <input id="name_last" name="name_last" value="test1" type="hidden">
    <input id="email_address" name="email_address" value="[email protected]" type="hidden">
    <input id="m_payment_id" name="m_payment_id" value="TRN1481493600" type="hidden">
    <input id="amount" name="amount" value="930" type="hidden">
    <input id="item_name" name="item_name" value="package" type="hidden">
    <input id="item_description" name="item_description" value="package" type="hidden">
    <input id="payment_method" name="payment_method" value="cc" type="hidden">
    <input id="signature" name="signature" value="a8836e97dfdbeb6116747d7c1130f4ff" type="hidden">
</form>

Controller

<?php
function itn()
{
    define( 'PAYFAST_SERVER', 'TEST' );
    define( 'USER_AGENT', 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)' );
    define( 'PF_ERR_AMOUNT_MISMATCH', 'Amount mismatch' );
    define( 'PF_ERR_BAD_SOURCE_IP', 'Bad source IP address' );
    define( 'PF_ERR_CONNECT_FAILED', 'Failed to connect to PayFast' );
    define( 'PF_ERR_BAD_ACCESS', 'Bad access of page' );
    define( 'PF_ERR_INVALID_SIGNATURE', 'Security signature mismatch' );
    define( 'PF_ERR_CURL_ERROR', 'An error occurred executing cURL' );
    define( 'PF_ERR_INVALID_DATA', 'The data received is invalid' );
    define( 'PF_ERR_UKNOWN', 'Unkown error occurred' );
    define( 'PF_MSG_OK', 'Payment was successful' );
    define( 'PF_MSG_FAILED', 'Payment has failed' );
    header( 'HTTP/1.0 200 OK' );
    flush();
    $pfError = false;
    $pfErrMsg = '';
    $filename = 'notify.txt';
    $output = '';
    $pfParamString = '';
    $pfHost = ( PAYFAST_SERVER == 'LIVE' ) ?
    'www.payfast.co.za' : 'sandbox.payfast.co.za';
    $pfData = [];

    if( !$pfError )
    {
        $output = $_POST;
        foreach( $_POST as $key => $val )
            $pfData[$key] = stripslashes( $val );

        foreach( $pfData as $key => $val )
        {
            if( $key != 'signature' )
                $pfParamString .= $key .'='. urlencode( $val ) .'&';
        }

        $pfParamString = substr( $pfParamString, 0, -1 );
        $pfTempParamString = $pfParamString;

        $passPhrase = '123456789101112';
        if( !empty( $passPhrase ) )
        {
            $pfTempParamString .= '&passphrase='.urlencode( $passPhrase );
        }
        $signature = md5( $pfTempParamString );

        $result = ( $_POST['signature'] == $signature );

        $output .= "Security Signature:\n\n";
        $output .= "- posted     = ". $_POST['signature'] ."\n";
        $output .= "- calculated = ". $signature ."\n";
        $output .= "- result     = ". ( $result ? 'SUCCESS' : 'FAILURE' ) ."\n";
        echo "<pre>";
        var_dump($output);
        echo "</pre>";
        die();
    }

    if( !$pfError )
    {
        $validHosts = array(
            'www.payfast.co.za',
            'sandbox.payfast.co.za',
            'w1w.payfast.co.za',
            'w2w.payfast.co.za',
            );

        $validIps = array();

        foreach( $validHosts as $pfHostname )
        {
            $ips = gethostbynamel( $pfHostname );

            if( $ips !== false )
                $validIps = array_merge( $validIps, $ips );
        }
        $validIps = array_unique( $validIps );

        if( !in_array( $_SERVER['REMOTE_ADDR'], $validIps ) )
        {
            $pfError = true;
            $pfErrMsg = PF_ERR_BAD_SOURCE_IP;
        }
    }

    if( !$pfError )
    {
        if( function_exists( 'curl_init' ) )
        {
            $output .= "\n\nUsing cURL\n\n";
            $ch = curl_init();
            $curlOpts = array(
                CURLOPT_USERAGENT => USER_AGENT,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HEADER => false,
                CURLOPT_SSL_VERIFYHOST => 2,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_URL => 'https://'. $pfHost . '/eng/query/validate',
                CURLOPT_POST => true,
                CURLOPT_POSTFIELDS => $pfParamString,
                );
            curl_setopt_array( $ch, $curlOpts );
            $res = curl_exec( $ch );
            curl_close( $ch );

            if( $res === false )
            {
                $pfError = true;
                $pfErrMsg = PF_ERR_CURL_ERROR;
            }
        }
        else
        {
            $output .= "\n\nUsing fsockopen\n\n";
            $header = "POST /eng/query/validate HTTP/1.0\r\n";
            $header .= "Host: ". $pfHost ."\r\n";
            $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
            $header .= "Content-Length: " . strlen( $pfParamString ) . "\r\n\r\n";
            $socket = fsockopen( 'ssl://'. $pfHost, 443, $errno, $errstr, 10 );
            fputs( $socket, $header . $pfParamString );
            $res = '';
            $headerDone = false;

            while( !feof( $socket ) )
            {
                $line = fgets( $socket, 1024 );
                if( strcmp( $line, "\r\n" ) == 0 )
                {
                    $headerDone = true;
                }
                else if( $headerDone )
                {
                    $res .= $line;
                }
            }
        }
    }

    if( !$pfError )
    {
        $lines = explode( "\n", $res );
        $output .= "\n\nValidate response from server:\n\n";
        foreach( $lines as $line )
            $output .= $line ."\n";
    }

    if( !$pfError )
    {
        $result = trim( $lines[0] );
        $output .= "\nResult = ". $result;

        if( strcmp( $result, 'VALID' ) == 0 )
        {

        }
        else
        {
            $pfError = true;
            $pfErrMsg = PF_ERR_INVALID_DATA;
        }
    }

    if( $pfError )
    {
        $output .= "\n\nAn error occurred!";
        $output .= "\nError = ". $pfErrMsg;
    }

    echo "<pre>";
    var_dump($pfError);
    echo "</pre>";
    file_put_contents( $filename, $output );
}
?>

Также я следую коду: PayFast-Платежный шлюз

Проблема в том, что я не получаю никакого ответа. Как я могу получить ответ?

Author: Jatin Raikwar, 2016-12-12

2 answers

Если ваш notify_url не возвращает ответ header 200, вы не сможете получить возвращаемые переменные от PayFast.

Ниже приведен минимальный пример кода ITN:

// Notify PayFast that information has been received - this is required
header( 'HTTP/1.0 200 OK' );
flush();

// Posted variables from ITN -the return variables
$pfData = $_POST;

// Update db
switch( $pfData['payment_status'] ){
  case 'COMPLETE':
     // If complete, update your application                   
     break;
  case 'FAILED':                    
     // There was an error, update your application
     break;
  default:
     // If unknown status, do nothing (safest course of action)
     break;
}

Посмотрите мой ответ на этот вопрос для получения информации.

 1
Author: Daniel_ZA, 2017-05-23 12:08:40

Пожалуйста, введите эти три URL -адреса, уведомите о возврате, отмените

 0
Author: Wiki, 2016-12-15 05:36:34