summaryrefslogtreecommitdiffstats
path: root/restaurant_orders/core/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'restaurant_orders/core/utils.py')
-rw-r--r--restaurant_orders/core/utils.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/restaurant_orders/core/utils.py b/restaurant_orders/core/utils.py
new file mode 100644
index 0000000..d716a9c
--- /dev/null
+++ b/restaurant_orders/core/utils.py
@@ -0,0 +1,100 @@
1import json
2from woocommerce import API
3from twilio.rest import Client
4from twilio.base.exceptions import TwilioRestException
5
6from django.core.mail import send_mail
7from django.conf import settings
8
9from core.models import Order
10
11class Sender():
12 def __init__(self, order):
13 self.order = order
14
15 def get_order_url(self):
16 order_id = self.order.wp_id
17 order_key = self.order.wp_order_key
18 restaurant_url = self.order.restaurant.wordpress_url
19 return f'{restaurant_url}/zamowienie/order-pay/{order_id}/?pay_for_order=true&key={order_key}'
20
21 def get_message_body(self):
22 return f'Prosze dokonac platnosci: {self.get_order_url()}'
23
24 def send(self) -> (bool, str):
25 pass
26
27
28class SendSms(Sender):
29 def __init__(self, order):
30 account_sid = settings.TWILIO_ACCOUNT_SID
31 auth_token = settings.TWILIO_TOKEN
32
33 self.client = Client(account_sid, auth_token)
34 self.from_ = "+17432007359"
35
36 super().__init__(order)
37
38 def send(self) -> (bool, str):
39 phone = self.order.billing.get('phone', None)
40 phone = "+48609155122"
41 if phone:
42 try:
43 message = self.client.messages.create(to=phone,
44 from_=self.from_,
45 body=self.get_message_body())
46 except TwilioRestException as err:
47 return (False, err.msg)
48 else:
49 return (True, 'Wyslano sms')
50 return (False, 'Nie znaleziono numeru telefonu.')
51
52
53
54class SendMail(Sender):
55 def send(self) -> (bool, str):
56 email = self.order.billing.get('email', None)
57 email = 'jdlugosz963@gmail.com'
58 if email: # Jesli sie spierdoli to wypluje
59 try:
60 send_mail('Strona do zaplaty', self.get_message_body(), 'no-reply@reami.pl', (email, ), fail_silently=False)
61 except smtplib.SMTPException:
62 return (False, "Niestety nie udalo sie wyslac maila.")
63 else:
64 return (True, "Wyslano maila.")
65 return (False, "Nie znaleziono maila.")
66
67
68class Orders:
69 def __init__(self, restaurant, billing):
70 self.restaurant = restaurant
71 self.billing = billing
72
73 self.wcapi = API(
74 url=restaurant.wordpress_url,
75 consumer_key=restaurant.woocommerce_consumer_key,
76 consumer_secret=restaurant.woocommerce_consumer_secret,
77 timeout=7
78 )
79
80 def get_custom_order_data(self, items):
81 return {
82 "payment_method": "bacs",
83 "payment_method_title": "Direct Bank Transfer",
84 "set_paid": False,
85 "billing": self.billing,
86 "shipping": self.billing,
87 "line_items": [
88 {
89 "product_id": pk,
90 "total": total,
91 "quantity": 1,
92 } for pk, total in items
93 ]
94 }
95
96 def create_custom_order(self, items):
97 data = self.get_custom_order_data(items)
98 response = self.wcapi.post("orders", data=data).json()
99 return Order.create_from_response_disable_view(response, self.restaurant)
100