Giter Site home page Giter Site logo

redsys-polite's Introduction

npm version GitHub version NPM

redsys-polite

Redsys payment gateway compatible with ES6. It implements the new HMAC-SHA256 request signing procedure.

Install package

You can install the package with npm:

npm install --save redsys-polite

Import package using ES6:

import {RedsysBuilder, PaymentBuilder} from 'redsys-polite';

With node:

var RedsysBuilder = require('redsys-polite').RedsysBuilder;
var PaymentBuilder = require('redsys-polite').PaymentBuilder;

How to use it

Generate form parameters with the following code.

const commerce_code = "<your_commerce_code>";
const secret_code = "<your_secret_key>";

const redsys = new RedsysBuilder()
    .setMerchantCode(commerce_code)
    .setName("Faable.com")
    .setTitular("Marc Pomar")
    .setSecret(secret_code)
    .enableDebug()
    .build();

const payment = new PaymentBuilder()
    .setTotal(3.20)
    .setOrderId("1")
    .setUrlMerchant("http://faable.com/notify")
    .setUrlCancel("http://faable.com/cancel")
    .setUrlOK("http://faable.com/accept")
    .build();

const form_encoded_params = redsys.getFormData(payment);

Then form_encoded_params will have the required properties to generate a payment form. Use this object to

{ redsys_url: 'https://sis-t.redsys.es:25443/sis/realizarPago',
  Ds_SignatureVersion: 'HMAC_SHA256_V1',
  Ds_MerchantParameters: '<encoded merchant parameters>',
  Ds_Signature: '<signature>' }

Form example using React:

  <form action={sign_payment.redsys_url} method="POST" ref="payform" >
    <input type="hidden" name="Ds_SignatureVersion" value={sign_payment.Ds_SignatureVersion}/>
    <input type="hidden" name="Ds_MerchantParameters" value={sign_payment.Ds_MerchantParameters}/>
    <input type="hidden" name="Ds_Signature" value={sign_payment.Ds_Signature}/>
  </form>

Decode POST notification received from payment server

With initialized redsys object using RedsysBuilder call the method like this: var Ds_Signature = req.body.Ds_Signature; var Ds_MerchantParameters = req.body.Ds_MerchantParameters; redsys.decodeNotifiedMerchantParams(Ds_Signature, Ds_MerchantParameters) .then((decodedParams) => console.log(decodedParams)) .catch(e => console.log(e)); // Catch error for invalid signature

_decodeNotifiedMerchantParams returns a promise that is rejected if signature is not valid. Remember to catch it like the shown example.

Redsys server notification

Convert response DS_Code to a message string: import {codeToMessage} from 'redsys-polite'; var obj = codeToMessage('9915'); // Example Ds_Response code can be 9915, user canceled the payment; console.log(obj.message); // Prints text description for this code

Author

Marc Pomar Torres

redsys-polite's People

Contributors

boyander avatar cesarcf avatar sergiojup avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

redsys-polite's Issues

various errors

Hey, thanks for the work you did. It helped me understand the subject better.
I installed your library and I get that error when I try to pay.
The amount and the order number are not arriving (the data are harcoded).
How can I insert this window in my website so that it is in?

Any suggestion?

Thank you

image

Not creating the right merchantParameters

Hello,

Maybe some of you can help me with this. I am having an issue with this package and all NPM packages I have tried. When submitting the form I always get "Error en datos enviados. Contacte con su comercio. Importe 0 Sin Asignar " because merchantParameters and signature are not right.

Using the same FUC and SECRET in a PHP environment using Redsys API PHP doesn't give me any problem at all.

This is my code

const order = new Date().getTime().toString();
                const total = parseInt(this.getTotal) * 100;

                const redsys = new RedsysBuilder()
                    .setMerchantCode(process.env.VUE_APP_REDSYS_FUC)
                    .setName(process.env.VUE_APP_NAME)
                    .setTitular(process.env.VUE_APP_NAME)
                    .setSecret(process.env.VUE_APP_REDSYS_SECRET)
                    .enableDebug()
                    .build();

                const payment = new PaymentBuilder()
                    .setTotal(this.getTotal)
                    .setOrderId("1")
                    .setUrlMerchant(process.env.VUE_APP_URL)
                    .setUrlCancel(process.env.VUE_APP_URL + 'pagament/ko')
                    .setUrlOK(process.env.VUE_APP_URL + 'pagament/ok')
                    .build();

                const form_encoded_params = redsys.getFormData(payment);

Thanks in advance.

Throws TypeError: invalid suite type in ReactJS app

Following the example, when executing getFormData, it triggers TypeError: invalid suite type.

The same code executed in node works correctly.

My workaround:

import { createCipheriv } from 'browserify-cipher'

const encodeOrder = (order_id, secret) => {
    var secretKey = new Buffer(secret, 'base64');
    var iv = new Buffer(8);
    iv.fill(0);
    var cipher = createCipheriv('des-ede3-cbc', secretKey, iv);
    cipher.setAutoPadding(false);
    var res = cipher.update(order_id, 'utf8', 'base64') + cipher.final('base64');
    return res;
  }

const getFormData = (redsys, payment) => {
    // Merchant parameters as a Base64-JSON
    var merchant = new Buffer(JSON.stringify(redsys.generateMerchantParams(payment))).toString('base64');

    // Calculate signature
    var order_encoded = encodeOrder(payment.order_id, redsys.secret);
    var signature = redsys.doSignature(order_encoded, merchant);

    return {
      'redsys_url': redsys.url,
      'Ds_SignatureVersion': 'HMAC_SHA256_V1',
      'Ds_MerchantParameters': merchant,
      'Ds_Signature': signature,
    };
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.