Skip to content

Commit

Permalink
feat: add checkout session to embed token (#23)
Browse files Browse the repository at this point in the history
* add checkout session to embed token

* updated read me

* update version
  • Loading branch information
phillipgr4vy authored Jun 29, 2023
1 parent 90d6661 commit f10f163
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 181 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,38 @@ needs to be created before it can be used in this way.
buyer_request = {"display_name": "Jane Smith"}

new_buyer = client.create_new_buyer(**buyer_request).get('id')
embed = {
embed_data = {
"amount": 1299,
"currency": "USD",
"buyerId": new_buyer,
}

embed_token = client.generate_embed_token(embed)
embed_token = client.generate_embed_token(embed_data=embed_data)

print("Embed token: {}".format(embed_token))
```
Checkout sessions can also be passed within an Embed token:

```python
from gr4vy import Gr4vyClient

client = Gr4vyClient("gr4vy_instance","private_key.pem", "production")

checkout_session_id = client.create_new_checkout_session().get("id")

embed_data = {
"amount": 1299,
"currency": "USD",
}

emebed_token = client.generate_embed_token(
embed_data=embed_data, checkout_session_id=checkout_session_id
)

print("Embed token: {}".format(embed_token))
```



## Initialization

Expand Down
2 changes: 1 addition & 1 deletion gr4vy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .gr4vy_client import Gr4vyClient, Gr4vyClientWithBaseUrl
from .gr4vy_client import Gr4vyClient, Gr4vyClientWithBaseUrl
38 changes: 22 additions & 16 deletions gr4vy/gr4vy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def __call__(self, r):


class Gr4vyError(Exception):
def __init__(
self, message, details, http_status_code
) -> None:
def __init__(self, message, details, http_status_code) -> None:

super().__init__(
f"Error Reason: {message} \n Error Details: {details} \n HTTP Status Code: {http_status_code}"
Expand All @@ -44,11 +42,15 @@ def __init__(


class Gr4vyClient:
def __init__(self, gr4vyId, private_key_file, environment, merchant_account_id=None):
def __init__(
self, gr4vyId, private_key_file, environment, merchant_account_id=None
):
self.gr4vyId = gr4vyId
self.private_key_file = private_key_file
self.environment = environment
self.merchant_account_id = merchant_account_id if merchant_account_id else "default"
self.merchant_account_id = (
merchant_account_id if merchant_account_id else "default"
)
self.session = requests.Session()
self.base_url = self._generate_base_url()
self.token = self.generate_token()
Expand Down Expand Up @@ -79,7 +81,9 @@ def _generate_base_url(self):
base_url = "https://api.{}.gr4vy.app".format(self.gr4vyId)
return base_url

def generate_token(self, scopes=["*.read", "*.write"], embed_data=None):
def generate_token(
self, scopes=["*.read", "*.write"], embed_data=None, checkout_session_id=None
):
private_key, kid = self._private_key_file_to_string()
data = {
"iss": "Gr4vy SDK {} - {}".format(VERSION, PYTHON_VERSION),
Expand All @@ -91,6 +95,8 @@ def generate_token(self, scopes=["*.read", "*.write"], embed_data=None):
if embed_data:
data["embed"] = embed_data
data["scopes"] = ["embed"]
if checkout_session_id:
data["checkout_session"] = checkout_session_id
token = api_jwt.encode(
data, private_key, algorithm="ES512", headers={"kid": kid}
)
Expand Down Expand Up @@ -120,12 +126,15 @@ def _request(

params = self._prepare_params(params) if params else params

headers = {
"X-GR4VY-MERCHANT-ACCOUNT-ID": self.merchant_account_id
}
headers = {"X-GR4VY-MERCHANT-ACCOUNT-ID": self.merchant_account_id}

response = self.session.request(
method, url, params=query, json=params, auth=BearerAuth(self.token), headers=headers
method,
url,
params=query,
json=params,
auth=BearerAuth(self.token),
headers=headers,
)

try:
Expand All @@ -152,8 +161,8 @@ def _thumbprint(self, jwk: dict) -> str:
digest.update(json_claims.encode("utf8"))
return self._b64e(digest.finalize())

def generate_embed_token(self, embed_data):
token = self.generate_token(embed_data=embed_data)
def generate_embed_token(self, embed_data, checkout_session_id=None):
token = self.generate_token(embed_data=embed_data, checkout_session_id=None)
return token

def list_audit_logs(self, **kwargs):
Expand Down Expand Up @@ -393,7 +402,7 @@ def list_role_assignments(self, **kwargs):
return response

def create_new_role_assignment(self, **kwargs):
response = self._request("get", "/roles/assignments", query=kwargs)
response = self._request("post", "/roles/assignments", params=kwargs)
return response

def delete_role_assignment(self, role_assignment_id):
Expand All @@ -408,6 +417,3 @@ def list_api_error_logs(self, **kwargs):
class Gr4vyClientWithBaseUrl(Gr4vyClient):
def __init__(self, base_url, private_key, environment):
super().__init__(base_url, private_key, environment)

#client = Gr4vyClient("spider", "private_key.pem", "sandbox")
#print(client.list_audit_logs())
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "gr4vy"
version = "0.11.0"
version = "0.12.0"
description = "Python SDK for Gr4vy"
authors = ["Gr4vy <code@gr4vy.com>"]
readme = "README.md"
Expand Down
Loading

0 comments on commit f10f163

Please sign in to comment.