summaryrefslogtreecommitdiffstats
path: root/restaurant_orders/core/utils.py
blob: d716a9c06a58bef46d22f096740061b348b69e68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
from woocommerce import API
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException

from django.core.mail import send_mail
from django.conf import settings

from core.models import Order

class Sender():
    def __init__(self, order):
        self.order = order

    def get_order_url(self):
        order_id = self.order.wp_id
        order_key = self.order.wp_order_key
        restaurant_url = self.order.restaurant.wordpress_url
        return f'{restaurant_url}/zamowienie/order-pay/{order_id}/?pay_for_order=true&key={order_key}'

    def get_message_body(self):
        return f'Prosze dokonac platnosci: {self.get_order_url()}'

    def send(self) -> (bool, str):
        pass


class SendSms(Sender): 
    def __init__(self, order):
        account_sid = settings.TWILIO_ACCOUNT_SID
        auth_token  = settings.TWILIO_TOKEN
        
        self.client = Client(account_sid, auth_token)
        self.from_ = "+17432007359"
        
        super().__init__(order)

    def send(self) -> (bool, str):
        phone = self.order.billing.get('phone', None)
        phone = "+48609155122"
        if phone:
            try:
                message = self.client.messages.create(to=phone,
                                                      from_=self.from_,
                                                      body=self.get_message_body())
            except TwilioRestException as err:
                return (False, err.msg)
            else:
                return (True, 'Wyslano sms')
        return (False, 'Nie znaleziono numeru telefonu.')



class SendMail(Sender):
    def send(self) -> (bool, str):
        email = self.order.billing.get('email', None)
        email = 'jdlugosz963@gmail.com'
        if email:  # Jesli sie spierdoli to wypluje  
            try:
                send_mail('Strona do zaplaty', self.get_message_body(), 'no-reply@reami.pl', (email, ), fail_silently=False)
            except smtplib.SMTPException:
                return (False, "Niestety nie udalo sie wyslac maila.")
            else:
                return (True, "Wyslano maila.")
        return (False, "Nie znaleziono maila.")


class Orders:
    def __init__(self, restaurant, billing):
        self.restaurant = restaurant
        self.billing = billing

        self.wcapi = API(
            url=restaurant.wordpress_url,
            consumer_key=restaurant.woocommerce_consumer_key,
            consumer_secret=restaurant.woocommerce_consumer_secret,
            timeout=7
        )

    def get_custom_order_data(self, items):
        return {
            "payment_method": "bacs",
            "payment_method_title": "Direct Bank Transfer",
            "set_paid": False,
            "billing": self.billing,
            "shipping": self.billing,
            "line_items": [
                {
                    "product_id": pk,
                    "total": total,
                    "quantity": 1,
                } for pk, total in items
            ]
        }

    def create_custom_order(self, items):
        data = self.get_custom_order_data(items)
        response = self.wcapi.post("orders", data=data).json()
        return Order.create_from_response_disable_view(response, self.restaurant)