forked from Shopify/sample-django-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
46 lines (37 loc) · 1.34 KB
/
views.py
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
from django.http import JsonResponse
from shopify_app.decorators import session_token_required
from shopify_app.partnerjam import PartnerJamClient
from shopify_app.models import Shop
import shopify
@session_token_required
def products(request):
products = shopify.Product.find()
return JsonResponse({'products': [p.to_dict() for p in products]})
@session_token_required
def orders(request):
orders = shopify.Order.find(status='any')
return JsonResponse({'orders': [o.to_dict() for o in orders]})
@session_token_required
def subscription(request):
full_price = 10
plan_name = 'Standard plan'
shopify_shop = shopify.Shop.current()
shop_record = Shop.objects.get(shopify_domain=shopify_shop.myshopify_domain)
discount = PartnerJamClient.get_discount(shop_record.partnerjam_token)
if discount:
price = full_price * ((100 - discount) / 100)
plan = f"{plan_name} - {str(discount)}% discount"
else:
price = full_price
plan = plan_name
charge = shopify.RecurringApplicationCharge.create(
{
"name": plan,
"price": str(price),
"return_url": request.build_absolute_uri('/'),
"trial_days": 3,
"test": True,
}
)
url = charge.confirmation_url
return JsonResponse({'url': url })