====== PHP XML Payments ======
Below you can find sample code for **Payments** page in **PHP**. You should also use this [[developer:integration_docs:testing-guide|Testing Guide]], which also contains test card details.
The sample code below requires the **{{:developer:sample_codes:gateway_xml_php_api092102.zip|PHP XML API}}**.
**Settings file (nuvei_account.inc):**
----
\\
**Authorisation:**
SetNonSecureCardCardInfo($cardExpiry,$cardHolderName);
if($cvv != "") $auth->SetCvv($cvv);
if($cardCurrency != "" && $cardAmount != "" && $conversionRate != "") $auth->SetForeignCurrencyInformation($cardCurrency,$cardAmount,$conversionRate);
if($email != "") $auth->SetEmail($email);
if($mobileNumber != "") $auth->SetMobileNumber($mobileNumber);
if($description != "") $auth->SetDescription($description);
if($issueNo != "") $auth->SetIssueNo($issueNo);
if($address1 != "" && $address2 != "" && $postcode != "") $auth->SetAvs($address1,$address2,$postcode);
if($country != "") $auth->SetCountry($country);
if($phone != "") $auth->SetPhone($phone);
if($mpiref != "") $auth->SetMpiRef($mpiref);
if($deviceId != "") $auth->SetDeviceId($deviceId);
if($multicur) $auth->SetMultiCur();
if($autoready) $auth->SetAutoReady($autoready);
if($isMailOrder) $auth->SetMotoTrans();
# Perform the online authorisation and read in the result
$response = $auth->ProcessRequestToGateway($secret,$testAccount, $gateway);
$expectedResponseHash = md5($terminalId . $response->UniqueRef() . ($multicur == true ? $currency : '') . $amount . $response->DateTime() . $response->ResponseCode() . $response->ResponseText() . $secret);
if($response->IsError()) echo 'AN ERROR OCCURED! You transaction was not processed. Error details: ' . $response->ErrorString();
elseif($expectedResponseHash == $response->Hash()) {
switch($response->ResponseCode()) {
case "A" : # -- If using local database, update order as Authorised.
echo 'Payment Processed successfully. Thanks you for your order.';
$uniqueRef = $response->UniqueRef();
$responseText = $response->ResponseText();
$approvalCode = $response->ApprovalCode();
$avsResponse = $response->AvsResponse();
$cvvResponse = $response->CvvResponse();
break;
case "R" :
case "D" :
case "C" :
case "S" :
default : # -- If using local database, update order as declined/failed --
echo 'PAYMENT DECLINED! Please try again with another card. Bank response: ' . $response->ResponseText();
}
} else {
$uniqueReference = $response->UniqueRef();
echo 'PAYMENT FAILED: INVALID RESPONSE HASH. Please contact ' . $adminEmail . ' or call ' . $adminPhone . ' to clarify if you will get charged for this order.';
if(isset($uniqueReference)) echo 'Please quote Nuvei Terminal ID: ' . $terminalId . ', and Unique Reference: ' . $uniqueReference . ' when mailing or calling.';
}
?>
----
\\
**Perform a Refund** (standard refunds can only be performed against authorised sale transactions that have already been put through the same account system. Also, the Order ID of the original sale must be unique.):
SetUniqueRef($uniqueRef);
if($autoready) $refund->SetAutoReady($autoready);
# Perform the refund and read in the result
$response = $refund->ProcessRequestToGateway($secret,$testAccount, $gateway);
$expectedResponseHash = md5($terminalId . $response->UniqueRef() . ($multicur == true ? $currency : '') . $amount . $response->DateTime() . $response->ResponseCode() . $response->ResponseText() . $secret);
if($response->IsError()) echo 'AN ERROR OCCURED! You refund was not processed. Error details: ' . $response->ErrorString();
elseif($expectedResponseHash == $response->Hash()) {
switch($response->ResponseCode()) {
case "A" : # -- If using local database, update order as (partially) Refunded.
echo 'Refund Processed successfully.';
$responseText = $response->ResponseText();
break;
case "R" :
case "D" :
case "C" :
case "S" :
default : # -- If using local database, update order as declined/failed --
echo 'REFUND DECLINED!';
}
} else {
echo 'REFUND FAILED: INVALID RESPONSE HASH. Please contact ' . $adminEmail . ' or call ' . $adminPhone . ' to clarify if you will get refunded for this order.';
$uniqueRef = $response->UniqueRef();
if(isset($uniqueRef)) echo 'Please quote Nuvei Terminal ID: ' . $terminalId . ', and Unique Reference: ' . $uniqueRef . ' when mailing or calling.';
}
?>