Web services/WebShop payment

From Complete Cyclos documentation wiki
Jump to: navigation, search

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:

  • As an administrator
    • Decide which password will be used for web shop payments. Under Settings > Channels menu, enter the WebShop channel. You may check 'use pin' or not. When pin is used, each member has to access their Personal > External access menu and type a pin. Otherwise, the same login password (and transaction password, if enabled) will be used for the payment.
    • Make sure there is one payment type from member to member enabled to the WebShop payment. By default, the 'external payment' is.
    • Grant permission to a member group to that payment type
    • Register a web service client, with the ip address of the web shop server. When using dynamic DNS, the hostname may be used as well. Type the member which owns the web shop. Also, grant permission to WebShop payments.
  • As the web shop client (not the owner member)
    • If pin is used for web shop payment, the client needs a pin. To do this, access the Personal > External access menu. Set a pin and save it.
    • Check the WebShop channel and save it.

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

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