Skip to content

Commit

Permalink
fix: added saas api's to communicate with frappe cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
shariquerik committed Nov 4, 2024
1 parent 71406a4 commit 32c4b24
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
56 changes: 56 additions & 0 deletions crm/api/saas_billing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import frappe
import requests

def get_base_url():
url = "https://frappecloud.com"
if frappe.conf.developer_mode and frappe.conf.get("saas_billing_base_url"):
url = frappe.conf.get("saas_billing_base_url")
return url

def get_site_name():
site_name = frappe.local.site
if frappe.conf.developer_mode and frappe.conf.get("saas_billing_site_name"):
site_name = frappe.conf.get("saas_billing_site_name")
return site_name

def get_headers():
# check if user is system manager
if frappe.get_roles(frappe.session.user).count("System Manager") == 0:
frappe.throw("You are not allowed to access this resource")

# check if communication secret is set
if not frappe.conf.get("fc_communication_secret"):
frappe.throw("Communication secret not set")

return {
"X-Site-Token": frappe.conf.get("fc_communication_secret"),
"X-Site": get_site_name()
}

@frappe.whitelist()
def generate_access_token():
request = requests.post(f"{get_base_url()}/api/method/press.saas.api.auth.generate_access_token", headers=get_headers())
if request.status_code == 200:
return request.json()["message"]
else:
frappe.throw("Failed to generate access token")

@frappe.whitelist()
def is_access_token_valid(token):
headers={ 'Content-Type': 'application/json' }
request = requests.post(f"{get_base_url()}/api/method/press.saas.api.auth.is_access_token_valid", headers, json={ token })
return request.json()["message"]

@frappe.whitelist()
def current_site_info():
request = requests.post(f"{get_base_url()}/api/method/press.saas.api.site.info", headers=get_headers())
if request.status_code == 200:
return request.json().get("message")
else:
frappe.throw("Failed to get site info")

@frappe.whitelist()
def saas_api(method, data={}):
request = requests.post(f"{get_base_url()}/api/method/press.saas.api.{method}", headers=get_headers(), json=data)
print(request.json())
return request.json().get("message")
4 changes: 1 addition & 3 deletions crm/www/crm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from __future__ import unicode_literals
import frappe
from frappe.boot import add_subscription_conf
from frappe.utils import cint
from frappe.utils.telemetry import capture

Expand Down Expand Up @@ -34,8 +33,7 @@ def get_boot():
"site_name": frappe.local.site,
"read_only_mode": frappe.flags.read_only,
"csrf_token": frappe.sessions.get_csrf_token(),
"telemetry_site_age": frappe.utils.telemetry.site_age(),
"subscription_conf": add_subscription_conf(),
"fc_communication_secret": frappe.conf.fc_communication_secret,
"setup_complete": cint(frappe.get_system_settings("setup_complete"))
}
)
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/Layouts/AppSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
</div>
</div>
<TrialBanner
baseAPIPath="crm.api.saas_billing"
:isSidebarCollapsed="isSidebarCollapsed"
@upgradePlan="showBillingSettingPage"
/>
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/components/Settings/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,21 @@ const tabs = computed(() => {
},
{
label: __('Subscription'),
condition: () => window.subscription_conf,
condition: () => window.fc_communication_secret && isManager(),
items: [
{
label: 'Plans',
icon: UpgradeIcon,
component: markRaw(Plans),
component: markRaw(h(Plans, { baseAPIPath: 'crm.api.saas_billing' })),
},
{
label: 'Billing',
icon: WalletsIcon,
component: markRaw(
h(Billing, { onChangePlan: () => setActiveTab('Plans') }),
h(Billing, {
baseAPIPath: 'crm.api.saas_billing',
onChangePlan: () => setActiveTab('Plans'),
}),
),
},
],
Expand Down

0 comments on commit 32c4b24

Please sign in to comment.