This topic outlines best practices for constructing and submitting baskets, and provides high-level guidance on how to approach payment processing with SCAPI.
Never pass payment card data to B2C Commerce. Instead, have the shopper provide payment card data directly to the payment provider using the provider’s script/iframe, and pass the token returned from the provider to the API. Validate the token, and if the token is valid, accept the order, otherwise reject it.
This article assumes that you are interacting with a payment gateway from which you get a payment token.
A strategy for how you will create orders. In SCAPI, the recommended strategy is to create an order before you validate payment.
A SLAS private client or SLAS public client. The code examples provided in this topic use a private client. For details, see Create a SLAS Client.
Build a Basket
You have two options for building baskets:
Build a basket incrementally
Build a basket with a single request
Build a Basket Incrementally
When you build a basket incrementally, you enter the information as the shopper provides it. This persists the basket in the B2C Commerce backend, and is useful if you can’t easily persist the basket.
It also performs validation and returns API errors as you go, as shown in the following code examples. Note that the comments included in the code examples are provided for clarity and must be removed for the code to work.
1#!/bin/bash2set -euo pipefail34CODE='kv7kzm78'5ORG='f_ecom_zzrf_001'6SITE='RefArchGlobal'7CLIENT=8SECRET=910BASE="https://$CODE.api.commercecloud.salesforce.com"11BASE_AUTH="$BASE/shopper/auth/v1/organizations/$ORG"12BASE_BASKETS="$BASE/checkout/shopper-baskets/v1/organizations/$ORG"1314# 1. Get guest token (Note: The following code provides a rough flow using a private client. The commands for getting a token for a public client differ.)15TOKEN=$(16 curl "$BASE_AUTH/oauth2/token" \17 -sS --fail-with-body \18 -u "$CLIENT:$SECRET" \19 -d 'grant_type=client_credentials' | jq -r .'access_token'20)2122# 2. Create basket (Note: When you create a basket, you store a reference to the basket, and use the reference in subsequent calls.)23BASKET=$(24 curl "$BASE_BASKETS/baskets?siteId=$SITE" \25 -sS --fail-with-body \26 -H "Authorization: Bearer $TOKEN" \27 -H "Content-Type: application/json" \28 -d '{ "productItems": [{ "quantity": 1, "productId": "682875090845M"}]}' | jq -r '.basketId'29)3031# 3. Set basket customer32curl "$BASE_BASKETS/baskets/$BASKET/customer?siteId=$SITE" \33 -sS --fail-with-body -o /dev/null \34 -X 'PUT' \35 -H "Authorization: Bearer $TOKEN" \36 -H "Content-Type: application/json" \37 -d '{ "email": "shopper@salesforce.com" }'3839# 4. Set shipping address. All baskets have a shipment called `me`. For multiple shipments, you must create those shipments yourself. See [createShipmentForBasket](https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-baskets?meta=createShipmentForBasket).40curl "$BASE_BASKETS/baskets/$BASKET/shipments/me?siteId=$SITE" \41 -sS --fail-with-body -o /dev/null \42 -X 'PATCH' \43 -H "Authorization: Bearer $TOKEN" \44 -H "Content-Type: application/json" \45 -d '{46 "shippingAddress": {47 "firstName": "Joe",48 "lastName": "Shopper",49 "address1": "415 Mission St.",50 "city": "San Francisco",51 "postalCode": "94105",52 "stateCode": "CA",53 "countryCode": "US"54 }55 }'5657# 5. Get applicable shipping methods (Note: This example defaults to the first shipping method that is available.)58SHIPPING_METHOD=$(59 curl "$BASE_BASKETS/baskets/$BASKET/shipments/me/shipping-methods?siteId=$SITE" \60 -sS --fail-with-body \61 -H "Authorization: Bearer $TOKEN" | jq -r '.applicableShippingMethods[0].id'62)6364# 6. Set shipping method65curl "$BASE_BASKETS/baskets/$BASKET/shipments/me/shipping-method?siteId=$SITE" \66 -sS --fail-with-body -o /dev/null \67 -X 'PUT' \68 -H "Authorization: Bearer $TOKEN" \69 -H "Content-Type: application/json" \70 -d '{"id": "'$SHIPPING_METHOD'"}'7172# 7. Set payment instrument (Note: This example uses PayPal.)73curl "$BASE_BASKETS/baskets/$BASKET/payment-instruments?siteId=$SITE" \74 -sS --fail-with-body -o /dev/null \75 -H "Authorization: Bearer $TOKEN" \76 -H "Content-Type: application/json" \77 -d '{ "paymentMethodId": "PayPal" }'7879# 8. Set billing address80curl "$BASE_BASKETS/baskets/$BASKET/billing-address?siteId=$SITE&useAsShipping=false" \81 -sS --fail-with-body -o /dev/null \82 -X 'PUT' \83 -H "Authorization: Bearer $TOKEN" \84 -H "Content-Type: application/json" \85 -d '{86 "firstName": "Joe",87 "lastName": "Shopper",88 "address1": "415 Mission St",89 "city": "San Francisco",90 "postalCode": "94105",91 "stateCode": "CA",92 "countryCode": "US"93 }'9495# 9. Submit order96curl "$BASE/checkout/shopper-orders/v1/organizations/$ORG/orders?siteId=$SITE" \97 -sS --fail-with-body \98 -H "Authorization: Bearer $TOKEN" \99 -H "Content-Type: application/json" \100 -d '{ "basketId": "'$BASKET'"}' | jq
Build Basket with a Single Request
Create the basket with just one request, and provide the relevant parts you want to fill.
The following example builds an entire basket and its nested documents with one request: