27.1.12

Dibs gateway integration php rediect and api

Integrating DIBS payment Gateway(Redirect method and API method)


        Redirect method
  • Integrating DIBS payment Gateway is Simple ,You need to create a test account in dibs and with those details we can integrate dibs in your application
  • Integrating via redirect method or server post method
  • After the saving the buy details just save those in your Database and after that redirect to the below form page with filled details and need to submit
  • Below is the example of sample dibs form

<form action="https://pay.dibspayment.com/" method="post" name="dibs">
<input type="hidden" name="merchant" value="merchant_id" />
<input type="hidden" name="account" value="account-name" />
<input type="hidden" name="amount" value="TotalPrice*100" />
<input type="hidden" name="currency" value="208" />
<input type="hidden" name="orderid" value="unique_id" />
<input type="hidden" name="acceptReturnUrl" value="http://www.yoururl.com/success.php" />
<input type="hidden" name="paytype" value="cardtype" />
<input type="hidden" name="uniqueid" value="uniqueid" />
<input type="hidden" name="callbackurl" value="http://www.yoururl.com/callbackurl.php"/>
<input type="hidden" name="cancelreturnurl" value="http://www.yoururl.com/error.php" />
<input type="hidden" name="language" value="da_DK" />
<input type="hidden" name="test" value="1" />
</form>



  • After the Successfull Payment user will be redirected to our website(accepturl)



$Merchant = "123456";
$MD5 = "qJuH6vjXHLSDB*%€&/hbnkjlBHGhjJKJ";
$test ="yes";
if($_GET['status']=='ACCEPTED'){
$paid = DIBSCapt($Merchant,$_GET['amount'], $_GET['transaction'],$_GET['orderid'],$MD5,$test)
if ($paid==true){Payment Captured}else{Payment Failed;}
}
---------------------------------------------------------------------------
        API Method
  • For API or webservice Way of Payment Integration
  • Just need to call two functions for capturing the Payment
  • First need to authorise the Payment by calling

$trancaction = DIBSAuth($Merchant,$Amount,$Currency,$CardNo, $ExpMon,$ExpYear,$CVC,$OrderID,$postype="ssl", $MD5="",$test="no");
$paid = DIBSCapt($Merchant,$amount,$trancaction['transaction'], $trancaction['orderid'],$MD5,$test)
if ($paid==true){Payment Captured}else{Payment Failed;}

---------------------------------------------------------------------------
CODE Include this functions

//!A function for asking DIBS for authorisation of a creditcard payment 
function DIBSAuth($Merchant,$Amount,$Currency,$CardNo,$ExpMon, $ExpYear,$CVC,$OrderID,$postype="ssl",$MD5="",$test="no") {

  //Set up post-variable string
  $postvars = "merchant=".parmchk($Merchant);
  $postvars .= "&orderid=".parmchk($OrderID);
  $postvars .= "&currency=".parmchk($Currency);
  $postvars .= "&amount=".parmchk($Amount);

  //Check if MD5key check is used and add if it is
  if ( count($MD5) == 2 ) {
    $md5Key = md5($MD5['K2'].md5($MD5['K1'].$postvars));
    $postvars .= "&md5key=".$md5Key;
  }

  $postvars .= "&cardno=".parmchk($CardNo);
  $postvars .= "&expmon=".parmchk($ExpMon);
  $postvars .= "&expyear=".parmchk($ExpYear);
  $postvars .= "&cvc=".parmchk($CVC);
  $postvars .= "&textreply=yes";
  $postvars .= "&postype=".parmchk($postype);

  //Add testvar if "yes"
  if ( $test == "yes" ) {
    $postvars .= "&test=yes"; 
  }

  //Send post request
  $response = http_post('payment.architrade.com','/cgi-ssl/auth.cgi', $postvars );

  //Deal with reponse
  $response = explode("&",$response);
  $N = count($response);
  if ( $N < 2 ) { //Response is an error
    $AuthInfo = false;
  } else { //Response is good (does not mean that authorisation was granted)
    $AuthInfo = array(); //Define output array
    while ( $N-- > 0 ) {
      $A = explode("=",$response[$N]);
      $AuthInfo[$A[0]] = $A[1];
    }
  }
  
  //Check that the returned MD5 key is correct
  if ( $AuthInfo['authkey'] <> "" ) {
    $vars = "transact=".$AuthInfo['transact']."&amount=".$Amount."&currency=".$Currency;
    $Control = md5($MD5['K2'].md5($MD5['K1'].$vars));
    if ( $Control <> $AuthInfo['authkey'] ) {
      $AuthInfo = array();
      $AuthInfo['result'] = "MD5key authorisation failed";
    }
  }

  return $AuthInfo;
}

