Here is an example for performing a payment in a web shop through Cyclos, using PHP.
To make this example work, some configurations are required in the Cyclos application:
In the web shop itself, two pages are needed. The first one will prepare the payment parameters and will redirect the client into Cyclos, where he can authenticate himself and confirm the payment. The other will receive the flow after the payment confirmation (or cancellation) and ask Cyclos whether the payment has been performed.
It requires the cyclos.php file to be included
<?php
// Get the web service proxy
require_once 'cyclos.php';
$cyclos = new Cyclos();
$webShopService = $cyclos->service('webshop');
// Setup the payment parameters
$params = new stdclass();
$params->amount = 15.23;
$params->description = "Buying new stuff";
$params->clientAddress = $_SERVER['REMOTE_ADDR'];
$params->toUsername = 'a_valid_cyclos_user';
// This should be the absolute url for the page which will process the payment
$params->returnUrl = "http://localhost/cyclos_ws/complete_payment.php";
// Generate the ticket
try {
//Ensure the input parameter is named 'params' and the output, 'return'
$ticket = $webShopService->generate(array('params' => $params))->return;
} catch (SoapFault $e) {
die("Error generating a payment ticket: $e");
}
// With the ticket ok, redirect the client to perform the payment
header( "Location: ".Cyclos::$server_root."/do/webshop/payment?ticket=".$ticket ) ;
?>
<?php
// Get the web service proxy
require_once 'cyclos.php';
$cyclos = new Cyclos();
$webShopService = $cyclos->service('webshop');
// Get the ticket details
try {
//Ensure the input parameter is named 'params' and the output, 'return'
$ticket = $webShopService->get(array('ticket' => $_GET['ticket']))->return;
} catch (SoapFault $e) {
die("Error retrieving payment ticket: $e");
}
// Validate the ticket data
$expected_amount = 15.23;
if ($ticket->awaitingAuthorization) {
die("The payment is awaiting authorization in Cyclos");
} else if (!$ticket->ok) {
die("The ticket was not validated");
} else if ($ticket->amount != $expected_amount) {
die("Wrong ticket: invalid amount");
} else if ($ticket->clientAddress != $_SERVER['REMOTE_ADDR']) {
die("Wrong ticket: unexpected client address");
} else {
echo("The payment has been successfully processed");
}
?>