Skip to content

Commit 6eef2bc

Browse files
author
renzo
committed
Sending sales messages on status change
Close #4880
1 parent 29ea7bf commit 6eef2bc

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

pythonpro/discord/bot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ class _DevProDiscordBotClient(DiscordBotClient):
1111
def send_to_sales_channel(self, msg: str) -> dict:
1212
return self.create_message(settings.DISCORD_GUILD_SALES_CHANNEL_ID, msg)
1313

14+
def send_to_checkout_channel(self, msg: str) -> dict:
15+
return self.create_message(settings.DISCORD_GUILD_SALES_CHANNEL_ID, msg)
16+
1417

1518
devpro_discord_bot_client = _DevProDiscordBotClient(settings.DISCORD_APP_BOT_TOKEN)

pythonpro/discord/tasks.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def clean_discord_user(discord_user_id):
5858
---------------------------------------------------------
5959
"""
6060

61-
6261
_WARN_USER_TEMPLATE = """Olá {user_name},
6362
6463
Sua assinatura anual da DevPro está prestes a expirar, e queremos oferecer uma oportunidade imperdível para que você continue aproveitando todos os benefícios de ser nosso assinante.
@@ -119,3 +118,23 @@ def warn_subscription_expiration(user_id: int, expiration_date: str):
119118
)
120119
logger.info(f'Subscription warn sent to user with id: {user.id}')
121120
return None
121+
122+
123+
_CHECKOUT_STATUS_CHANGED_TEMPLATE = """#####################################
124+
O status agora é {status}
125+
Transaction id = https://beta.dashboard.pagar.me/#/transactions/{transaction_id}
126+
"""
127+
128+
129+
@shared_task(
130+
rate_limit=1,
131+
max_retries=5,
132+
retry_backoff=True,
133+
retry_backoff_max=700,
134+
retry_jitter=True
135+
)
136+
def send_payment_status_change(status, transaction_id):
137+
return devpro_discord_bot_client.send_to_checkout_channel(_CHECKOUT_STATUS_CHANGED_TEMPLATE.format(
138+
status=status,
139+
transaction_id=transaction_id
140+
))

pythonpro/domain/checkout_domain.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django_pagarme import facade as django_pagarme_facade
44

55
from pythonpro.core import facade as core_facade
6+
from pythonpro.discord.tasks import send_payment_status_change
67
from pythonpro.domain import user_domain, subscription_domain
78
from pythonpro.domain.hotzapp_domain import verify_purchase, send_purchase_notification
89
from pythonpro.email_marketing import facade as email_marketing_facade
@@ -57,6 +58,7 @@ def payment_handler_task(payment_id):
5758
pass # no need to handle payment with no Item
5859
else:
5960
status = payment.status()
61+
send_payment_status_change.delay(status, payment.transaction_id)
6062
if status == django_pagarme_facade.PAID:
6163
user = payment.user
6264
subscription_domain.create_subscription_and_activate_services(payment)

pythonpro/domain/tests/test_checkout/test_boleto_generation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ def send_purchase_notification_mock(mocker):
6060
return mocker.patch('pythonpro.domain.checkout_domain.send_purchase_notification.delay')
6161

6262

63+
@pytest.fixture(autouse=True)
64+
def send_payment_status_change_mock(mocker):
65+
return mocker.patch('pythonpro.domain.checkout_domain.send_payment_status_change.delay')
66+
67+
6368
@pytest.fixture
6469
def resp(client, pagarme_responses, create_or_update_lead_mock, payment_handler_task_mock, tag_as_mock,
6570
active_product_item, sync_on_discourse_mock, send_purchase_notification_mock):

pythonpro/domain/tests/test_checkout/test_credit_card_payment.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ def send_purchase_notification_mock(mocker):
9595
return mocker.patch('pythonpro.domain.checkout_domain.send_purchase_notification.delay')
9696

9797

98+
@pytest.fixture(autouse=True)
99+
def send_payment_status_change_mock(mocker):
100+
return mocker.patch('pythonpro.domain.checkout_domain.send_payment_status_change.delay')
101+
102+
98103
# tests for user not logged
99104

100105
@pytest.fixture

pythonpro/domain/tests/test_checkout/test_payment_handler.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ def send_purchase_notification_mock(mocker):
2424
return mocker.patch('pythonpro.domain.checkout_domain.send_purchase_notification.delay')
2525

2626

27+
@pytest.fixture(autouse=True)
28+
def send_payment_status_change_mock(mocker):
29+
return mocker.patch('pythonpro.domain.checkout_domain.send_payment_status_change.delay')
30+
31+
2732
def test_pagarme_payment_paid_boleto(db, tag_as_mock, remove_tags_mock, logged_user,
2833
send_purchase_notification_mock, mock_subscription_creation):
2934
payment = baker.make(PagarmePayment, payment_method=facade.BOLETO, user=logged_user)
@@ -113,7 +118,8 @@ def mock_subscription_creation(mocker):
113118

114119

115120
def test_pagarme_payment_paid_credit_card(mock_subscription_creation, db, tag_as_mock, remove_tags_mock,
116-
logged_user, send_purchase_notification_mock):
121+
logged_user, send_purchase_notification_mock,
122+
send_payment_status_change_mock):
117123
payment = baker.make(PagarmePayment, payment_method=facade.CREDIT_CARD, user=logged_user)
118124
baker.make(PagarmeNotification, status=facade.PAID, payment=payment)
119125
config = baker.make(PagarmeItemConfig, payments=[payment])
@@ -124,7 +130,8 @@ def test_pagarme_payment_paid_credit_card(mock_subscription_creation, db, tag_as
124130
remove_tags_mock.assert_called_once_with(
125131
logged_user.email, logged_user.id, f'{config.slug}-refused'
126132
)
127-
send_purchase_notification_mock.asser_called_once_with(payment.id)
133+
send_purchase_notification_mock.assert_called_once_with(payment.id)
134+
send_payment_status_change_mock.assert_called_once_with(facade.PAID, payment.transaction_id)
128135

129136

130137
def test_pagarme_payment_waiting_payment_boleto(mock_subscription_creation, db, tag_as_mock, remove_tags_mock,
@@ -135,7 +142,7 @@ def test_pagarme_payment_waiting_payment_boleto(mock_subscription_creation, db,
135142
checkout_domain.payment_handler_task(payment.id)
136143
assert remove_tags_mock.called is False
137144
tag_as_mock.assert_called_once_with(logged_user.email, logged_user.id, f'{config.slug}-boleto')
138-
send_purchase_notification_mock.asser_called_once_with(payment.id)
145+
send_purchase_notification_mock.assert_called_once_with(payment.id)
139146

140147

141148
@pytest.mark.parametrize(
@@ -212,4 +219,4 @@ def test_pagarme_payment_refused(db, tag_as_mock, remove_tags_mock, logged_user,
212219
checkout_domain.payment_handler_task(payment.id)
213220
assert remove_tags_mock.called is False
214221
tag_as_mock.assert_called_once_with(logged_user.email, logged_user.id, f'{config.slug}-refused')
215-
send_purchase_notification_mock.asser_called_once_with(payment.id)
222+
send_purchase_notification_mock.assert_called_once_with(payment.id)

0 commit comments

Comments
 (0)