Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Punchh Dynamic Code Generation liquid #7828

Merged
merged 6 commits into from
Aug 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,103 +33,87 @@ The JWT.IO library decodes, verifies, and generates JSON web tokens, an open, in

The following `ClaimType` names can be used to ensure the uniqueness of guests and coupons:

- `campaign_id`: represents the system-generated Punchh campaign ID.
- `email`: represents the user's email address.
- `campaign_id`: represents the system-generated Punchh campaign ID.
- `first_name`: captures the user's first name.
- `last_name`: captures the user's last name.

To use the Punchh dynamic coupon code API, a JWT Token must be constructed. Add the following Liquid template to your Braze dashboard in the message body of the channel you'd like to use:

{% raw %}

```liquid
{% capture header %}{"alg":"HS256","typ":"JWT"}{% endcapture %}
{% assign header = '{"alg":"HS256","typ":"JWT"}' | base64_encode | replace: '=', '' | replace: '+', '-' | replace: '/', '_' %}

{% capture payload_raw %}

{
"campaign_id": "CAMPAIGN_ID",
"email": "{{${email_address}}}",
"first_name": "{{${first_name}}}",
"last_name": "{{${last_name}}}"
}

{% endcapture %}

{% capture payload %}{"campaign_id":"CAMPAIGN_ID","email":"{{${email_address}}}","first_name":"{{${first_name}}}","last_name":"{{${last_name}}}"}{% endcapture %}
{% assign payload = payload_raw | replace: ' ', '' | replace: '\n', '' | base64_encode | replace: '=', '' | replace: '+', '-' | replace: '/', '_' %}

{% capture signature_structure %}{{header | base64_encode}}.{{payload | base64_encode | remove: '='}}{% endcapture %}
{% assign unsigned_token = header | append: "." | append: payload %}

{% assign secret = "DYNAMIC_COUPON_GENERATION_TOKEN" %}

{% assign final_signature = {{signature_structure | hmac_sha256_base64: secret}} %}
{% assign signature_raw = unsigned_token | hmac_sha256_base64: secret %}

{% capture jwt %}{{signature_structure}}.{{final_signature | remove: '='}}{% endcapture %}
{% assign signature = signature_raw | replace: '=', '' | replace: '+', '-' | replace: '/', '_' %}

{% assign jwt = unsigned_token | append: "." | append: signature %}

```
{% endraw %}


Replace the following:

| Placeholder | Description |
|--------------------|------------------------------------------------------|
| `SERVER_NAME` | The name of your server. |
| `DYNAMIC_COUPON_GENERATION_TOKEN` | Your dynamic coupon generation token. |
| `CAMPAIGN_ID` | Your campaign ID. |

### Step 3: Append coupon code to message body

#### Link to Punchh web page

To link to a Puncch-hosted web page, add the following snippet to your message body.
To link to a Puncch-hosted web page, add the Dynamic Generation URL provided in Punchh Campaign creation UI and replace `GENERATED_SIGNATURE` with `{{jwt}}`:

{% raw %}
```liquid
https://SERVER_NAME.punchh.com/request_coupons/DYNAMIC_COUPON_GENERATION_TOKEN?sign={{jwt}}
```
{% endraw %}
https://example.punchh.com/request_coupons/XXXXXXXXXXXXX?sign={{jwt}}
```

Replace the following:

| Placeholder | Description |
|--------------------|------------------------------------------------------|
| `SERVER_NAME` | The name of your server. |
| `DYNAMIC_COUPON_GENERATION_TOKEN` | Your dynamic coupon generation token. |

When a user clicks the coupon URL, they'll be redirected to a Punchh-hosted web page, where their generated coupon will be displayed.

![Example confirmation message after a user successfully generates a coupon code.][1]

#### Extract code via JSON as plain text

To return a JSON response, add `.json` before the sign query parameter of the Punchh URL as shown in the following snippet:
To return a JSON response, add `.json` before the sign query parameter of the Dynamic Generation URL as shown in the following snippet example:

{% raw %}
```liquid
https://SERVER_NAME.punchh.com/request_coupons/DYNAMIC_COUPON_GENERATION_TOKEN.json?sign={{jwt}}
https://example.punchh.com/request_coupons/XXXXXXXXXXXXX.json?sign={{jwt}}
```
{% endraw %}

Replace the following:

| Placeholder | Description |
|--------------------|------------------------------------------------------|
| `SERVER_NAME` | The name of your server. |
| `DYNAMIC_COUPON_GENERATION_TOKEN` | Your dynamic coupon generation token. |

You could then leverage [Connected Content]({{site.baseurl}}/user_guide/personalization_and_dynamic_content/connected_content/making_an_api_call/) to insert the code as plain text into any message body. For example:

{% raw %}
```liquid
{% connected_content https://SERVER_NAME.punchh.com/request_coupons/DYNAMIC_COUPON_GENERATION_TOKEN.json?sign={{jwt}} :save punchh_coupon %}
{% connected_content https://example.punchh.com/request_coupons/XXXXXXXXXXXXX.json?sign={{jwt}} :save punchh_coupon %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its ok if we change the variable names from the original, but lets make sure that these are descriptive variables instead of example or XXXXXXXXXXXXX. Additionally, the variable should be all caps (as that is apart of our style guide).

{{punchh_coupon.coupon}}
```
{% endraw %}
````

#### Link image inside email content

To link the coupon code inside an image, add the following snippet to your message body:
To link the coupon code inside an image, add `.png` before the sign query parameter of the Dynamic Generation URL as shown in the following snippet example:

{% raw %}
```liquid
<img src="https://SERVER_NAME.punchh.com/request_coupons/DYNAMIC_COUPON_GENERATION_TOKEN.png?sign={{jwt}}">`
```
{% endraw %}

Replace the following:

| Placeholder | Description |
|--------------------|------------------------------------------------------|
| `SERVER_NAME` | The name of your server. |
| `DYNAMIC_COUPON_GENERATION_TOKEN` | Your dynamic coupon generation token. |
<img src="https://example.punchh.com/request_coupons/XXXXXXXXXXXXX.png?sign={{jwt}}">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its ok if we change the variable names from the original, but lets make sure that these are descriptive variables instead of example or XXXXXXXXXXXXX. Additionally, the variable should be all caps (as that is apart of our style guide).

````

Your output will be similar to the following:

Expand Down