Overview

The Loyalty Offers service allows you to create an offer in SAP Hybris Loyalty after coupon details, iBeacon details, and geofence details are provided. Once these coupons are created in the system, based on the certain attributes (iBeacon/geofence) information, notifications are sent to users with offer details. The following two types of offers are:

  • Push offer - Email is sent to a customer with an offer code when a particular condition is met.
  • Pull offer - Mobile app will request for valid offers for customers when they hit the specified geofence radius.


API Reference

/offers

This resourceType defines the GET, POST, DELETE methods and their responses for a collection resource.

/offers

get

Gets all Offer details. No coupon details are retrieved.

Accepted scopes:

  • 'sap.loyoffer.view' - Required to view Offers
post

Creates a new Offer in the system. Need to pass offer and coupon details.

Details:

  • Offer Code should match with Coupon Code
  • Coupon gets created first and then offer gets created
  • Offer data is replicated into elastic search

Accepted scopes:

  • 'sap.loyoffer.manage' - Required to create new Offers

/offers/aggr

get

Gets Average, Sum and Count for Loyalty Offers

Accepted scopes:

  • 'sap.loyoffer.view' - Required to view aggregation

/offers/laasOffer/{laasOfferId}

This resource type defines the GET, PUT, DELETE methods and their responses for a single element resource.

get

Gets an Offer details along with corresponding coupon data

Accepted scopes:

  • 'sap.loyoffer.view' - Required to view Offer details
put

Updates a single laasOffer entity.

delete

Deletes a single laasOffer entity.

/offers/{offerId}

This resource type defines the GET, PUT, DELETE methods and their responses for a single element resource.

get

Gets an Offer details. No coupon details are retrieved.

Accepted scopes:

  • 'sap.loyoffer.view' - Required to view Offer details
put

Updates an Offer details.

Accepted scopes:

  • 'sap.loyoffer.manage' - Required to update Offers
delete

Deletes an Offer

Accepted scopes:

  • 'sap.loyoffer.delete' - Required to soft delete Offers

/offers/index

post

Creates all Offer indices required for Elastic Search

Accepted scopes:

  • 'sap.loyoffer.manage' - Required to create elastic search indices for Offers

/locationOffers

This resourceType defines the POST, PUT methods and their responses for a collection resource.

/locationOffers

post

Gets Offers based on Geo Location like longitude, latitude and radius. Additionally, memberId can also be passed to fetch offers for a particular memberId

Accepted scopes:

  • 'sap.loyoffer.manage' - Required to query Offers

/beaconOffers

This resourceType defines the POST, PUT methods and their responses for a collection resource.

/beaconOffers

post

Gets Offers based on Beacon Information like Id, majorId and minorId. Additionally, memberId can also be passed to fetch offers for a particular memberId

Accepted scopes:

  • 'sap.loyoffer.manage' - Required to query Offers

/migration

/migration

get

Migrates offer data to relevant format for the latest release

Accepted scopes:

  • 'sap.loyoffer_view' - Required to do read operations


Error Types

For more information about error codes, see the detailed error description for each API operation in API Reference. You can also check the standard error codes at API best practices.


Scopes

Scopes are strings that let you specify exactly what type of access you need to resources and operations in the Loyalty offer service.

You must provide a proper scope that enables users to perform certain operations. The scopes should be granted in an access token from OAuth 2.0 service. For more information about the authorization and authentication used in hybris services, and also about the scopes in general, see: Authorization Scopes

The table presents all the scopes supported by the Loyalty Offer service.

ScopeDescription
sap.loyoffer_viewUse this scope to view existing offers.
sap.loyoffer_manageUse this scope to create or modify existing offers.
sap.loyoffer_deleteUse this scope to delete an existing offer.


Perform Simple CRUD Operations on Offers Object

Introduction

