Skip to content

Instantly share code, notes, and snippets.

@z1haze
Created January 29, 2024 16:31
Show Gist options
  • Save z1haze/febf2e558b1fe1202f3737a43ef930a6 to your computer and use it in GitHub Desktop.
Save z1haze/febf2e558b1fe1202f3737a43ef930a6 to your computer and use it in GitHub Desktop.
passwordless-login-with-merge-basket.sh
#!/bin/bash
# Passwordless Login with Merge Basket!
# https://developer.salesforce.com/docs/commerce/commerce-api/references?meta=shopper-login:authorizePasswordlessCustomer
# Client needs additional scope `sfcc.pwdless_login`.
set -euo pipefail
CODE='kv7kzm78'
ORG='f_ecom_zzrf_001'
CLIENT='d125886c-c45a-49f5-acf5-08e48617b66b'
SECRET=
SITE='RefArch'
CALLBACK=
USER='[email protected]'
BASE_SCAPI="https://$CODE.api.commercecloud.salesforce.com"
BASE_AUTH="$BASE_SCAPI/shopper/auth/v1/organizations/$ORG"
BASE_BASKET="$BASE_SCAPI/checkout/shopper-baskets/v1/organizations/$ORG"
# 1. Get a Guest Token
echo "--> $BASE_AUTH/oauth2/token"
RESPONSE=$(
curl "$BASE_AUTH/oauth2/token" \
-sS --fail-with-body \
-u "$CLIENT:$SECRET" \
-d "channel_id=$SITE" \
-d 'grant_type=client_credentials'
)
TOKEN=$(echo $RESPONSE | jq -r '.access_token')
USID=$(echo $RESPONSE | jq -r '.usid')
# 2. Create a basket.
echo "--> POST $BASE_BASKET/baskets?siteId=$SITE"
curl "$BASE_BASKET/baskets?siteId=$SITE" \
-sS --fail-with-body \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{ "productItems": [{ "quantity": 1, "productId": "682875090845M"}]}' |
jq -r '.basketId'
# 3. Begin Passwordless login w/ USID.
echo "--> POST $BASE_AUTH/oauth2/passwordless/login"
curl "$BASE_AUTH/oauth2/passwordless/login" \
-sS --fail-with-body \
-u "$CLIENT:$SECRET" \
-d "channel_id=$SITE" \
-d "user_id=$USER" \
-d "usid=$USID" \
-d 'mode=callback' \
-d "callback_uri=$CALLBACK"
echo -en "\n--> Enter Token: "
read PTOKEN
# 4. Complete Passwordless login by providing the token.
echo -e "\n--> POST oauth2/passwordless/token"
RESPONSE=$(
curl "$BASE_AUTH/oauth2/passwordless/token" \
-sS --fail-with-body \
-u "$CLIENT:$SECRET" \
-d 'grant_type=client_credentials' \
-d 'hint=pwdless_login' \
-d "pwdless_login_token=$PTOKEN"
)
RTOKEN=$(echo $RESPONSE | jq -r '.access_token')
# 5. Merge the basket. We expect our basket!
echo "--> POST baskets/actions/merge?siteId=$SITE&createDestinationBasket=true"
curl "$BASE_BASKET/baskets/actions/merge?siteId=$SITE&createDestinationBasket=true" \
-sS --fail-with-body \
-X 'POST' \
-H "Authorization: Bearer $RTOKEN" \
-H 'Content-Type: application/json' |
jq '.productItems | length'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment