Code: Select all
prepare_payment.php
<?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 ) ;
?>
complete_payment.php
<?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");
}
?>