Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error 431 using Flask (render_template) #6

Open
blackhold opened this issue Nov 28, 2018 · 0 comments
Open

Error 431 using Flask (render_template) #6

blackhold opened this issue Nov 28, 2018 · 0 comments
Labels

Comments

@blackhold
Copy link

blackhold commented Nov 28, 2018

I share the problem that I had some days ago, finally I solved! :) I tried to contact with redsys support and told me that they don't offer support of 3rd code... so then here you have the solution of my problem:

The function prepare_request from Client, returns values as binary b'string', if you send it directly to template when using Flask and render_template, the variable of hidden fields converts b'string' to HTML b'string' to avoid that before passing variables to render_template you have to convert them to unicode (or string)

     _redsys = {
     "Ds_MerchantParameters":_redsys2.get("Ds_MerchantParameters").decode('utf-8'),        
     "Ds_SignatureVersion":_redsys2.get("Ds_SignatureVersion"),
     "Ds_Signature":_redsys2.get("Ds_Signature").decode('utf-8')
     }

I will send to render_template the variable called _redsys

Here you have the example code using python3.5 + flask + render_template + boostrap

Add to file views.py

from decimal import Decimal as D, ROUND_HALF_UP
from redsys import currencies, languages, transactions
from redsys.client import RedirectClient

@app2.route('/order')
def order():
    _error = "0"
    form = ""

    _data = {'url' : 'https://sis-t.redsys.es:25443/sis/realizarPago' }

    secret_key = u'XXX'
    sandbox = True
    client = RedirectClient(secret_key, sandbox)
    request = client.create_request()
    request.merchant_code = u'XXX'
    request.terminal = u'1'
    request.transaction_type = transactions.STANDARD_PAYMENT
    request.currency = currencies.EUR
    request.order = u'000000001'
    # The amount must be defined as decimal and pre-formated with only two decimals
    request.amount = D('10.56489').quantize(D('.01'), ROUND_HALF_UP)
    request.merchant_data = 'merchant data for tracking purpose like order_id, session_key, ...'
    request.merchant_name = "your company name"
    request.titular = "customer name"
    request.product_description = "product description"
    request.merchant_url = "https://sis-t.redsys.es:25443/sis/realizarPago"
    _redsys2 = client.prepare_request(request)

    _redsys = {
    "Ds_MerchantParameters":_redsys2.get("Ds_MerchantParameters").decode('utf-8'),
    "Ds_SignatureVersion":_redsys2.get("Ds_SignatureVersion"),
    "Ds_Signature":_redsys2.get("Ds_Signature").decode('utf-8')
    }

    return render_template('pages/order.html', form=form, error=_error, data=_data, redsys=_redsys)

Then in template file pages/order.html:

<form action="{{ data['url'] }}" method="post" role="form">
    <input type="hidden" name="Ds_SignatureVersion" value="{{ redsys['Ds_SignatureVersion'] }}"/>
    <input type="hidden" name="Ds_MerchantParameters" value="{{ redsys['Ds_MerchantParameters'] }}"/>
     <input type="hidden" name="Ds_Signature" value="{{ redsys['Ds_Signature'] }}"/>
     <button type="submit" class="btn btn-default">Submit Button</button>
     <button type="reset" class="btn btn-default">Reset Button</button>
</form>

Take care to install using pip3 install the missing modules

I wish it could be helpful to someone some day :)

Thanks you much!

@ddiazpinto ddiazpinto added the bug label Nov 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants