[MERGE] [ADD] payment: added Adyen acquirer. See adyen.com for more details about this acquirer.

Only the form-based acquirer is implemented. Server2server communications will not
be supported. This is a classic implementation, like the paypal or ogone one.

bzr revid: tde@openerp.com-20131202154444-ptr0uj9g19xpaoqc
This commit is contained in:
Thibault Delavallée
2013-12-02 16:44:44 +01:00
16 changed files with 498 additions and 5 deletions
+23
View File
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
import models
import controllers
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
{
'name': 'Adyen Payment Acquirer',
'category': 'Hidden',
'summary': 'Payment Acquirer: Adyen Implementation',
'version': '1.0',
'description': """Adyen Payment Acquirer""",
'author': 'OpenERP SA',
'depends': ['payment_acquirer'],
'data': [
'views/adyen.xml',
'views/payment_acquirer.xml',
'data/adyen.xml',
],
'installable': True,
}
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
import main
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from openerp.addons.web import http
from openerp.addons.web.http import request
from openerp.addons.website.models import website
try:
import simplejson as json
except ImportError:
import json
import logging
import pprint
_logger = logging.getLogger(__name__)
class AdyenController(http.Controller):
_return_url = '/payment/adyen/return/'
@website.route([
'/payment/adyen/return/',
], type='http', auth='public')
def adyen_return(self, **post):
""" Paypal IPN. """
_logger.info('Beginning Adyen form_feedback with post data %s', pprint.pformat(post)) # debug
request.registry['payment.transaction'].form_feedback(request.cr, request.uid, post, 'adyen', context=request.context)
return_url = post.pop('return_url', '')
if not return_url:
custom = json.loads(post.pop('merchantReturnData', '{}'))
return_url = custom.pop('return_url', '/')
return request.redirect(return_url)
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="0">
<record id="payment_acquirer_adyen" model="payment.acquirer">
<field name="name">adyen</field>
<field name="view_template_id" ref="adyen_acquirer_button"/>
<field name="env">test</field>
<field name="adyen_merchant_account">OpenERPCOM</field>
<field name="adyen_skin_code">cbqYWvVL</field>
<field name="adyen_skin_hmac_key">cbqYWvVL</field>
</record>
</data>
</openerp>
@@ -0,0 +1,9 @@
.. _changelog:
Changelog
=========
`trunk (saas-3)`
----------------
- Module creation
@@ -0,0 +1,14 @@
Payment module documentation
============================
Payment documentation topics
''''''''''''''''''''''''''''
Changelog
'''''''''
.. toctree::
:maxdepth: 1
changelog.rst
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
import adyen
@@ -0,0 +1,198 @@
# -*- coding: utf-'8' "-*-"
import base64
try:
import simplejson as json
except ImportError:
import json
from hashlib import sha1
import hmac
import logging
import urlparse
from openerp.addons.payment_acquirer.models.payment_acquirer import ValidationError
from openerp.addons.payment_acquirer_adyen.controllers.main import AdyenController
from openerp.osv import osv, fields
from openerp.tools import float_round
_logger = logging.getLogger(__name__)
class AcquirerAdyen(osv.Model):
_inherit = 'payment.acquirer'
def _get_adyen_urls(self, cr, uid, ids, name, args, context=None):
""" Adyen URLs
- yhpp: hosted payment page: pay.shtml for single, select.shtml for multiple
"""
res = {}
for acquirer in self.browse(cr, uid, ids, context=context):
qualif = acquirer.env
res[acquirer.id] = {
'adyen_form_url': 'https://%s.adyen.com/hpp/pay.shtml' % qualif,
}
return res
_columns = {
'adyen_merchant_account': fields.char('Merchant Account', required_if_provider='adyen'),
'adyen_skin_code': fields.char('Skin Code', required_if_provider='adyen'),
'adyen_skin_hmac_key': fields.char('Skin HMAC Key', required_if_provider='adyen'),
'adyen_form_url': fields.function(
_get_adyen_urls, multi='_get_adyen_urls',
type='char', string='Transaction URL', required_if_provider='adyen'),
}
def _adyen_generate_merchant_sig(self, acquirer, inout, values):
""" Generate the shasign for incoming or outgoing communications.
:param browse acquirer: the payment.acquirer browse record. It should
have a shakey in shaky out
:param string inout: 'in' (openerp contacting ogone) or 'out' (adyen
contacting openerp). In this last case only some
fields should be contained (see e-Commerce basic)
:param dict values: transaction values
:return string: shasign
"""
assert inout in ('in', 'out')
assert acquirer.name == 'adyen'
if inout == 'in':
keys = "paymentAmount currencyCode shipBeforeDate merchantReference skinCode merchantAccount sessionValidity shopperEmail shopperReference recurringContract allowedMethods blockedMethods shopperStatement merchantReturnData billingAddressType deliveryAddressType offset".split()
else:
keys = "authResult pspReference merchantReference skinCode paymentMethod shopperLocale merchantReturnData".split()
def get_value(key):
if values.get(key):
return values[key]
return ''
sign = ''.join('%s' % get_value(k) for k in keys).encode('ascii')
key = acquirer.adyen_skin_hmac_key.encode('ascii')
return base64.b64encode(hmac.new(key, sign, sha1).digest())
def adyen_form_generate_values(self, cr, uid, id, reference, amount, currency, partner_id=False, partner_values=None, tx_custom_values=None, context=None):
if partner_values is None:
partner_values = {}
base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url')
acquirer = self.browse(cr, uid, id, context=context)
# tmp
import datetime
from dateutil import relativedelta
tmp_date = datetime.date.today() + relativedelta.relativedelta(days=1)
partner = None
if partner_id:
partner = self.pool['res.partner'].browse(cr, uid, partner_id, context=context)
tx_values = {
'merchantReference': reference,
'paymentAmount': '%d' % int(float_round(amount, 2) * 100),
'currencyCode': currency and currency.name or 'EUR',
'shipBeforeDate': tmp_date,
'skinCode': acquirer.adyen_skin_code,
'merchantAccount': acquirer.adyen_merchant_account,
'shopperLocale': partner and partner.lang or partner_values.get('lang', 'en_US'),
'sessionValidity': tmp_date,
'merchantSig': 'oij',
'resURL': '%s' % urlparse.urljoin(base_url, AdyenController._return_url),
}
if tx_custom_values and tx_custom_values.get('return_url'):
tx_values['merchantReturnData'] = json.dumps({'return_url': '%s' % tx_custom_values.pop('return_url')})
if tx_custom_values:
tx_values.update(tx_custom_values)
tx_values['merchantSig'] = self._adyen_generate_merchant_sig(acquirer, 'in', tx_values)
return tx_values
def adyen_get_form_action_url(self, cr, uid, id, context=None):
acquirer = self.browse(cr, uid, id, context=context)
return acquirer.adyen_form_url
class TxAdyen(osv.Model):
_inherit = 'payment.transaction'
_columns = {
'adyen_psp_reference': fields.char('Adyen PSP Reference'),
}
# --------------------------------------------------
# FORM RELATED METHODS
# --------------------------------------------------
def adyen_form_generate_values(self, cr, uid, id, tx_custom_values=None, context=None):
tx = self.browse(cr, uid, id, context=context)
tx_data = {
'shopperLocale': tx.partner_lang,
}
if tx_custom_values:
tx_data.update(tx_custom_values)
return self.pool['payment.acquirer'].paypal_form_generate_values(
cr, uid, tx.acquirer_id.id,
tx.reference, tx.amount, tx.currency_id,
tx_custom_values=tx_data,
context=context
)
def _adyen_form_get_tx_from_data(self, cr, uid, data, context=None):
reference, pspReference = data.get('merchantReference'), data.get('pspReference')
if not reference or not pspReference:
error_msg = 'Adyen: received data with missing reference (%s) or missing pspReference (%s)' % (reference, pspReference)
_logger.error(error_msg)
raise ValidationError(error_msg)
# find tx -> @TDENOTE use pspReference ?
tx_ids = self.pool['payment.transaction'].search(cr, uid, [('reference', '=', reference)], context=context)
if not tx_ids or len(tx_ids) > 1:
error_msg = 'Adyen: received data for reference %s' % (reference)
if not tx_ids:
error_msg += '; no order found'
else:
error_msg += '; multiple order found'
_logger.error(error_msg)
raise ValidationError(error_msg)
tx = self.pool['payment.transaction'].browse(cr, uid, tx_ids[0], context=context)
# verify shasign
shasign_check = self.pool['payment.acquirer']._adyen_generate_merchant_sig(tx.acquirer_id, 'out', data)
if shasign_check != data.get('merchantSig'):
error_msg = 'Adyen: invalid merchantSig, received %s, computed %s' % (data.get('merchantSig'), shasign_check)
_logger.warning(error_msg)
# raise ValidationError(error_msg)
return tx
def _adyen_form_get_invalid_parameters(self, cr, uid, tx, data, context=None):
# TODO: txn_id: shoudl be false at draft, set afterwards, and verified with txn details
invalid_parameters = []
if data.get('skinCode') != tx.acquirer_id.adyen_skin_code:
invalid_parameters.append(('skinCode', data.get('skinCode'), tx.acquirer_id.adyen_skin_code))
if not data.get('authResult'):
invalid_parameters.append(('authResult', data.get('authResult'), 'something'))
return invalid_parameters
def _adyen_form_validate(self, cr, uid, tx, data, context=None):
status = data.get('authResult', 'PENDING')
if status == 'AUTHORISED':
tx.write({
'state': 'done',
'adyen_psp_reference': data.get('pspReference'),
# 'date_validate': data.get('payment_date', fields.datetime.now()),
# 'paypal_txn_type': data.get('express_checkout')
})
return True
elif status == 'PENDING':
tx.write({
'state': 'pending',
'adyen_psp_reference': data.get('pspReference'),
})
return True
else:
error = 'Paypal: feedback error'
_logger.info(error)
tx.write({
'state': 'error',
'state_message': error
})
return False
Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from . import test_adyen
checks = [
test_adyen,
]
@@ -0,0 +1,105 @@
# -*- coding: utf-8 -*-
from openerp.addons.payment_acquirer.models.payment_acquirer import ValidationError
from openerp.addons.payment_acquirer.tests.common import PaymentAcquirerCommon
from openerp.addons.payment_acquirer_adyen.controllers.main import AdyenController
from openerp.osv.orm import except_orm
from openerp.tools import mute_logger
from lxml import objectify
import urlparse
class AdyenCommon(PaymentAcquirerCommon):
def setUp(self):
super(AdyenCommon, self).setUp()
cr, uid = self.cr, self.uid
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_adyen', 'adyen_acquirer_button')
# create a new ogone account
self.adyen_id = self.payment_acquirer.create(
cr, uid, {
'name': 'adyen',
'env': 'test',
'view_template_id': self.paypal_view_id,
'adyen_merchant_account': 'OpenERPCOM',
'adyen_skin_code': 'cbqYWvVL',
'adyen_skin_hmac_key': 'cbqYWvVL',
})
# some CC (always use expiration date 06 / 2016, cvc 737, cid 7373 (amex))
self.amex = (('370000000000002', '7373'))
self.dinersclub = (('36006666333344', '737'))
self.discover = (('6011601160116611', '737'), ('644564456445644', '737'))
self.jcb = (('3530111333300000', '737'))
self.mastercard = (('5555444433331111', '737'), ('5555555555554444', '737'))
self.visa = (('4111 1111 1111 1111', '737'), ('4444333322221111', '737'))
self.mcdebit = (('5500000000000004', '737'))
self.visadebit = (('4400000000000008', '737'))
self.maestro = (('6731012345678906', '737'))
self.laser = (('630495060000000000', '737'))
self.hipercard = (('6062828888666688', '737'))
self.dsmastercard = (('521234567890 1234', '737', 'user', 'password'))
self.dsvisa = (('4212345678901237', '737', 'user', 'password'))
self.mistercash = (('6703444444444449', None, 'user', 'password'))
class AdyenServer2Server(AdyenCommon):
def test_00_tx_management(self):
cr, uid, context = self.cr, self.uid, {}
class AdyenForm(AdyenCommon):
def test_10_adyen_form_render(self):
cr, uid, context = self.cr, self.uid, {}
# ----------------------------------------
# Test: button direct rendering
# ----------------------------------------
form_values = {
'merchantAccount': 'OpenERPCOM',
'merchantReference': 'test_ref0',
'skinCode': 'cbqYWvVL',
'paymentAmount': '1',
'currencyCode': 'EUR',
'resURL': '%s' % urlparse.urljoin(self.base_url, AdyenController._return_url),
}
# render the button
res = self.payment_acquirer.render(
cr, uid, self.adyen_id,
'test_ref0', 0.01, self.currency_euro,
partner_id=None,
partner_values=self.buyer_values,
context=context)
# check form result
tree = objectify.fromstring(res)
self.assertEqual(tree.get('action'), 'https://test.adyen.com/hpp/pay.shtml', 'adyen: wrong form POST url')
for form_input in tree.input:
if form_input.get('name') in ['submit', 'shipBeforeDate', 'sessionValidity', 'shopperLocale', 'merchantSig']:
continue
self.assertEqual(
form_input.get('value'),
form_values[form_input.get('name')],
'adyen: wrong value for form: received %s instead of %s' % (form_input.get('value'), form_values[form_input.get('name')])
)
@mute_logger('openerp.addons.payment_acquirer_paypal.models.paypal', 'ValidationError')
def test_20_paypal_form_management(self):
cr, uid, context = self.cr, self.uid, {}
# {'authResult': u'AUTHORISED',
# 'merchantReference': u'SO014',
# 'merchantReturnData': u'return_url=/shop/payment/validate',
# 'merchantSig': u'GaLRO8aMHFaQX3gQ5BVP/YETzeA=',
# 'paymentMethod': u'visa',
# 'pspReference': u'8813859935907337',
# 'shopperLocale': u'en_US',
# 'skinCode': u'cbqYWvVL'}
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="0">
<template id="adyen_acquirer_button">
<form t-if="acquirer.adyen_merchant_account" t-att-action="acquirer.adyen_form_url" method="post" target="_self">
<input type="hidden" name="merchantReference" t-att-value="tx_values['merchantReference']"/>
<input type="hidden" name="paymentAmount" t-att-value="tx_values['paymentAmount']"/>
<input type="hidden" name="currencyCode" t-att-value="tx_values['currencyCode']"/>
<input type="hidden" name="shipBeforeDate" t-att-value="tx_values['shipBeforeDate']"/>
<input type="hidden" name="skinCode" t-att-value="tx_values['skinCode']"/>
<input type="hidden" name="merchantAccount" t-att-value="tx_values['merchantAccount']"/>
<input type="hidden" name="shopperLocale" t-att-value="tx_values['shopperLocale']"/>
<input type="hidden" name="sessionValidity" t-att-value="tx_values['sessionValidity']"/>
<input type="hidden" name="merchantSig" t-att-value="tx_values['merchantSig']"/>
<!-- URLs -->
<input t-if="tx_values.get('resURL')" type='hidden' name='resURL'
t-att-value="tx_values.get('resURL')"/>
<!-- custom -->
<input t-if="tx_values.get('merchantReturnData')" type='hidden' name='merchantReturnData'
t-att-value="tx_values.get('merchantReturnData')"/>
<!-- button -->
<input type="image" name="submit" id="payment_submit"
width="100px"
src="/payment_acquirer_adyen/static/src/img/logo.jpg"/>
</form>
</template>
</data>
</openerp>
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="acquirer_form_adyen" model="ir.ui.view">
<field name="name">acquirer.form.adyen</field>
<field name="model">payment.acquirer</field>
<field name="inherit_id" ref="payment_acquirer.acquirer_form"/>
<field name="arch" type="xml">
<xpath expr='//group[@name="acquirer_base"]' position='after'>
<group string="Adyen Details"
attrs="{'invisible': [('name', '!=', 'adyen')]}">
<field name="adyen_merchant_account"/>
</group>
</xpath>
</field>
</record>
<record id="transaction_form_adyen" model="ir.ui.view">
<field name="name">acquirer.transaction.form.adyen</field>
<field name="model">payment.transaction</field>
<field name="inherit_id" ref="payment_acquirer.transaction_form"/>
<field name="arch" type="xml">
<xpath expr='//notebook' position='inside'>
<page string="Adyen TX Details">
<group>
<field name="adyen_psp_reference"/>
</group>
</page>
</xpath>
</field>
</record>
</data>
</openerp>
+8 -5
View File
@@ -879,17 +879,20 @@
</div>
<div class="js_payment mb64" t-if="not payment_acquirer_id and payments" id="payment_method">
<p>Payment method:</p>
<div>
<h3>Choose your payment method</h3>
<div class="col-lg-5 col-sm-6">
<t t-foreach="payments or []" t-as="payment">
<label t-if="payment._content">
<input t-att-value="payment.id" type="radio" name="payment_type"/> <span t-field="payment.name"/>
</label>
</t>
<t t-foreach="payments" t-as="payment">
<div t-att-data-id="payment.id" t-raw="payment._content" class="oe_payment_acquirer hidden"/>
</t>
</div>
<div class="col-lg-3 col-sm-3">
<button class="btn btn-primary pull-right">Pay Now !!!</button>
</div>
<t t-foreach="payments" t-as="payment">
<div t-att-data-id="payment.id" t-raw="payment._content" class="oe_payment_acquirer hidden"/>
</t>
</div>
</div>