Skip to content
This repository was archived by the owner on Nov 6, 2022. It is now read-only.

Commit 47ce915

Browse files
author
Hugo Osvaldo Barrera
committed
Use the newer Payment search endpoint
The old endpoint has historically only worked "sometimes", and a lot less recently. There's a new Payment search endpoint now, so use that, since it seems to work all the time.
1 parent c83e853 commit 47ce915

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

django_mercadopago/models.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
import requests
34
from django.conf import settings
45
from django.db import models
56
from django.db import transaction
@@ -248,16 +249,23 @@ def update(self, title=None, price=None):
248249

249250
def poll_status(self):
250251
"""
251-
Manually poll for the status of this preference
252+
Manually poll for the status of this preference.
252253
"""
253254
service = self.owner.service
254-
response = service.search_payment({
255-
'external_reference': self.reference
256-
})
257-
if response['response']['results']:
255+
response = requests.get(
256+
'https://api.mercadopago.com/v1/payments/search',
257+
params={
258+
'access_token': service.get_access_token(),
259+
'external_reference': self.reference,
260+
},
261+
)
262+
response.raise_for_status()
263+
response = response.json()
264+
265+
if response['results']:
258266
logger.info('Polled for %s. Creating Payment', self.pk)
259267
return Payment.objects.create_or_update_from_raw_data(
260-
response['response']['results'][-1]
268+
response['results'][-1]
261269
)
262270
else:
263271
logger.info('Polled for %s. No data', self.pk)
@@ -279,7 +287,9 @@ def __str__(self):
279287
class PaymentManager(models.Manager):
280288

281289
def create_or_update_from_raw_data(self, raw_data):
282-
raw_data = raw_data['collection']
290+
# Older endpoints will use this formats, newer one won't.
291+
if 'collection' in raw_data:
292+
raw_data = raw_data['collection']
283293

284294
preference = Preference.objects.filter(
285295
reference=raw_data['external_reference'],

0 commit comments

Comments
 (0)