. . .

Paymill Module

images/download/attachments/27164698/db3-module-paymill.PNG

You can use Paymill to make online payments easy. This module is provided as JavaScript library and accessible in our Server Code v.1 ApiOmat module. The library is based on the paymill-js project. This project makes it easy to access the different facets (offer, subscription, etc.) of payment. In the following examples we will show you some basic operations. If you need more documentation then please see paymill-js project and paymill API documentation.

Usage

To interact with paymill backend you need two ApiOmat modules: Server Code and Paymill.

Add them in your App Setup Screen in the dashboard.

Initialization

In your Server Code you include the paymill javascript library with the following command:

var paymill = require('paymill.apiomat');

After this you can access all paymill functions. To send requests to the paymill API you need an API Key. This key is located on the paymill website at My Account – Settings. To set the key call execute

paymill.initialize("<your_key>");

in your Server Code.

Services

Each endpoint of the paymill API has a corrseponding service. For example, to access client functions you can use paymill.clients.

Examples

In the following sections we will show you some examples how you can use paymill in Server Code.

Create a paymill client

To create a client in the paymill client we have to use the create method from paymill.clients. We create a new one with an email address

test@apiomat.com

and a description

A new test client

with the following command:

var client = paymill.clients.create("test@apiomat.com", "A new test client");

You can handle response and errors by chaining then functions:

var client = paymill.clients.create("test@apiomat.com", "A new test client").then(function(result) {
AOM.log("Client: " + result);
}).then(function() {
AOM.log("Created client");
}, function(err) {
AOM.log("Request Error: " + err);
});

Get details of a client

To retrieve details of a client call the following statement with a valid clientId.

paymill.clients.detail(clientId).then(function (client) {
AOM.log("Received client details: " + client.name);
});

Create a new payment

A payment object is a representation of a credit card or direct debit payment.

Once you created a payment object, you can use the payment to create multiple transactions.

To create a new payment object for a client you need two things:

  • A valid payment token (You must create this token on your client side and transfer it to your Server Code. See paymill js bridge how you get a valid token or use the native mobile SDKs )

  • A valid client object (see above) or a client id

With this information you can create a new payment for a given client (clientDetail).

paymill.payments.create(paymentToken, client).then(function(payment) {
AOM.log("Attached payment " + payment);
}).then(function(result) {
AOM.log("Finished Payment");
}, function(err) {
AOM.throwException("Error on pay: " + err);
});

Create a new transaction

The following method allows to create a new transaction with given payment details (paymentDetail) and client details or client ID (clientDetail).

paymill.transactions.createWithPayment(paymentDetail, '1500', 'EUR', 'A new test subscription', clientDetail);

Create a new offer

Create a new offer with the following statement. This offer will cost 15 Euros and will be available every month.

paymill.offers.create('1500', 'EUR', '1 MONTH', 'A test offer');

To subscribe as client to a new offer you need offer details or id (offerDetail), payment details or id (payment) and you client details or id (clientDetail)

paymill.subscriptions.create(offerDetail, payment, clientDetail);

Complex example

Let’s create a new transaction for a existing client with a new payment.

We assume that we are created 2 classes in our dashboard ‘Client’ and ‘Item’.

‘Item’ Attributes:

  • name – String: name of the item

  • price – Number: price of the item

‘Client’ class has following attributes:

  • foreignId – Contains the paymill client ID of existing client

  • payToken – String: Here you have to save your payment token, from the paymill javascript bridge (see also ‘Create a new payment’ above; Notice that this token is only valid for 5 minutes)

  • shoppedItems – Collection of references from class ‘Item’

We execute this request in our Server Code for class ‘Client’.

The Server Code function aom_afterPostRef will be executed when a reference to an object of class ‘Client’ is added .

The if statement in line 3 checks that transactions are only executed if a reference between a ‘Client’ and an ‘Item’ object was created.

function aom_afterPostRef(obj, referencedObject, referenceName)
{
/* Let's pay the shopped item */
if (referenceName === "shoppedItems") {
var wait = true;
var clientId = obj.foreignId + "";
var payToken = obj.payToken + "";
 
var clientDetail = undefined;
var item = referencedObject;
var description = "Test Buy for " + item.name;
var amount = item.price / 1; //To make sure that price is a number
paymill.payments.create(payToken, clientId).then(function(payment) {
AOM.log("Payment: " + payment.id);
return paymill.transactions.createWithPayment(payment, amount, "EUR", description, clientDetail);
}).then(function(transaction) {
/* Verify data */
AOM.log("Description: " + transaction.description + " == " + description);
}).then(function(result) {
AOM.log("Finished transaction"); wait = false;
}, function(err) {
wait = false;
AOM.throwException("Error on pay: " + err);
});
/* Let's wait until async requests are finished
* to see all logs in logging window
*/
while(wait) { }
}
}
* link only available in Enterprise Documentation