Create recurring payment

PHP code sample for creating a recurring payment.

Overview

The following PHP code sample includes comments explaining the different elements and parameters involved in the Create transaction API call, specifically when being used to make a recurring payment.

Please also see also our tutorial on the same subject

Code sample

The below example uses Basic Auth authentication

<?php

// The POST URL and parameters
$request =  'https://demo.vivapayments.com/api/transactions/';	// demo environment URL
//$request =  'https://www.vivapayments.com/api/transactions';	// production environment URL

// Your merchant ID and API Key can be found in the 'Security' settings on your profile.
$merchant_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
$api_key = 'xxxxxxxxxxxxx';

//Set the ID of the Initial Transaction
$request .= 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';

//Set the Payment Amount
$amount = 100;	// Amount in cents

$postargs = 'Amount='.$amount;

// Get the curl session object
$session = curl_init($request);

// Set query data here with the URL
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_USERPWD, $merchant_id.':'.$api_key);

$response = curl_exec($session);
curl_close($session);

// Parse the JSON response
try {
	$resultObj=json_decode($response);
} catch( Exception $e ) {
	echo $e->getMessage();

}

if ($resultObj->ErrorCode==0){
	// print JSON output
echo json_encode($resultObj);
}
else{
	echo $resultObj->ErrorText;
}

?>

Get Support

If you would like to integrate with Viva, or if you have any queries about our products and solutions, please see our Contact & Support page to see how we can help!