<commit_before># Payment Maksukaista
[![Build Status](https://travis-ci.org/[pmpuro]/[payment-maksukaista].png)](https://travis-ci.org/[pmpuro]/[payment-maksukaista])

## Overview
This module can be used for making payments through Maksukaista.fi API.
It is designed to be used with express and its body parser.
You can use callback functions to the responses of payments.
Also, you can have multiple (sub)merchant ids simultaneously.

Example:

	var paymentFormData = handler.createPayment(
		true,
		totalSum,
		merchantId,
		privateKey, 
		function(error, orderId) {
			if(!error) {
				// paid
			}
			else {
				// payment failed
			}
	});

	var url = handler.paymentUrl(paymentFormData.ORDER_NUMBER);
	res.render('pay', { 
		url: url,
		fields: paymentFormData
	});

	// a form is then constructed from payment data and url.
	// an example jade snipped:

    form(id='confirm-payment-form', action=url, method='post')
      each val, p in fields
        input(type='hidden', name=p, value=val)
      input(name='confirm-payment', type='submit', value='Pay now')

## Installation
Installation is simple, install the npm module:

    npm install payment-maksukaista

## Usage
### Initialization
The create function of the module takes an object as the first parameter 
which supplies the supported return addresses required by Maksukaista.
Also, the object provides the URL for test mode and the actual
Maksukaista API.

Here's an example showing how to create an instance 
and configure it with express.

	var app = express();

	app.configure('development', function () {
	  app.set('payment_return_url', 'https://localhost/paid');
	  app.set('payment_cancel_url', 'https://localhost/failed');
	  app.set('payment_pay_url', 'https://www.paybyway.com/e-payments/pay');

	  app.set('payment_demo_return_url', 'https://localhost/paid');
	  app.set('payment_demo_cancel_url', 'https://localhost/failed');
	  app.set('payment_demo_pay_url', 'https://www.paybyway.com/e-payments/test_pay');
	});

	var payment = require('payment-maksukaista');
	var handler = payment.create(app);

### Setup the routes
The app needs to provide routes for the callback addresses 
for Maksukaista to report the result of a payment.

Note that these needs to match the addresses configured in the initialization.

Example routes:

	app.post('/paid', handler.successfulPaymentHandler, function(req, res) {
		// ...
	});

	app.post('/failed', handler.failedPaymentHandler, function(req, res) {
		// ...
	});

### Make the payment
A payment is performent in two phases. 
First, all data is gathered and generated for the payment.
It is archieved by calling the createPayment function.

	var paymentFormData = payment.createPayment(
		true,
		totalSum,
		merchantId,
		privateKey, 
		function(error, orderId) {
			if(!error) {
				// paid
			}
			else {
				// payment failed
			}
	});

The first parameter tells if we want to use the Maksukaista API (true) or <commit_msg>Fixed couple of misspelled words.<commit_after># Payment Maksukaista
[![Build Status](https://travis-ci.org/[pmpuro]/[payment-maksukaista].png)](https://travis-ci.org/[pmpuro]/[payment-maksukaista])

## Overview
This module can be used for making payments through Maksukaista.fi API.
It is designed to be used with express and its body parser.
You can use callback functions to the responses of payments.
Also, you can have multiple (sub)merchant ids simultaneously.

Example:

	var paymentFormData = handler.createPayment(
		true,
		totalSum,
		merchantId,
		privateKey, 
		function(error, orderId) {
			if(!error) {
				// paid
			}
			else {
				// payment failed
			}
	});

	var url = handler.paymentUrl(paymentFormData.ORDER_NUMBER);
	res.render('pay', { 
		url: url,
		fields: paymentFormData
	});

	// a form is then constructed from payment data and url.
	// an example jade snipped:

    form(id='confirm-payment-form', action=url, method='post')
      each val, p in fields
        input(type='hidden', name=p, value=val)
      input(name='confirm-payment', type='submit', value='Pay now')

## Installation
Installation is simple, install the npm module:

    npm install payment-maksukaista

## Usage
### Initialization
The create function of the module takes an object as the first parameter 
which supplies the supported return addresses required by Maksukaista.
Also, the object provides the URL for test mode and the actual
Maksukaista API.

Here's an example showing how to create an instance 
and configure it with express.

	var app = express();

	app.configure('development', function () {
	  app.set('payment_return_url', 'https://localhost/paid');
	  app.set('payment_cancel_url', 'https://localhost/failed');
	  app.set('payment_pay_url', 'https://www.paybyway.com/e-payments/pay');

	  app.set('payment_demo_return_url', 'https://localhost/paid');
	  app.set('payment_demo_cancel_url', 'https://localhost/failed');
	  app.set('payment_demo_pay_url', 'https://www.paybyway.com/e-payments/test_pay');
	});

	var payment = require('payment-maksukaista');
	var handler = payment.create(app);

### Setup the routes
The app needs to provide routes for the callback addresses 
for Maksukaista to report the result of a payment.

Note that these needs to match the addresses configured in the initialization.

Example routes:

	app.post('/paid', handler.successfulPaymentHandler, function(req, res) {
		// ...
	});

	app.post('/failed', handler.failedPaymentHandler, function(req, res) {
		// ...
	});

### Make the payment
A payment is performed in two phases. 
First, all data is gathered and generated for the payment.
It is achieved by calling the createPayment function.

	var paymentFormData = payment.createPayment(
		true,
		totalSum,
		merchantId,
		privateKey, 
		function(error, orderId) {
			if(!error) {
				// paid
			}
			else {
				// payment failed
			}
	});

The first parameter tells if we want to use the Maksukaista API (true) or 
{'fraction_non_alphanumeric': 0.1161650321271559, 'fraction_numerical': 0.0, 'mean_word_length': 3.9873524451939293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 16, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5116829', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1748, 'n_words': 574}