2C2Pv1.8.0

Refund transaction via API

Refund your settled transaction via API call.
Download full demo PHP code

Prepare refund request on backend code

Import http post & pkcs7 encryption classes

<?php
    include_once('HTTP.php');
    include_once('pkcs7.php');

Set account credentials.

    //Merchant Account Information
    $merchantID = "JT01";        //Get MerchantID when opening account with 2C2P
    $secretKey  = "7jYcp4FxFdf0";    //Get SecretKey from 2C2P PGW Dashboard

Set transaction information.

    //Transaction Information
    $timeStamp = time();
    $apiVersion = "2.1";
    $invoiceNo = "Invoice1401872337";    //Transaction invoice number
    $processType = "R";        
    $actionAmount = "000000001100";    //Required only when doing Refund or Capture request type. Format: 12 digit.
    $stringToHash = $apiVersion . $merchantID . $invoiceNo . $actionAmount . $processType; 
    $hash = strtoupper(hash_hmac('sha1', $stringToHash ,$secretKey, false));      //Calculate Hash Value
Process Type Description
I Inquiry request
V Void request
R Refund request
S Settlement request

Construct payment request message.

    //Construct payment request message  
    $xml = "<PaymentProcessRequest>
        <version>$apiVersion</version>
        <timeStamp>$timeStamp</timeStamp>
        <merchantID>$merchantID</merchantID>
        <invoiceNo>$invoiceNo</invoiceNo>
        <actionAmount>$actionAmount</actionAmount>
        <processType>$processType</processType>
        <hashValue>$hash</hashValue>
        </PaymentProcessRequest>";  

Encrypt request message.

    //Encrypt request
    $pkcs7 = new pkcs7();
    $payload = $pkcs7->encrypt($xml,"./keys/demo2.crt"); 

Submit request.

    //Send request
    $http = new HTTP();
    $response = $http->post("https://demo2.2c2p.com/2C2PFrontend/PaymentActionV2/PaymentAction.aspx","paymentRequest=".$payload); 

Read and decrypt response message.

    //Decrypt response and display  
    $response = $pkcs7->decrypt($response,"./keys/demo2.crt","./keys/demo2.pem","2c2p");   
    echo "Response:<br/><textarea style='width:100%;height:80px'>". $response."</textarea>"; 

Compare response hash value.

    //Compare response Hash
    $resXml=simplexml_load_string($response); 
    $apiVersion = $resXml->version;
    $respCode = $resXml->respCode;
    $pan = $resXml->pan;
    $amt = $resXml->amt;
    $invoiceNo = $resXml->invoiceNo;
    $tranRef = $resXml->tranRef;
    $approvalCode = $resXml->approvalCode;
    $eci = $resXml->eci;
    $dateTime = $resXml->dateTime;
    $status = $resXml->status;
    $failReason = $resXml->failReason;
    $stringToHash = $apiVersion . $respCode . $pan . $amt . $invoiceNo . $tranRef . $approvalCode . $eci . $dateTime . $status . $failReason; 
    $responseHash = strtoupper(hash_hmac('sha1',$stringToHash,$secretKey, false));      //Calculate response Hash Value 
    echo "<br/>hash: $responseHash<br/>";  
    if($resXml->hashValue == $responseHash){ echo "valid response"; } 
    else{ echo "invalid response"; } 
?>

Complete Code

Copy & Paste below code to 'payment_inquiry.php' file, and put this file in your Web Server.

/payment_inquiry.php
<?php
    include_once('HTTP.php');
    include_once('pkcs7.php');
  
    //Merchant Account Information
    $merchantID = "JT01";        //Get MerchantID from 2c2p PGW Dashboard
    $secretKey = "7jYcp4FxFdf0";    //Get SecretKey from 2c2p PGW Dashboard

    //Request Information
    $timeStamp = time();
    $apiVersion = "2.1";
    $invoiceNo = "Invoice1401872337";    //Transaction invoice number 
    /* 
    Process Type:
        I = transaction inquiry
        V = transaction void
        R = transaction Refund
        S = transaction Settlement 
    */
    $processType = "R";        
    $actionAmount = "000000001100";    //Required only when doing Refund or Capture request type. Format: 12 digit.
    $stringToHash = $apiVersion . $merchantID . $invoiceNo . $actionAmount . $processType; 
    $hash = strtoupper(hash_hmac('sha1', $stringToHash ,$secretKey, false));      //Calculate Hash Value 

    //Construct request message
    $xml = "<PaymentProcessRequest>
        <version>$apiVersion</version>
        <timeStamp>$timeStamp</timeStamp>
        <merchantID>$merchantID</merchantID>
        <invoiceNo>$invoiceNo</invoiceNo>
        <actionAmount>$actionAmount</actionAmount>
        <processType>$processType</processType>
        <hashValue>$hash</hashValue>
        </PaymentProcessRequest>";  
 
    //Encrypt request
    $pkcs7 = new pkcs7();
    $payload = $pkcs7->encrypt($xml,"./keys/demo2.crt"); 
     
    //Send authorization request
    $http = new HTTP();
     $response = $http->post("https://demo2.2c2p.com/2C2PFrontend/PaymentActionV2/PaymentAction.aspx","paymentRequest=".$payload);
     
    //Decrypt response and display  
    $response = $pkcs7->decrypt($response,"./keys/demo2.crt","./keys/demo2.pem","2c2p");    
    echo "Response:<br/><textarea style='width:100%;height:80px'>". $response."</textarea>"; 
    
    //Compare response Hash
    $resXml=simplexml_load_string($response); 
    $apiVersion = $resXml->version;
    $respCode = $resXml->respCode;
    $pan = $resXml->pan;
    $amt = $resXml->amt;
    $invoiceNo = $resXml->invoiceNo;
    $tranRef = $resXml->tranRef;
    $approvalCode = $resXml->approvalCode;
    $eci = $resXml->eci;
    $dateTime = $resXml->dateTime;
    $status = $resXml->status;
    $failReason = $resXml->failReason;
    $stringToHash = $apiVersion . $respCode . $pan . $amt . $invoiceNo . $tranRef . $approvalCode . $eci . $dateTime . $status . $failReason; 
    $responseHash = strtoupper(hash_hmac('sha1',$stringToHash,$secretKey, false));      //Calculate response Hash Value 
    echo "<br/>hash: $responseHash<br/>";  
    if($resXml->hashValue == $responseHash){ echo "valid response"; } 
    else{ echo "invalid response"; }
?>

Download full demo PHP code