Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How do get instance of payment acquire in controller class

By
Ralph LAvaud
on 5/17/15, 11:39 AM 845 views

Hi all

I am finalizing my implementation of stripe.com payment acquire. I need to get a reference of the payment acquire object in my controller class and I can't seem to get this to work. To give more context, there is a private key in the payment acquirer, and I need to get a hold of it on the server side in the the form feedback method.

I created this function in my payment acquirer class 


def stripe_get_secret_key(self, cr, uid, id, context='None')

   acquirer=self.browse(cr, uid, id, context)

   return acquirer.stripe_publishable_key


How do I call this method from my controller class?


0
Ralph LAvaud
On 5/17/15, 1:58 PM

UPDATE

I got this to work. Turns out the id for the payment acquirer is not pass to the controller class, furthermore, there could potentially be two payment acquirer of the same type for the site (maybe I am not sure). In any case, I am putting the payment acquire ID in the form. When I post, I can get the actual acquire used, then get the private key (which does not change per transaction but should never be put on the form). Once I got the ID it was trivial to get the other stuff.









I tried that but I got  

File "/home/rlavaud/addons/payment_stripe/controllers/main.py", line 24, in transfer_form_feedback

stripe.api_key = self.env['payment.stripe'].stripe_get_secret_key()

AttributeError: 'StripeController' object has no attribute 'env'


I think it has to be something like

request.registry['payment.acquirer'].stripe_get_secret_key(cr, uid, id, context)

but I am not sure of the syntax


I get this error


File "/home/rlavaud/addons/payment_stripe/controllers/main.py", line 23, in transfer_form_feedback
stripe.api_key = request.registry['payment.acquirer'].stripe_get_secret_key(cr, uid, id, context)
File "/opt/odoo/server/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/rlavaud/addons/payment_stripe/models/payment_acquirer.py", line 27, in stripe_get_secret_key
acquirer = self.browse(cr, uid, id, context=context)
File "/opt/odoo/server/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/opt/odoo/server/openerp/models.py", line 5205, in browse
ids = _normalize_ids(arg)
File "/opt/odoo/server/openerp/models.py", line 6069, in _normalize_ids
return tuple(arg)
TypeError: 'builtin_function_or_method' object is not iterable
File "/home/rlavaud/addons/payment_stripe/controllers/main.py", line 23, in transfer_form_feedback
stripe.api_key = request.registry['payment.acquirer'].stripe_get_secret_key(cr, uid, id, context)
File "/opt/odoo/server/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/rlavaud/addons/payment_stripe/models/payment_acquirer.py", line 27, in stripe_get_secret_key
acquirer = self.browse(cr, uid, id, context=context)
File "/opt/odoo/server/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/opt/odoo/server/openerp/models.py", line 5205, in browse
ids = _normalize_ids(arg)
File "/opt/odoo/server/openerp/models.py", line 6069, in _normalize_ids
return tuple(arg)
TypeError: 'builtin_function_or_method' object is not iterable



And here is the code I am calling


Printing the variables cr uid id and context I get this. I think id is undefined, where would I get id from?



2015-05-17 18:28:51,121 14605 INFO demo openerp.addons.payment_stripe.controllers.main: Print cr <openerp.sql_db.Cursor object at 0x7fbb46c25250>

2015-05-17 18:28:51,122 14605 INFO demo openerp.addons.payment_stripe.controllers.main: Print uid 1

2015-05-17 18:28:51,122 14605 INFO demo openerp.addons.payment_stripe.controllers.main: Print id <built-in function id>

2015-05-17 18:28:51,122 14605 INFO demo openerp.addons.payment_stripe.controllers.main: Print context {'lang': u'en_US', 'tz': u'Europe/Brussels', 'uid': 1}

2015-05-17 18:28:51,133 14605 INFO demo werkzeug: 127.0.0.1 - - [17/May/2015 18:28:51] "POST /payment/stripe/feedback HTTP/1.0" 500 -


def transfer_form_feedback(self, **post):
cr, uid, context = request.cr, SUPERUSER_ID, request.context
_logger.info('Beginning form_feedback with post data %s', pprint.pformat(post)) # debug
stripe.api_key = request.registry['payment.acquirer'].stripe_get_secret_key(cr, uid, id, context)
resp = stripe.Charge.create (
amount= post.pop('data-amount'),
currency=post.pop('data-currency'),
source=post.pop('data-token'),
description=post.pop('data-description')
)
_logger.info('Stripe response: %s', pprint.pformat(resp)) # debug
request.registry['payment.transaction'].form_feedback(cr, uid, post, 'stripe', context)
return werkzeug.utils.redirect(post.pop('return_url', '/'))




yes, it should works if you don't forget to import request. The cr, uid, id, context depends if you works with new api or not... Be aware: context=None and not context='None'... Do you have an error that you have some doubts about the synthax?

Jérémy Kersten (jke)
on 5/17/15, 2:03 PM

What do you have if you print uid, id, context before the call to browse ? (please use style "code" to keep indentation)

Jérémy Kersten (jke)
on 5/17/15, 2:23 PM

Ok, so the error in traceback is because your id is not a integer ! the stripe_publishable_key is uniq for each payment ? Where do you save this key, on each line ? If it's the same for each payment, you could use ir_config_parameter to save it... Do you have a github to see your code ?

Jérémy Kersten (jke)
on 5/17/15, 3:51 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 5/17/15, 11:39 AM
Seen: 845 times
Last updated: 5/17/15, 5:30 PM