How to perform payment in a web shop via Cyclos, using PHP

Any issue about Cyclos 4 scripting and Webservices

Moderators: hugo, alexandre, rmvanarkel

Post Reply
seintitus
Posts: 16
Joined: Mon Jun 11, 2012 8:59 am

How to perform payment in a web shop via Cyclos, using PHP

Post by seintitus » Mon Sep 05, 2016 10:32 am

I saw a post on your website describing an example for performing a payment in a web shop through Cyclos, using PHP. I was wondering if this example still apply for the current cyclos version?

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");
}
?>

Post Reply