Skip to content

Commit

Permalink
[FIX] payment_razorpay: fix proccessing data in token flow
Browse files Browse the repository at this point in the history
Steps:
- Install razorpay and sales app.
- Enable Razorpay provider.
- Create a create a token in razorpay.
- Pay via that token.

Issue:
- When we are paying via token we really don't need
processing values because those processing values
only needed when we open Razorpay form so here it is
calling APIs(customer and order) and creating order
unwantedly.

Fix:
- Return empty dict when operation is `online_token` in
`_get_specific_processing_values` method

Also IMP/REV of PR: odoo#157533

task-3653372

closes odoo#159250

Signed-off-by: Kartik Chavda (kcv) <kcv@odoo.com>
  • Loading branch information
kcv-odoo committed May 6, 2024
1 parent de8f82b commit abf8ffb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
17 changes: 9 additions & 8 deletions addons/payment_razorpay/models/payment_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ def _get_specific_processing_values(self, processing_values):
if self.provider_code != 'razorpay':
return res

customer_is_required = self.tokenize or self.operation in ('online_token', 'offline')
customer_id = customer_is_required and self._razorpay_create_customer()['id']
if self.operation in ('online_token', 'offline'):
return {}

customer_id = self._razorpay_create_customer()['id']
order_id = self._razorpay_create_order(customer_id)['id']
return {
'razorpay_key_id': self.provider_id.razorpay_key_id,
Expand All @@ -53,7 +55,7 @@ def _razorpay_create_customer(self):
payload = {
'name': self.partner_name,
'email': self.partner_email,
'contact': self._validate_phone_number(self.partner_phone),
'contact': self.partner_phone and self._validate_phone_number(self.partner_phone) or '',
'fail_existing': '0', # Don't throw an error if the customer already exists.
}
_logger.info(
Expand All @@ -75,12 +77,12 @@ def _validate_phone_number(self, phone):
:return str: The formatted phone number.
:raise ValidationError: If the phone number is missing or incorrect.
"""
if not phone:
if not phone and self.tokenize:
raise ValidationError("Razorpay: " + _("The phone number is missing."))

try:
phone = self._phone_format(
number=phone, country=self.partner_country_id, raise_exception=True
number=phone, country=self.partner_country_id, raise_exception=self.tokenize
)
except Exception:
raise ValidationError("Razorpay: " + _("The phone number is invalid."))
Expand Down Expand Up @@ -122,8 +124,7 @@ def _razorpay_prepare_order_payload(self, customer_id=None):
**({'method': pm_code} if pm_code != 'wallets_india' else {}),
}
if self.operation in ['online_direct', 'validation']:
if customer_id:
payload['customer_id'] = customer_id # Required for only non-subsequent payments.
payload['customer_id'] = customer_id # Required for only non-subsequent payments.
if self.tokenize:
payload['token'] = {
'max_amount': payment_utils.to_minor_currency_units(
Expand Down Expand Up @@ -404,7 +405,7 @@ def _process_notification_data(self, notification_data):
if self.provider_id.capture_manually:
self._set_authorized()
elif entity_status in const.PAYMENT_STATUS_MAPPING['done']:
if self.tokenize:
if entity_data.get('token_id') and self.provider_id.allow_tokenization:
self._razorpay_tokenize_from_notification_data(notification_data)
self._set_done()

Expand Down
7 changes: 2 additions & 5 deletions addons/payment_razorpay/static/src/js/payment_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ paymentForm.include({
* @return {object}
*/
_prepareRazorpayOptions(processingValues) {
const vals = Object.assign({}, processingValues, {
return Object.assign({}, processingValues, {
'key': processingValues['razorpay_key_id'],
'customer_id': processingValues['razorpay_customer_id'],
'order_id': processingValues['razorpay_order_id'],
'description': processingValues['reference'],
'recurring': processingValues['is_tokenize_request'] ? '1': '0',
Expand All @@ -78,10 +79,6 @@ paymentForm.include({
}
},
});
if (processingValues['razorpay_customer_id']) {
vals['customer_id'] = processingValues['razorpay_customer_id'];
}
return vals;
},

});
1 change: 1 addition & 0 deletions addons/payment_razorpay/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def setUpClass(cls):
'razorpay_key_secret': 'Y63AyP9eL91',
'razorpay_webhook_secret': 'coincoin_motherducker',
'payment_method_ids': [Command.set([cls.env.ref('payment.payment_method_card').id])],
'allow_tokenization': True,
})

cls.razorpay_customer_id = 'cust_123'
Expand Down

0 comments on commit abf8ffb

Please sign in to comment.