Webshop payments with Cyclos 3.6.1 - better sample code

Functionality discussions and feature requests

Moderators: hugo, alexandre, rmvanarkel

Post Reply
simonjwoolf
Posts: 111
Joined: Mon May 28, 2012 8:49 am

Webshop payments with Cyclos 3.6.1 - better sample code

Post by simonjwoolf »

We've recently been testing the webshop payments harness that is avaialble via SOAP webservices (there doesn't seem to be anything similar available via REST, just yet).

This example on the Cyclos wiki is incomplete and does not work, at least on any implementation/configuration that we tried:
http://project.cyclos.org/wiki/index.ph ... op_payment

So, I'm providing an updated example that does work. I hope this will be helpful for others. The only change is to add $params->toUsername to process_payment.php
If this is not present, a ticket is not created as this parameter cannot be null (at least, it throws an error to the webservices log if it is null!)

Code: Select all

<?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 = 'someuser';
// This should be the absolute url for the page which will process the payment
$params->returnUrl = "http://your.test.location/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");
}

//print_r($ticket);

// With the ticket ok, redirect the client to perform the payment
header( "Location: ".Cyclos::$server_root."/do/webshop/payment?ticket=".$ticket ) ;

?>
Note that the commented out print_r statement is not necessary, but you might find it useful if you get the message "Ticket could not be found". Uncomment this line, and comment out the header line (so that the page does not forward to your cyclos install). You can then check to see if a ticket was successfully created or not - the ticket looks a bit like this: 3CSJGTUYL5LJED5P2K03QMINC6BRR3LA
If you get no output, then something went wrong and you should consult the webservices log (NB: It's very useful to first go to settings > log settings in cyclos admin, and crank up the logging level for web services to "detailed logging (detailed parameters)".
---
Simon Woolf
Cyclos Consultant

simonjwoolf
Posts: 111
Joined: Mon May 28, 2012 8:49 am

Re: Webshop payments with Cyclos 3.6.1 - better sample code

Post by simonjwoolf »

I've now made this edit to the wiki page
---
Simon Woolf
Cyclos Consultant

admin
Site Admin
Posts: 1422
Joined: Mon Jan 24, 2005 10:31 am

Re: Webshop payments with Cyclos 3.6.1 - better sample code

Post by admin »

Thanks Simon

Post Reply