Creating a formToken
The first step for displaying a payment form is to create a formToken.
Preparing your environment
If you use PHP with our SDK, we recommend to store your keys in a configuration file.
Example with test keys:
<doc-param name="php-link">https://github.com/lyra/rest-php-examples/blob/master/www/keys.php</doc-param>
<?php /** * Get the client */ require_once __DIR__ . '/vendor/autoload.php'; /** * Define configuration */ /* Username, password and endpoint used for server to server web-service calls */ Lyra\Client::setDefaultUsername("69876357"); Lyra\Client::setDefaultPassword("testpassword_DEMOPRIVATEKEY23G4475zXZQ2UA5x7M"); Lyra\Client::setDefaultEndpoint("https://api.payzen.eu"); /* publicKey and used by the javascript client */ Lyra\Client::setDefaultPublicKey("69876357:testpublickey_DEMOPUBLICKEY95me92597fd28tGD4r5"); /* Javascript content delivery server */ Lyra\Client::setDefaultClientEndpoint("https://static.payzen.eu"); /* SHA256 key */ Lyra\Client::setDefaultSHA256Key("38453613e7f44dc58732bad3dca2bca3");
Make sure you replace them with your personal keys.
For more information, see the following articles: Using our SDKs and Obtaining my keys.
Creating the formToken
When a Buyer finalizes their purchase on your website, you must validate their transaction on your merchant server, making sure to check the amount, currency, cart contents, etc.
Once these verification has been completed, your merchant server must call the Charge/CreatePayment Web Service in order to initialize the transaction.
In response, your merchant server retrieves a formToken, an encrypted object allowing to initialize the embedded form with the transaction details and the details corresponding to your shop configuration.
{ "amount": 990, "currency": "EUR", "orderId": "myOrderId-999999", "customer": { "email": "sample@example.com" } }
{ "amount": 1500, "currency": "PEN", "orderId": "myOrderId-999999", "more": "parameters", "customer": { "email": "sample@example.com" } }
{ "amount": 20000, "currency": "ARS", "orderId": "myOrderId-999999", "more": "parameters", "customer": { "email": "sample@example.com" } }
{ "amount": 100000, "currency": "COP", "orderId": "myOrderId-999999", "more": "parameters", "customer": { "email": "sample@example.com" } }
{ "amount": 2500, "currency": "BRL", "orderId": "myOrderId-999999", "more": "parameters", "customer": { "email": "sample@example.com" } }
/** * I initialize the PHP SDK */ require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/keys.php'; require_once __DIR__ . '/helpers.php'; /** * Initialize the SDK * see keys.php */ $client = new Lyra\Client(); /** * I create a formToken */ $store = array("amount" => 250, "currency" => "EUR", "orderId" => uniqid("MyOrderId"), "customer" => array( "email" => "sample@example.com" )); $response = $client->post("V4/Charge/CreatePayment", $store); /* I check if there are some errors */ if ($response['status'] != 'SUCCESS') { /* an error occurs, I throw an exception */ display_error($response); $error = $response['answer']; throw new Exception("error " . $error['errorCode'] . ": " . $error['errorMessage']); } /* everything is fine, I extract the formToken */ $formToken = $response["answer"]["formToken"]; ?>
/** * I initialize the PHP SDK */ require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/keys.php'; require_once __DIR__ . '/helpers.php'; /** * Initialize the SDK * see keys.php */ $client = new Lyra\Client(); /** * I create a formToken */ $store = array("amount" => 250, "currency" => "EUR", "orderId" => uniqid("MyOrderId"), "customer" => array( "email" => "sample@example.com" )); $response = $client->post("V4/Charge/CreatePayment", $store); /* I check if there are some errors */ if ($response['status'] != 'SUCCESS') { /* an error occurs, I throw an exception */ display_error($response); $error = $response['answer']; throw new Exception("error " . $error['errorCode'] . ": " . $error['errorMessage']); } /* everything is fine, I extract the formToken */ $formToken = $response["answer"]["formToken"]; ?>
You can find more information on the authentication of calls to the REST web service here: Authentication phase.
The response will be:
{ "status": "SUCCESS", "_type": "V4/WebService/Response", "webService": "Charge/CreatePayment", "applicationProvider": "NPS", "version": "V4", "applicationVersion": "4.1.0", "answer": { "formToken": "DEMO-TOKEN-TO-BE-REPLACED", "_type": "V4/Charge/PaymentForm" } }
Once it has been created, you can Create your form.