def platform_shopify_update_product(): """ Currently this works with the Shopify API From Webhook - update product "topic": "products\/update" """ payload = get_post_payload() platform_product_id = str(payload.get('id', '')) product_title = payload.get('title') shopify_shop_domain = request.headers.get('X-Shopify-Shop-Domain') shop = models.Shop.query.filter_by(domain=shopify_shop_domain).first() if not shop: raise exceptions.DbException('no such shop %s' % shopify_shop_domain) product = models.Product.query.filter_by( platform_product_id=platform_product_id, shop_id=shop.id).first() if not product: raise exceptions.DbException('no such product %s in shop %s' % (platform_product_id, shop.id)) product.name = product_title db.session.add(product) db.session.commit() return jsonify({}), 200
def platform_shopify_create_product(): """ Currently this works with the Shopify API From Webhook - create product "topic": "products\/create" """ payload = get_post_payload() shopify_shop_domain = request.headers.get('X-Shopify-Shop-Domain') shop = models.Shop.query.filter_by(domain=shopify_shop_domain).first() if not shop: raise exceptions.DbException('no such shop %s' % shopify_shop_domain) platform_product_id = str(payload.get('id', '')) existing_product = models.Product.query.filter_by( platform_product_id=platform_product_id).first() if existing_product: raise exceptions.DbException('Product already exists', status_code=401) product_title = payload.get('title') product = models.Product(name=product_title, shop=shop, platform_product_id=platform_product_id) db.session.add(product) db.session.commit() return build_created_response('client.get_product', product_id=product.id)
def platform_shopify_app_uninstalled(): shopify_shop_domain = request.headers.get('X-Shopify-Shop-Domain') shop = models.Shop.query.filter_by(domain=shopify_shop_domain).first() if not shop: raise exceptions.DbException('no such shop %s' % shopify_shop_domain) shop_customer = shop.owner.customer[0] shop_customer.active = False db.session.add(shop_customer) subscription = shop_customer.subscription[0] subscription.cancel() db.session.add(subscription) # revoke tasks for order in shop.orders: for task in order.tasks: task.revoke() db.session.add(task) shop.active = False shop.access_token = None db.session.add(shop) db.session.commit() return jsonify({}), 200
def platform_shopify_create_order(): payload = get_post_payload() shopify_shop_domain = request.headers.get('X-Shopify-Shop-Domain') shop = models.Shop.query.filter_by(domain=shopify_shop_domain).first() if not shop: raise exceptions.DbException('no such shop %s' % shopify_shop_domain) platforms.create_order(shop, payload) return jsonify({}), 201
def create_order(self, payload): platform_order_id = str(payload.get('id', '')) existing_order = models.Order.query.filter_by( platform_order_id=platform_order_id).first() if existing_order: raise exceptions.DbException('Order already exists', status_code=401) try: created_at_dt = date_parser.parse( payload.get('created_at')).astimezone( pytz.utc).replace(tzinfo=None) except: created_at_dt = datetime.datetime.utcnow() browser_ip = str(payload.get('browser_ip', '')) order = models.Order(platform_order_id=platform_order_id, shop=self.shop, purchase_timestamp=created_at_dt, browser_ip=browser_ip) # TODO: Extract to ShopifyOpinewAdapter # try to speculatively find a FunnelStream to match for this order - from this browser IP, latest funnel_stream = None if browser_ip: funnel_stream = models.FunnelStream.query.filter_by( plugin_loaded_from_ip=browser_ip).order_by( models.FunnelStream.plugin_load_ts.desc()).first() if funnel_stream: funnel_stream.order = order db.session.add(funnel_stream) customer_email = payload.get('customer', {}).get('email') customer_name = "%s %s" % (payload.get('customer', {}).get( 'first_name', ''), payload.get('customer', {}).get( 'last_name', '')) existing_user = models.User.get_by_email_no_exception(customer_email) if existing_user: order.user = existing_user else: user_legacy, _ = models.UserLegacy.get_or_create_by_email( customer_email, name=customer_name) order.user_legacy = user_legacy line_items = payload.get('line_items', []) for line_item in line_items: platform_product_id = str(line_item.get('product_id')) product = models.Product.query.filter_by( platform_product_id=platform_product_id, shop=self.shop).first() if product: order.products.append(product) else: variant = models.ProductVariant.query.filter_by( platform_variant_id=str(line_item.get( 'variant_id'))).first() if not variant: continue order.products.append(variant.product) order.products.append(product) db.session.add(order) db.session.commit()