With the Loyalty Offer service you can do all the necessary operations to manage your offers: create, retrieve, update, and delete. To perform the basic operations supported by the iBeacon service, you need to be properly authorized. This is achieved through the header that carries the correct access token. The examples in this tutorial will lead you through those operations, showing what you need to provide to the methods, and what you get in return.

clientId = clientIdPlaceholder;
clientSecret = clientSecretPlaceholder;
tenant = projectIdPlaceholder;
client = appIdPlaceholder;
scopesRequired = 'hybris.tenant='+tenant+' sap.loyoffer_view sap.loyoffer_manage sap.loyoffer_delete';
token = tokenPlaceholder;

Get access token

To perform any operations with a specific service, you always need an access token. For this purpose, create an API Client for the oAuth2 service:

API.createClient('oAuth2Service',
'/services/oauth2/v1/api.raml');

Now get the token:

AccessToken = oAuth2Service.token.post({
  'client_id' : clientId,
  'client_secret': clientSecret,
  'grant_type' : 'client_credentials',
  'token_type': 'Bearer',
  'scope': scopesRequired
});
access_token = AccessToken.body.access_token;

Create API client for Loyalty Offer service

API.createClient('offerService',
'/services/loyaltyoffer/v1/api.raml');
randomStringCouponCode = function() {
                    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
                    var stringLength = 8;
                    var randomstring = '';
                    for (var i = 0; i < stringLength; i++) {
                        var rnum = Math.floor(Math.random() * chars.length);
                        randomstring += chars.substring(rnum, rnum + 1);
                    }
                    return randomstring;
                }

offerCouponCode = randomStringCouponCode();

Create an Offer object

offer_obj = offerService.offers.post({
'laasOffer':
{
  'offerId':'f01c2b4295ef4d9284d2e0e8566da8b0',
  'offerType':'COUPON',
  'offerCode':offerCouponCode,
  'name':
  { 
     'en': 'API Tutorial Offer'
   },
  'description': 
  {
    'en': 'Playbook API tutorial offer'
  },
  'validFrom':'2016-04-01T00:00:00.000Z',
  'validTo':'2016-12-31T00:00:00.000Z',
  'emailTemplateId': 'GenericOfferEmail',
  'couponTemplateId': '2222',
  'targetGroup': ['Silver' ],
  'enableGeoMarketing': true,
  'geoFence': [ 'bdc44400241e44fd8cf0f8f867aff5bf',
               'c03c1052320d41e1aad653a66c8baf87',
               'e7ac5f85ce1548e3a3b25cf7210506e6',
               '38a33775fd224c4e8e14bc64b725cbf2' ],
  'beacon':
  [
    {
      'id':'B9407F30-F5F8-466E-AFF9-25556B57FE6D',
      'majorId':8000,
      'minorId':100
    },
   {
      'id':'B9407F30-F5F8-466E-AFF9-25556B57FE6D',
      'majorId':8000,
      'minorId':200
    }
  ]
},
'coupon':
  {
  'code': offerCouponCode,
  'name': offerCouponCode,
  'discountType': 'PERCENT',
  'discountPercentage': '10',
  'allowAnonymous': false,
  'restrictions': {
      'validFrom': '2016-03-29T14:55:27.763Z',
      'validUntil': '2016-12-31T14:55:27.764Z'
  },
  'status': 'VALID'
}
}, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           }
    }
  )

Retrieve object created with the previous step

  path = offer_obj.headers.location.split('/').reverse();
  offerId = path[0];
  offerService.offers.offerId(offerId).get(null, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           }
    }
  )

Update the Offer object

Perform a partial update on the object:

offerService.offers.offerId(offerId).put({
  'laasOffer':
{
  'offerId':'f01c2b4295ef4d9284d2e0e8566da8b0',
  'offerType':'COUPON',
  'offerCode':'JX1AQ127',
  'name':
  { 
     'en': 'API Tutorial Offer update'
   },
  'description': 
  {
    'en': 'Playbook API tutorial offer'
  },
  'validFrom':'2016-04-01T00:00:00.000Z',
  'validTo':'2016-12-31T00:00:00.000Z'
}
}, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           },
  query: {
    'partial' : true
  }
    }
  )

