|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +import requests |
| 4 | +from celery import shared_task |
| 5 | +from django_pagarme import facade as django_pagarme_facade |
| 6 | +from django_pagarme.models import PagarmePayment |
| 7 | + |
| 8 | +from pythonpro import settings |
| 9 | +from pythonpro.core import facade |
| 10 | + |
| 11 | + |
| 12 | +def total_price(purchased_items): |
| 13 | + """ |
| 14 | + Sum all items prices of purchase to return the total price |
| 15 | + :param purchased_items: itens of a givem purch |
| 16 | + :return total |
| 17 | + """ |
| 18 | + total = 0 |
| 19 | + for item in purchased_items: |
| 20 | + total += item.price |
| 21 | + return total / 100 |
| 22 | + |
| 23 | + |
| 24 | +@shared_task |
| 25 | +def send_abandoned_cart(name, email, phone, payment_item_slug): |
| 26 | + """ |
| 27 | + Send a potential client who filled their data but not complete the buy to hotzapp |
| 28 | + :param name: name filled at the form |
| 29 | + :param phone: phone filled at the form |
| 30 | + :param email: email filled at the form |
| 31 | + :param payment_item_slug: slug of the item of purchase |
| 32 | + """ |
| 33 | + payment_config_item = django_pagarme_facade.get_payment_item(payment_item_slug) |
| 34 | + price = total_price([payment_config_item]) |
| 35 | + potential_customer = { |
| 36 | + 'created_at': datetime.now().isoformat(), |
| 37 | + 'name': name, |
| 38 | + 'phone': phone, |
| 39 | + 'email': email, |
| 40 | + 'currency_code_from': 'R$', |
| 41 | + 'total_price': price, |
| 42 | + 'line_items': [ |
| 43 | + { |
| 44 | + 'product_name': payment_config_item.name, |
| 45 | + 'quantity': 1, |
| 46 | + 'price': price |
| 47 | + } |
| 48 | + ], |
| 49 | + } |
| 50 | + return requests.post(settings.HOTZAPP_API_URL, potential_customer) |
| 51 | + |
| 52 | + |
| 53 | +PAYMENT_METHOD_DCT = { |
| 54 | + django_pagarme_facade.CREDIT_CARD: 'credit', |
| 55 | + django_pagarme_facade.BOLETO: 'billet', |
| 56 | +} |
| 57 | +STATUS_DCT = { |
| 58 | + django_pagarme_facade.REFUSED: 'refused', |
| 59 | + django_pagarme_facade.PAID: 'paid', |
| 60 | + django_pagarme_facade.WAITING_PAYMENT: 'issued', |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +@shared_task |
| 65 | +def send_purchase_notification(payment_id): |
| 66 | + """ |
| 67 | + Send a potential client who filled their data but not complete the buy-in face of a refused credit card |
| 68 | + :params payment_id: id of payment |
| 69 | + """ |
| 70 | + payment = django_pagarme_facade.find_payment(payment_id) |
| 71 | + last_notification = payment.notifications.order_by('-creation').values('status', 'creation').first() |
| 72 | + payment_profile = django_pagarme_facade.get_user_payment_profile(payment.user_id) |
| 73 | + payment_config_items = payment.items.all() |
| 74 | + purchased_items = [{"product_name": item.name, "quantity": '1', "price": str(item.price / 100), } for item in |
| 75 | + payment_config_items] |
| 76 | + |
| 77 | + purchase = { |
| 78 | + "created_at": last_notification['creation'], |
| 79 | + "transaction_id": payment.transaction_id, |
| 80 | + "name": payment_profile.name, |
| 81 | + "email": payment_profile.email, |
| 82 | + "phone": str(payment_profile.phone), |
| 83 | + "total_price": total_price(payment_config_items), |
| 84 | + "line_items": purchased_items, |
| 85 | + "payment_method": PAYMENT_METHOD_DCT[payment.payment_method], |
| 86 | + "financial_status": STATUS_DCT[last_notification['status']], |
| 87 | + } |
| 88 | + return requests.post(settings.HOTZAPP_API_URL, purchase) |
| 89 | + |
| 90 | + |
| 91 | +@shared_task |
| 92 | +def verify_purchase(name, email, phone, payment_item_slug): |
| 93 | + """ |
| 94 | + Verify each buy interaction to see if it succeeded and depending on each situation take an action |
| 95 | + :param name: name filled at the form |
| 96 | + :param phone: phone filled at the form |
| 97 | + :param email: email filled at the form |
| 98 | + :param payment_item_slug: slug of the item of purchase |
| 99 | + """ |
| 100 | + try: |
| 101 | + user = facade.find_user_by_email(email=email) |
| 102 | + except facade.UserDoesNotExist: |
| 103 | + return send_abandoned_cart(name, phone, email, payment_item_slug) |
| 104 | + else: |
| 105 | + payment = PagarmePayment.objects.filter(user__id=user.id, items__slug__exact=payment_item_slug).order_by( |
| 106 | + '-id').first() |
| 107 | + if payment is None or payment.status() != django_pagarme_facade.PAID: |
| 108 | + return send_abandoned_cart(name, phone, email, payment_item_slug) |
0 commit comments