Skip to content

Commit

Permalink
fix: Fix error in payment pendent for mercadopago
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatasoli committed Oct 9, 2024
1 parent 912e705 commit df2485d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
70 changes: 38 additions & 32 deletions app/cart/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,27 @@ async def checkout(
cart.payment_intent = payment_response.id
logger.debug(f'Payment Intent {cart.payment_intent}')
authorization = payment_response.authorization_code
payment_accept = bootstrap.payment.accept_payment(
payment_gateway=cart.gateway_provider,
payment_id=cart.payment_intent,
)
payment_accept = None
if payment_response.status not in ['in_process', 'rejected']:
payment_accept = bootstrap.payment.accept_payment(
payment_gateway=cart.gateway_provider,
payment_id=cart.payment_intent,
)

logger.debug(f'Payment response: {payment_accept}')
logger.debug(f'Payment response: {payment_accept}')

# TODO: check if order is in inventory for decrease
await decrease_inventory(
cart=cart,
order_id=order_id,
bootstrap=bootstrap,
)
if payment_accept.status == 'approved':
payment_accept_status = getattr(
payment_accept,
'status',
'PENDING',
)
if payment_accept_status == 'approved':
await update_order(
order_update=OrderDBUpdate(
order_id=order_id,
Expand All @@ -155,7 +162,7 @@ async def checkout(
payment_id=payment_id,
payment_gateway=cart.gateway_provider,
authorization=authorization,
payment_status=payment_accept.status,
payment_status=payment_accept_status,
processed=True,
bootstrap=bootstrap,
)
Expand All @@ -165,35 +172,34 @@ async def checkout(
send_mail=True,
bootstrap=bootstrap,
)
logger.debug('Debug comission credit card')
logger.debug(f'Order Id {order_id}')
logger.debug(f'Affiliate {affiliate_id}')
logger.debug(f'subtotal {cart.subtotal}')
logger.debug(f'Coupon {coupon}')
logger.debug(f'payment_Id {_payment_id}')
logger.debug(f'payment_Id 2 {payment_id}')
if all([order_id, affiliate_id, coupon, _payment_id]):
logger.debug('Credit Card task start')
await bootstrap.message.broker.publish(
{
'user_id': affiliate_id,
'order_id': order_id,
'subtotal': cart.subtotal,
'coupon_id': coupon.coupon_id,
'commission_percentage': coupon.commission_percentage,
'payment_id': _payment_id,
},
queue=RabbitQueue('sales_commission'),
)
else:
logger.debug('Not commission')
logger.debug('Debug comission credit card')
logger.debug(f'Order Id {order_id}')
logger.debug(f'Affiliate {affiliate_id}')
logger.debug(f'subtotal {cart.subtotal}')
logger.debug(f'Coupon {coupon}')
logger.debug(f'payment_Id {_payment_id}')
if all([order_id, affiliate_id, coupon, _payment_id]):
logger.debug('Credit Card task start')
await bootstrap.message.broker.publish(
{
'mail_to': user['email'],
'order_id': order_id if order_id else '',
'user_id': affiliate_id,
'order_id': order_id,
'subtotal': cart.subtotal,
'coupon_id': coupon.coupon_id,
'commission_percentage': coupon.commission_percentage,
'payment_id': _payment_id,
},
queue=RabbitQueue('notification_order_paid'),
queue=RabbitQueue('sales_commission'),
)
else:
logger.debug('Not commission')
await bootstrap.message.broker.publish(
{
'mail_to': user['email'],
'order_id': order_id if order_id else '',
},
queue=RabbitQueue('notification_order_paid'),
)
logger.info(
f'Checkout cart {cart_uuid} with payment {payment_id} processed with success',
)
Expand Down
9 changes: 7 additions & 2 deletions app/infra/payment_gateway/mercadopago_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MercadoPagoPaymentResponse(BaseModel):

id: int
authorization_code: str
date_created: str
date_created: str | None = None
date_approved: str | None = None
currency_id: str
payment_method: dict
Expand Down Expand Up @@ -156,7 +156,12 @@ def create_credit_card_payment(
)
logger.info('Gateway Response')
logger.info(f'{payment_response["response"]}')
if payment_response["response"].get('status') not in [200, 201, 'authorized']:
if payment_response["response"].get('status') not in [
200,
201,
'authorized',
'in_process',
]:
logger.info(payment_response)
raise_payment_not_found()
return MercadoPagoPaymentResponse.model_validate(
Expand Down

0 comments on commit df2485d

Please sign in to comment.