diff --git a/addons/payment_acquirer_paypal/tests/test_paypal.py b/addons/payment_acquirer_paypal/tests/test_paypal.py index 266c97d6b3b..77ac34c9d60 100644 --- a/addons/payment_acquirer_paypal/tests/test_paypal.py +++ b/addons/payment_acquirer_paypal/tests/test_paypal.py @@ -1,155 +1,187 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Business Applications -# Copyright (c) 2012-TODAY OpenERP S.A. -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +from openerp.addons.payment_acquirer.models.payment_acquirer import ValidationError from openerp.addons.payment_acquirer.tests.common import PaymentAcquirerCommon +from openerp.addons.payment_acquirer_paypal.controllers.main import PaypalController from openerp.osv.orm import except_orm +from openerp.tools import mute_logger from lxml import objectify # import requests # import urlparse +import urllib +import urllib2 +import urlparse -class BasicPayment(PaymentAcquirerCommon): +class PaypalCommon(PaymentAcquirerCommon): - def test_10_paypal_basic(self): - pass - - def test_11_paypal_form(self): + def setUp(self): + super(PaypalCommon, self).setUp() cr, uid = self.cr, self.uid - context = {} - base_url = self.registry('ir.config_parameter').get_param(cr, uid, 'web.base.url') - # ogone_url = self.payment_acquirer._get_ogone_urls(cr, uid, [ogone_id], None, None)[ogone_id]['ogone_standard_order_url'] - model, paypal_view_id = self.registry('ir.model.data').get_object_reference(cr, uid, 'payment_acquirer_paypal', 'paypal_acquirer_button') + self.base_url = self.registry('ir.config_parameter').get_param(cr, uid, 'web.base.url') + model, self.paypal_view_id = self.registry('ir.model.data').get_object_reference(cr, uid, 'payment_acquirer_paypal', 'paypal_acquirer_button') - # forgot some mandatory fields: should crash - with self.assertRaises(except_orm): - paypal_id = self.payment_acquirer.create( - cr, uid, { - 'name': 'paypal', - 'env': 'test', - 'view_template_id': paypal_view_id, - 'paypal_email_id': 'tde+paypal-facilitator@openerp.com', - }, context=context - ) - # tde+buyer@openerp.com - - # create a new paypal account - paypal_id = self.payment_acquirer.create( + # create a new ogone account + self.paypal_id = self.payment_acquirer.create( cr, uid, { 'name': 'paypal', 'env': 'test', - 'view_template_id': paypal_view_id, - 'paypal_email_id': 'dummy', - 'paypal_username': 'dummy', + 'view_template_id': self.paypal_view_id, + 'paypal_email_id': 'tde+paypal-facilitator@openerp.com', + 'paypal_username': 'tde+paypal-facilitator_api1.openerp.com', + 'paypal_api_enabled': True, + 'paypal_api_username': 'AYf_uBATwly1C72DqE2njwDHmZI25UHcZMwvgvgICLkeQEgutvrhrg6y3KhZ', + 'paypal_api_password': 'EJSDgxC_LuZ9oeG-Ud_oozfiDqqN3mUVLMmzPK71IZA3TM4taicUY2uaJYU1', + }) + # tde+seller@openerp.com - tde+buyer@openerp.com - tde+buyer-it@openerp.com + + +class PaypalServer2Server(PaypalCommon): + + def test_00(self): + cr, uid, context = self.cr, self.uid, {} + + res = self.payment_acquirer._paypal_s2s_get_access_token(cr, uid, [self.paypal_id], context=context) + print res, res.get('access_token') + + res = self.payment_transaction.paypal_s2s_create( + cr, uid, { + 'amount': 0.01, + 'acquirer_id': self.paypal_id, + 'currency_id': self.currency_euro_id, + 'reference': 'test_reference', + 'paypal_txn_id': '61E67681CH3238416', }, context=context ) - # verify acquirer data - paypal = self.payment_acquirer.browse(cr, uid, paypal_id, context) + print res + + +class PaypalForm(PaypalCommon): + + def test_00_paypal_acquirer(self): + cr, uid, context = self.cr, self.uid, {} + # forgot some mandatory fields: should crash + with self.assertRaises(except_orm): + self.payment_acquirer.create( + cr, uid, { + 'name': 'paypal', + 'env': 'test', + 'view_template_id': self.paypal_view_id, + 'paypal_email_id': 'tde+paypal-facilitator@openerp.com', + }, context=context) + + paypal = self.payment_acquirer.browse(self.cr, self.uid, self.paypal_id, None) self.assertEqual(paypal.env, 'test', 'test without test env') + def test_10_paypal_form_render(self): + cr, uid, context = self.cr, self.uid, {} + + # ---------------------------------------- + # Test: button direct rendering + # ---------------------------------------- + + form_values = { + 'cmd': '_xclick', + 'business': 'tde+paypal-facilitator@openerp.com', + 'item_name': 'test_ref0', + 'item_number': 'test_ref0', + 'first_name': 'Buyer', + 'last_name': 'Norbert', + 'amount': '0.01', + 'currency_code': 'EUR', + 'address1': 'Huge Street 2/543', + 'city': 'Sin City', + 'zip': '1000', + 'country': 'Belgium', + 'email': 'norbert.buyer@example.com', + 'return': '%s' % urlparse.urljoin(self.base_url, PaypalController._return_url), + 'notify_url': '%s' % urlparse.urljoin(self.base_url, PaypalController._notify_url), + 'cancel_return': '%s' % urlparse.urljoin(self.base_url, PaypalController._cancel_url), + } + # render the button res = self.payment_acquirer.render( - cr, uid, paypal_id, + cr, uid, self.paypal_id, 'test_ref0', 0.01, self.currency_euro, partner_id=None, partner_values=self.buyer_values, context=context) - # print res - # # check some basic paypal methods - # res = self.payment_transaction.validate_paypal_notification( - # cr, uid, - # 'http://localhost/payment?mc_gross=19.95&protection_eligibility=Eligible&address_status=confirmed&payer_id=LPLWNMTBWMFAY&tax=0.00&address_street=1+Main+St&payment_date=20%3A12%3A59+Jan+13%2C+2009+PST&payment_status=Completed&charset=windows-1252&address_zip=95131&first_name=Test&mc_fee=0.88&address_country_code=US&address_name=Test+User¬ify_version=2.6&custom=&payer_status=verified&address_country=United+States&address_city=San+Jose&quantity=1&verify_sign=AtkOfCXbDm2hu0ZELryHFjY-Vb7PAUvS6nMXgysbElEn9v-1XcmSoGtf&payer_email=gpmac_1231902590_per%40paypal.com&txn_id=61E67681CH3238416&payment_type=instant&last_name=User&address_state=CA&receiver_email=gpmac_1231902686_biz%40paypal.com&payment_fee=0.88&receiver_id=S8XGHLYDW9T3S&txn_type=express_checkout&item_name=&mc_currency=USD&item_number=&residence_country=US&test_ipn=1&handling_amount=0.00&transaction_subject=&payment_gross=19.95&shipping=0.00') - # self.assertEqual(res, False, 'payment: paypal validation on a txn_id that does not exists should return False') + # check form result + tree = objectify.fromstring(res) + self.assertEqual(tree.get('action'), 'https://www.sandbox.paypal.com/cgi-bin/webscr', 'paypal: wrong form POST url') + for form_input in tree.input: + if form_input.get('name') in ['submit']: + continue + self.assertEqual( + form_input.get('value'), + form_values[form_input.get('name')], + 'paypal: wrong value for form: received %s instead of %s' % (form_input.get('value'), form_values[form_input.get('name')]) + ) - # txn_id = self.payment_transaction.create( - # cr, uid, { - # 'amount': 0.01, - # 'acquirer_id': paypal_id, - # 'currency_id': currency_id, - # 'reference': 'test_reference', - # 'paypal_txn_id': '61E67681CH3238416', - # }, context=context - # ) - # res = self.payment_transaction.validate_paypal_notification( - # cr, uid, - # 'http://localhost/payment?mc_gross=19.95&protection_eligibility=Eligible&address_status=confirmed&payer_id=LPLWNMTBWMFAY&tax=0.00&address_street=1+Main+St&payment_date=20%3A12%3A59+Jan+13%2C+2009+PST&payment_status=Completed&charset=windows-1252&address_zip=95131&first_name=Test&mc_fee=0.88&address_country_code=US&address_name=Test+User¬ify_version=2.6&custom=&payer_status=verified&address_country=United+States&address_city=San+Jose&quantity=1&verify_sign=AtkOfCXbDm2hu0ZELryHFjY-Vb7PAUvS6nMXgysbElEn9v-1XcmSoGtf&payer_email=gpmac_1231902590_per%40paypal.com&txn_id=61E67681CH3238416&payment_type=instant&last_name=User&address_state=CA&receiver_email=gpmac_1231902686_biz%40paypal.com&payment_fee=0.88&receiver_id=S8XGHLYDW9T3S&txn_type=express_checkout&item_name=&mc_currency=USD&item_number=&residence_country=US&test_ipn=1&handling_amount=0.00&transaction_subject=&payment_gross=19.95&shipping=0.00') - # print res + @mute_logger('openerp.addons.payment_acquirer_paypal.models.paypal', 'ValidationError') + def test_20_paypal_form_management(self): + cr, uid, context = self.cr, self.uid, {} - # # user pays using Paypal - # resp = self.payment_transaction.create_paypal_command( - # cr, uid, cmd='_xclick', parameters={ - # 'business': 'tdelavallee-facilitator@gmail.com', - # 'amount': 50, - # 'item_name': 'test_item', - # 'quantity': 1, - # 'currency_code': 'USD', - # 'return': 'http://www.example.com', - # }) - # print resp - # print resp.url - # print resp.text + # typical data posted by paypal after client has successfully paid + paypal_post_data = { + 'protection_eligibility': u'Ineligible', + 'last_name': u'Poilu', + 'txn_id': u'08D73520KX778924N', + 'receiver_email': u'tde+paypal-facilitator@openerp.com', + 'payment_status': u'Pending', + 'payment_gross': u'', + 'tax': u'0.00', + 'residence_country': u'FR', + 'address_state': u'Alsace', + 'payer_status': u'verified', + 'txn_type': u'web_accept', + 'address_street': u'Av. de la Pelouse, 87648672 Mayet', + 'handling_amount': u'0.00', + 'payment_date': u'03:21:19 Nov 18, 2013 PST', + 'first_name': u'Norbert', + 'item_name': u'test_ref_2', + 'address_country': u'France', + 'charset': u'windows-1252', + 'custom': u'', + 'notify_version': u'3.7', + 'address_name': u'Norbert Poilu', + 'pending_reason': u'multi_currency', + 'item_number': u'test_ref_2', + 'receiver_id': u'DEG7Z7MYGT6QA', + 'transaction_subject': u'', + 'business': u'tde+paypal-facilitator@openerp.com', + 'test_ipn': u'1', + 'payer_id': u'VTDKRZQSAHYPS', + 'verify_sign': u'An5ns1Kso7MWUdW4ErQKJJJ4qi4-AVoiUf-3478q3vrSmqh08IouiYpM', + 'address_zip': u'75002', + 'address_country_code': u'FR', + 'address_city': u'Paris', + 'address_status': u'unconfirmed', + 'mc_currency': u'EUR', + 'shipping': u'0.00', + 'payer_email': u'tde+buyer@openerp.com', + 'payment_type': u'instant', + 'mc_gross': u'1.95', + 'ipn_track_id': u'866df2ccd444b', + 'quantity': u'1' + } - # self.payment_transaction.validate(cr, uid, [tx_id], context=context) + # should raise error about unknown tx + with self.assertRaises(ValidationError): + self.payment_transaction.form_feedback(cr, uid, paypal_post_data, 'paypal', context=context) -# { -# 'protection_eligibility': u'Ineligible', -# 'last_name': u'Poilu', -# 'txn_id': u'08D73520KX778924N', -# 'receiver_email': u'tde+paypal-facilitator@openerp.com', -# 'payment_status': u'Pending', -# 'payment_gross': u'', -# 'tax': u'0.00', -# 'residence_country': u'FR', -# 'address_state': u'Alsace', -# 'payer_status': u'verified', -# 'txn_type': u'web_accept', -# 'address_street': u'Av. de la Pelouse, 87648672 Mayet', -# 'handling_amount': u'0.00', -# 'payment_date': u'03:21:19 Nov 18, 2013 PST', -# 'first_name': u'Norbert', -# 'item_name': u'test_ref_0', -# 'address_country': u'France', -# 'charset': u'windows-1252', -# 'custom': u'', -# 'notify_version': u'3.7', -# 'address_name': u'Norbert Poilu', -# 'pending_reason': u'multi_currency', -# 'item_number': u'test_ref_0', -# 'receiver_id': u'DEG7Z7MYGT6QA', -# 'transaction_subject': u'', -# 'business': u'tde+paypal-facilitator@openerp.com', -# 'test_ipn': u'1', -# 'payer_id': u'VTDKRZQSAHYPS', -# 'verify_sign': u'An5ns1Kso7MWUdW4ErQKJJJ4qi4-AVoiUf-3478q3vrSmqh08IouiYpM', -# 'address_zip': u'75002', -# 'address_country_code': u'FR', -# 'address_city': u'Paris', -# 'address_status': u'unconfirmed', -# 'mc_currency': u'EUR', -# 'shipping': u'0.00', -# 'payer_email': u'tde+buyer@openerp.com', -# 'payment_type': u'instant', -# 'mc_gross': u'1.95', -# 'ipn_track_id': u'866df2ccd444b', -# 'quantity': u'1' -# } + # create tx + tx_id = self.payment_transaction.create( + cr, uid, { + 'amount': 1.95, + 'acquirer_id': self.paypal_id, + 'currency_id': self.currency_euro_id, + 'reference': 'test_ref_2', + 'partner_name': 'Norbert Buyer', + }, context=context + ) + # validate it + self.payment_transaction.form_feedback(cr, uid, paypal_post_data, 'paypal', context=context)