//!A function for asking DIBS for capturing an already authorised creditcard payment
function DIBSCapt($Merchant,$Amount,$Transact,$OrderID,$MD5,$test="no") {

  //Set up post-variable string
  $postvars = "merchant=".parmchk($Merchant);
  $postvars .= "&orderid=".parmchk($OrderID);
  $postvars .= "&transact=".parmchk($Transact);
  $postvars .= "&amount=".parmchk($Amount);

  //Check if MD5key check is used and add if it is
  if ( count($MD5) == 2 ) {
    $md5Key = md5($MD5['K2'].md5($MD5['K1'].$postvars));
    $postvars .= "&md5key=".$md5Key;
  }
  
  $postvars .= "&textreply=yes";
  $postvars .= "&force=true";

  //Add testvar if "yes"
  if ( $test == "yes" ) {
    $postvars .= "&test=yes"; 
  }
  
  //Send post request
  $response = http_post('payment.architrade.com','/cgi-bin/capture.cgi', $postvars );

  //Deal with reponse
  $response = explode("&",$response);
  $N = count($response);
  if ( $N == 1 ) {
    $CaptInfo['result'] = $response[0];
  } else {
    if ( $N < 2 ) { //Response is an error
      $CaptInfo = false;
    } else { //Response is good (does not mean that the capture was successfull)
      $CaptInfo = array(); //Define output array
      while ( $N-- > 0 ) {
    $A = explode("=",$response[$N]);
    $CaptInfo[$A[0]] = $A[1];
      }
    }
  }
  
  return $CaptInfo;
}
//!Function for sending a 'within-php' post-request and receive the response-body as a string
function http_post($host, $path, $data, $auth="") {

  $sock = fsockopen("ssl://".$host, 443, $errno, $errstr, 30);
  if (!$sock) die("$errstr ($errno)\n");  
  
  fwrite($sock, "POST ".$path." HTTP/1.0\r\n");
  fwrite($sock, "Host: ".$host."\r\n");
  fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
  fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
  fwrite($sock, "User-Agent: PHP-DIBS (Kaka Consult) v1.0 \r\n"); 

  //If basic authentication is required (e.g. payinfo.cgi, changestatus.cgi and refund.cgi)
  if ( is_array($auth) ) {
    fwrite($sock, "Authorization: Basic ".base64_encode($auth['username'].":".$auth['password'])."\r\n");
  }
  
  fwrite($sock, "Accept: */*\r\n");
  fwrite($sock, "\r\n");
  fwrite($sock, $data."\r\n");
  fwrite($sock, "\r\n");

  //Take out the header first  
  $headers = "";
  while ( $str = trim(fgets($sock, 4096)) ) {
    $headers .= "$str\n";
  }
      
  //Then collect the body and prepare for returning
  $body = "";
  while ( !feof($sock) ) {
    $body .= fgets($sock, 4096);
  }  
  
  fclose($sock);

  return $body;
}
//!Function for checking that the parameters do not contain any &, which may corrupt message to DIBS
function parmchk($in) {
  return str_replace("&","",$in);
}

No comments:

Post a Comment