Retrieve the same Offer object to ensure that proper information is updated

Get the Offer object to make sure that it was updated.

offerService.offers.offerId(offerId).get(null, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           }
    }
  )

Retrieve the same Offer object to ensure that proper information is updated

Get the Offer object to make sure that it was updated.

offerService.offers.laasOffer.laasOfferId(offerId).get(null, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           }
    }
  )

Delete the above Offer Object

offerService.offers.offerId(offerId).delete(null, {
  headers: {
   'Authorization': 'Bearer ' + access_token,
  }
})

Retrieve the deleted offer object to ensure the offer status is set to INACTIVE (soft delete)

offer_object = offerService.offers.offerId(offerId).get(null, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           }
    }
  )
offer_object.body.laasOffer.offerStatus


Get offers based on geofence Location

Introduction

With the Loyalty Location Offer service, we can get offers based on geofence details:

clientId = clientIdPlaceholder;
clientSecret = clientSecretPlaceholder;
tenant = projectIdPlaceholder;
client = appIdPlaceholder;
scopesRequired = 'hybris.tenant='+tenant+' sap.loyoffer_view sap.loyoffer_manage sap.loyoffer_delete';
token = tokenPlaceholder;

Get access token

To perform any operations with a specific service, you always need an access token. For this purpose, create an API client for the oAuth2 service:

API.createClient('oAuth2Service',
'/services/oauth2/v1/api.raml');

Now get the token:

AccessToken = oAuth2Service.token.post({
  'client_id' : clientId,
  'client_secret': clientSecret,
  'grant_type' : 'client_credentials',
  'token_type': 'Bearer',
  'scope': scopesRequired
});
access_token = AccessToken.body.access_token;

Create API client for Loyalty Offer service

API.createClient('locationOfferService',
'/services/loyaltyoffer/v1/api.raml');

Get Offers based on geofence Location.

Change latitude, longitude and radius values as per your project while testing end point.

Location_obj = locationOfferService.locationOffers.post({
    'latitude':48.1333,
    'longitude': 11.5667,
    'radius':5000
}, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           }
    }
  )


Get Offers based on IBeacon

Introduction

With the Loyalty iBeacon Offer service, we can get offers based on iBeacon details:

clientId = clientIdPlaceholder;
clientSecret = clientSecretPlaceholder;
tenant = projectIdPlaceholder;
client = appIdPlaceholder;
scopesRequired = 'hybris.tenant='+tenant+' sap.loyoffer_view sap.loyoffer_manage sap.loyoffer_delete';
token = tokenPlaceholder;

Get access token

To perform any operations with a specific service, you always need an access token. For this purpose, create an API client for the oAuth2 service:

API.createClient('oAuth2Service',
'/services/oauth2/v1/api.raml');

Now get the token:

AccessToken = oAuth2Service.token.post({
  'client_id' : clientId,
  'client_secret': clientSecret,
  'grant_type' : 'client_credentials',
  'token_type': 'Bearer',
  'scope': scopesRequired
});
access_token = AccessToken.body.access_token;

Create API client for Loyalty Offer service

API.createClient('beaconOfferService',
'/services/loyaltyoffer/v1/api.raml');

Get Offers based on iBeacon details.

Change beaconId, majorId and minorId values as per your project while testing end point.

Location_obj = beaconOfferService.beaconOffers.post({
     'beaconId':'B9407F30-F5F8-466E-AFF9-25556B57FE6D',
    'majorId':8000,
    'minorId':100
}, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           }
    }
  )


  • Send feedback

    If you find any information that is unclear or incorrect, please let us know so that we can improve the Dev Portal content.

  • Get Help

    Use our private help channel. Receive updates over email and contact our specialists directly.

  • hybris Experts

    If you need more information about this topic, visit hybris Experts to post your own question and interact with our community and experts.