Code Monkey home page Code Monkey logo

payplug-nodejs's Introduction

payplug-nodejs

An NodeJS API for Payplug solution : https://www.payplug.com

This module aims to implement the Curl Payplug API describe here : https://www.payplug.com/docs/api

It is used as payment solution of the StartUp Clouderial (online project management, invoices, quotation and tasks for independant workers)

Install

$ npm install payplug-nodejs

Authenticate against PayPlug API

To authenticate :

var PayPlugAPI = require('payplug-nodejs').PayPlugAPI;

var payplugapi = new PayPlugAPI('mySecretKey', {
    // the url called in case of success
    sucessReturnUrl: 'https://[my-url]/payplugapi/test/success?tracker=',
    
    // the url called in case of cancelation
    cancelReturnUrl: 'https://[my-url]/payplugapi/test/cancel?tracker=',
    
    // the notification url. Cf. https://www.payplug.com/docs/api/apiref.html#notifications
    notificationUrl: 'https://[my-url]/payplugapi/test/notifications?tracker='
});

payplugapi.authenticate()
	.then(function (result) {
		// you are authenticated.
		// paypluapi can be use to manage payments
	})
	.fail(function(err){
		log.error('Error while authenticating. Message="%s"', err.message);
	})
	.done();
if (payplugapi.authenticated) {
	// The API is successfully authenticated
}

To test if authentication is successfull :

if (payplugapi.authenticated) {
	// The API is successfully authenticated
}

Payment

To create a payment :

// Cf above to have an authenticated PayplugApi object
// var payplugapi = new PayPlugAPI('mySecretKey');
// payplugapi.authenticate()
// ... 

// Create a new Payment
var Payment = require('payplug-nodejs').Payment;
payment = new Payment(payplugapi, 'paymentid', {
    'amount': 1000,
    'currency': 'EUR',
    'customer': {
        'email': '[email protected]',
        'first_name': 'ClientFirstname',
        'last_name': 'ClientLastname'
    },
    'save_card': false,
    'force_3ds': true
});

// Send the Payment creation
payment.sendCreate()
    .then(function (newPaymentResult) {
        // The API call is successfull
        // newPaymentResult.payment is Payment updated with id and tracker;
        newPaymentResult.getId();
        newPaymentResult.getTracker();
        newPaymentResult.getPaymentUrl();
        newPaymentResult.getPayplugPayment(); // get the original Payplug payment (as defined in Payplug API)
        
        // if payment OK
        if (newPaymentResult.isFailed()) {
            newPaymentResult.getFailure().code;
            newPaymentResult.getFailure().message;
        }
    })
    .fail(function(err){
        // there have been an error during payment creation
        err.message;
    })
    .done();

To retrieve a payment :

Call this method a retrieve an existing Payment from its id

// Cf above to have an authenticated PayplugApi object
// var payplugapi = new PayPlugAPI('mySecretKey');
// payplugapi.authenticate()
// ... 

// Retrieve a Payment with its Id
Payment.retrieve(payplugapi, 'theIDOfPayment')
    .then(function (payment) {
        // The API call is successfull
        // payment updated with id and tracker;
        payment.getId();
        payment.getTracker();
        payment.isPayed();
        payment.isRefunded();
        
        payment.getPayplugPayment(); // get the original Payplug payment (as defined in Payplug API)
        
        // if payment is aborted of failed
        if (newPayemnt.isFailed()) {
            newPayment.getFailure().code;
            newPayment.getFailure().message;
        }
    })
    .fail(function(err){
        // there have been an error during payment creation
        err.message;
    })
    .done();

To abort a payment :

Call this method a abort an existing Payment. Payment should have been send before or retrieved with list or retrieve

// Cf above to have an authenticated PayplugApi object
// var payplugapi = new PayPlugAPI('mySecretKey');
// payplugapi.authenticate()
// ... 
// Cf. above to retrieve a Payment
// Payment.retrieve(payplugapi, 'theIDOfPayment')...
// Payment.list(payplugapi)...

// Abort a Payment with its Id
payemnt.sendAbort()
    .then(function (payment) {
        // The API call is successfull
        payment.getId();
        payment.getTracker();

        payment.getPayplugPayment(); // get the original Payplug payment (as defined in Payplug API)
        
        payment.getFailure().code;
        payment.getFailure().message;
        
        expect(newPayment.getFailure().code).to.equal(Payment.ABORT_STATUS);
    })
    .fail(function(err){
        // there have been an error during payment creation
        err.message;
    })
    .done();

To get a Payment from a Payplug payment (found in notification_url for example)

    function notificationUrl (req, res, next) {
        ...
        var payment = Payment.fromPayplugPayment(req.body);
        ...
        payment.getId();
        payment.getTracker();

        payment.getPayplugPayment(); // get the original Payplug payment (as defined in Payplug API)

        payment.isFailed();
        payment.isPayed();
        payment.isRefunded();
        payment.getFailure().code;
        payment.getFailure().message;
    }

Tests

To launch the test you must first provide a tests/config.json file with you secret key. Example :

{
  "testSecretKey": "sk_test_751BVqbli82hx9hyL5GRGT"
}

payplug-nodejs's People

Contributors

jmcollin78 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jimteva

payplug-nodejs's Issues

How can you pass the paymentID when you create a new payment, since your can retrieve it specifically after having send the payment?

I have seen in your code the following snippet:

// Create a new Payment
var Payment = require('payplug-nodejs').Payment;
payment = new Payment(payplugapi, 'paymentid', {
    'amount': 1000,
    'currency': 'EUR',
    'customer': {
        'email': '[email protected]',
        'first_name': 'ClientFirstname',
        'last_name': 'ClientLastname'
    },
    'save_card': false,
    'force_3ds': true
});

// Send the Payment creation
payment.sendCreate()
    .then(function (newPaymentResult) {
        // The API call is successfull
        // newPaymentResult.payment is Payment updated with id and tracker;
        newPaymentResult.getId();
        newPaymentResult.getTracker();
        newPaymentResult.getPaymentUrl();
        newPaymentResult.getPayplugPayment(); // get the original Payplug payment (as defined in Payplug API)
        
        // if payment OK
        if (newPaymentResult.isFailed()) {
            newPaymentResult.getFailure().code;
            newPaymentResult.getFailure().message;
        }
    })
    .fail(function(err){
        // there have been an error during payment creation
        err.message;
    })
    .done();

How can you pass paymentid in the first function since you can retrive it particularly only after having sent the payment to the server?

Any hint would be great,
thanks

Feature request: refund function

After having abort a payment does the payment refunded? It would be awesome if it's not the case to add this feature.
thanks for your awesome work !

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.