Code Monkey home page Code Monkey logo

tbk's Introduction

tbk - Transbank's Webpay protocol

Attention: Beta not ready for production

Python implementation of Transbank's Webpay protocol.

Latest Version Development Status Build Status Coverage Status Code Health Downloads Documentation Status

Installation

pip install tbk

Usage

Set environment variable for Commerce and initialize.

os.environ['TBK_COMMERCE_ID'] = "597026007976"
os.environ['TBK_COMMERCE_KEY'] = "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEAn3HzPC1ZBzCO3edUCf/XJiwj3bzJpjjTi/zBO9O+DDzZCaMp...""

from tbk.webpay.commerce import Commerce

commerce = Commerce.create_commerce()
# for development purposes you can use
# commerce = Commerce(testing=True)

If you want to set the official webpay log (for certification issues):

from tbk.webpay.logging import configure_logger
from tbk.webpay.logging.official import WebpayOfficialHandler

configure_logger(WebpayOfficialHandler(LOG_BASE_PATH))

Create a new payment and redirect user.

from tbk.webpay.payment import Payment

payment = Payment(
    request_ip='127.0.0.1', # customer request ip
    commerce=commerce,
    success_url='http://localhost:8080/webpay/success/',
    confirmation_url='http://111.122.133.144:8080/webpay/confirmation/', # callback url with IP
    failure_url='http://localhost:8080/webpay/failure/',
    session_id='SOME_SESSION_VALUE',
    amount=123456, # could be int, str or Decimal
    order_id=1,
)
payment.redirect_url

Then to confirm payment, use an endpoint with:

from tbk.webpay.confirmation import Confirmation

def confirm_payment(request):
    confirmation = Confirmation(
        commerce=commerce,
        request_ip=request.ip_address,
        data=request.POST
    )

    # validate_confirmation validate if order_id and amount are valid.
    if confirmation.is_success() and validate_confirmation(confirmation.order_id, confirmation.amount):
        return HttpResponse(commerce.acknowledge)

    return HttpResponse(commerce.reject)

About webpay communication protocol: http://sagmor.com/rants/technical/webpay-communication-protocol/

Development

After cloning the repo:

python setup.py develop

For testing purposes:

python setup.py test

Recommended:

pip install nosy
nosy

Credits

This is a port from ruby implementation http://github.com/sagmor/tbk.

tbk's People

Contributors

pedroburon avatar

Watchers

 avatar

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.