Skip to content

Latest commit

 

History

History
863 lines (614 loc) · 28.2 KB

loyalty.md

File metadata and controls

863 lines (614 loc) · 28.2 KB

Loyalty

loyalty_api = client.loyalty

Class Name

LoyaltyApi

Methods

Create Loyalty Account

Creates a loyalty account. To create a loyalty account, you must provide the program_id and a mapping with the phone_number of the buyer.

def create_loyalty_account(self,
                          body)

Parameters

Parameter Type Tags Description
body Create Loyalty Account Request Body, Required An object containing the fields to POST for the request.

See the corresponding object definition for field details.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Create Loyalty Account Response.

Example Usage

body = {
    'loyalty_account': {
        'program_id': 'd619f755-2d17-41f3-990d-c04ecedd64dd',
        'mapping': {
            'phone_number': '+14155551234'
        }
    },
    'idempotency_key': 'ec78c477-b1c3-4899-a209-a4e71337c996'
}

result = loyalty_api.create_loyalty_account(body)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Search Loyalty Accounts

Searches for loyalty accounts in a loyalty program.

You can search for a loyalty account using the phone number or customer ID associated with the account. To return all loyalty accounts, specify an empty query object or omit it entirely.

Search results are sorted by created_at in ascending order.

def search_loyalty_accounts(self,
                           body)

Parameters

Parameter Type Tags Description
body Search Loyalty Accounts Request Body, Required An object containing the fields to POST for the request.

See the corresponding object definition for field details.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Search Loyalty Accounts Response.

Example Usage

body = {
    'query': {
        'mappings': [
            {
                'phone_number': '+14155551234'
            }
        ]
    },
    'limit': 10
}

result = loyalty_api.search_loyalty_accounts(body)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Retrieve Loyalty Account

Retrieves a loyalty account.

def retrieve_loyalty_account(self,
                            account_id)

Parameters

Parameter Type Tags Description
account_id str Template, Required The ID of the loyalty account to retrieve.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Retrieve Loyalty Account Response.

Example Usage

account_id = 'account_id2'

result = loyalty_api.retrieve_loyalty_account(account_id)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Accumulate Loyalty Points

Adds points earned from a purchase to a loyalty account.

  • If you are using the Orders API to manage orders, provide the order_id. Square reads the order to compute the points earned from both the base loyalty program and an associated loyalty promotion. For purchases that qualify for multiple accrual rules, Square computes points based on the accrual rule that grants the most points. For purchases that qualify for multiple promotions, Square computes points based on the most recently created promotion. A purchase must first qualify for program points to be eligible for promotion points.

  • If you are not using the Orders API to manage orders, provide points with the number of points to add. You must first perform a client-side computation of the points earned from the loyalty program and loyalty promotion. For spend-based and visit-based programs, you can call CalculateLoyaltyPoints to compute the points earned from the base loyalty program. For information about computing points earned from a loyalty promotion, see Calculating promotion points.

def accumulate_loyalty_points(self,
                             account_id,
                             body)

Parameters

Parameter Type Tags Description
account_id str Template, Required The ID of the target loyalty account.
body Accumulate Loyalty Points Request Body, Required An object containing the fields to POST for the request.

See the corresponding object definition for field details.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Accumulate Loyalty Points Response.

Example Usage

account_id = 'account_id2'

body = {
    'accumulate_points': {
        'order_id': 'RFZfrdtm3mhO1oGzf5Cx7fEMsmGZY'
    },
    'idempotency_key': '58b90739-c3e8-4b11-85f7-e636d48d72cb',
    'location_id': 'P034NEENMD09F'
}

result = loyalty_api.accumulate_loyalty_points(
    account_id,
    body
)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Adjust Loyalty Points

Adds points to or subtracts points from a buyer's account.

Use this endpoint only when you need to manually adjust points. Otherwise, in your application flow, you call AccumulateLoyaltyPoints to add points when a buyer pays for the purchase.

def adjust_loyalty_points(self,
                         account_id,
                         body)

Parameters

Parameter Type Tags Description
account_id str Template, Required The ID of the target loyalty account.
body Adjust Loyalty Points Request Body, Required An object containing the fields to POST for the request.

See the corresponding object definition for field details.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Adjust Loyalty Points Response.

Example Usage

account_id = 'account_id2'

body = {
    'idempotency_key': 'bc29a517-3dc9-450e-aa76-fae39ee849d1',
    'adjust_points': {
        'points': 10,
        'reason': 'Complimentary points'
    }
}

result = loyalty_api.adjust_loyalty_points(
    account_id,
    body
)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Search Loyalty Events

Searches for loyalty events.

A Square loyalty program maintains a ledger of events that occur during the lifetime of a buyer's loyalty account. Each change in the point balance (for example, points earned, points redeemed, and points expired) is recorded in the ledger. Using this endpoint, you can search the ledger for events.

Search results are sorted by created_at in descending order.

def search_loyalty_events(self,
                         body)

Parameters

Parameter Type Tags Description
body Search Loyalty Events Request Body, Required An object containing the fields to POST for the request.

See the corresponding object definition for field details.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Search Loyalty Events Response.

Example Usage

body = {
    'query': {
        'filter': {
            'order_filter': {
                'order_id': 'PyATxhYLfsMqpVkcKJITPydgEYfZY'
            }
        }
    },
    'limit': 30
}

result = loyalty_api.search_loyalty_events(body)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

List Loyalty Programs

This endpoint is deprecated.

Returns a list of loyalty programs in the seller's account. Loyalty programs define how buyers can earn points and redeem points for rewards. Square sellers can have only one loyalty program, which is created and managed from the Seller Dashboard. For more information, see Loyalty Program Overview.

Replaced with RetrieveLoyaltyProgram when used with the keyword main.

def list_loyalty_programs(self)

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type List Loyalty Programs Response.

Example Usage

result = loyalty_api.list_loyalty_programs()
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Retrieve Loyalty Program

Retrieves the loyalty program in a seller's account, specified by the program ID or the keyword main.

Loyalty programs define how buyers can earn points and redeem points for rewards. Square sellers can have only one loyalty program, which is created and managed from the Seller Dashboard. For more information, see Loyalty Program Overview.

def retrieve_loyalty_program(self,
                            program_id)

Parameters

Parameter Type Tags Description
program_id str Template, Required The ID of the loyalty program or the keyword main. Either value can be used to retrieve the single loyalty program that belongs to the seller.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Retrieve Loyalty Program Response.

Example Usage

program_id = 'program_id0'

result = loyalty_api.retrieve_loyalty_program(program_id)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Calculate Loyalty Points

Calculates the number of points a buyer can earn from a purchase. Applications might call this endpoint to display the points to the buyer.

  • If you are using the Orders API to manage orders, provide the order_id and (optional) loyalty_account_id. Square reads the order to compute the points earned from the base loyalty program and an associated loyalty promotion.

  • If you are not using the Orders API to manage orders, provide transaction_amount_money with the purchase amount. Square uses this amount to calculate the points earned from the base loyalty program, but not points earned from a loyalty promotion. For spend-based and visit-based programs, the tax_mode setting of the accrual rule indicates how taxes should be treated for loyalty points accrual. If the purchase qualifies for program points, call ListLoyaltyPromotions and perform a client-side computation to calculate whether the purchase also qualifies for promotion points. For more information, see Calculating promotion points.

def calculate_loyalty_points(self,
                            program_id,
                            body)

Parameters

Parameter Type Tags Description
program_id str Template, Required The ID of the loyalty program, which defines the rules for accruing points.
body Calculate Loyalty Points Request Body, Required An object containing the fields to POST for the request.

See the corresponding object definition for field details.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Calculate Loyalty Points Response.

Example Usage

program_id = 'program_id0'

body = {
    'order_id': 'RFZfrdtm3mhO1oGzf5Cx7fEMsmGZY',
    'loyalty_account_id': '79b807d2-d786-46a9-933b-918028d7a8c5'
}

result = loyalty_api.calculate_loyalty_points(
    program_id,
    body
)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

List Loyalty Promotions

Lists the loyalty promotions associated with a loyalty program. Results are sorted by the created_at date in descending order (newest to oldest).

def list_loyalty_promotions(self,
                           program_id,
                           status=None,
                           cursor=None,
                           limit=None)

Parameters

Parameter Type Tags Description
program_id str Template, Required The ID of the base loyalty program. To get the program ID,
call RetrieveLoyaltyProgram using the main keyword.
status str (Loyalty Promotion Status) Query, Optional The status to filter the results by. If a status is provided, only loyalty promotions
with the specified status are returned. Otherwise, all loyalty promotions associated with
the loyalty program are returned.
cursor str Query, Optional The cursor returned in the paged response from the previous call to this endpoint.
Provide this cursor to retrieve the next page of results for your original request.
For more information, see Pagination.
limit int Query, Optional The maximum number of results to return in a single paged response.
The minimum value is 1 and the maximum value is 30. The default value is 30.
For more information, see Pagination.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type List Loyalty Promotions Response.

Example Usage

program_id = 'program_id0'

result = loyalty_api.list_loyalty_promotions(program_id)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Create Loyalty Promotion

Creates a loyalty promotion for a loyalty program. A loyalty promotion enables buyers to earn points in addition to those earned from the base loyalty program.

This endpoint sets the loyalty promotion to the ACTIVE or SCHEDULED status, depending on the available_time setting. A loyalty program can have a maximum of 10 loyalty promotions with an ACTIVE or SCHEDULED status.

def create_loyalty_promotion(self,
                            program_id,
                            body)

Parameters

Parameter Type Tags Description
program_id str Template, Required The ID of the loyalty program to associate with the promotion.
To get the program ID, call RetrieveLoyaltyProgram
using the main keyword.
body Create Loyalty Promotion Request Body, Required An object containing the fields to POST for the request.

See the corresponding object definition for field details.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Create Loyalty Promotion Response.

Example Usage

program_id = 'program_id0'

body = {
    'loyalty_promotion': {
        'name': 'Tuesday Happy Hour Promo',
        'incentive': {
            'type': 'POINTS_MULTIPLIER',
            'points_multiplier_data': {
                'multiplier': '3.0'
            }
        },
        'available_time': {
            'time_periods': [
                'BEGIN:VEVENT\nDTSTART:20220816T160000\nDURATION:PT2H\nRRULE:FREQ=WEEKLY;BYDAY=TU\nEND:VEVENT'
            ]
        },
        'trigger_limit': {
            'times': 1,
            'interval': 'DAY'
        },
        'minimum_spend_amount_money': {
            'amount': 2000,
            'currency': 'USD'
        },
        'qualifying_category_ids': [
            'XTQPYLR3IIU9C44VRCB3XD12'
        ]
    },
    'idempotency_key': 'ec78c477-b1c3-4899-a209-a4e71337c996'
}

result = loyalty_api.create_loyalty_promotion(
    program_id,
    body
)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Retrieve Loyalty Promotion

Retrieves a loyalty promotion.

def retrieve_loyalty_promotion(self,
                              promotion_id,
                              program_id)

Parameters

Parameter Type Tags Description
promotion_id str Template, Required The ID of the loyalty promotion to retrieve.
program_id str Template, Required The ID of the base loyalty program. To get the program ID,
call RetrieveLoyaltyProgram using the main keyword.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Retrieve Loyalty Promotion Response.

Example Usage

promotion_id = 'promotion_id0'

program_id = 'program_id0'

result = loyalty_api.retrieve_loyalty_promotion(
    promotion_id,
    program_id
)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Cancel Loyalty Promotion

Cancels a loyalty promotion. Use this endpoint to cancel an ACTIVE promotion earlier than the end date, cancel an ACTIVE promotion when an end date is not specified, or cancel a SCHEDULED promotion. Because updating a promotion is not supported, you can also use this endpoint to cancel a promotion before you create a new one.

This endpoint sets the loyalty promotion to the CANCELED state

def cancel_loyalty_promotion(self,
                            promotion_id,
                            program_id)

Parameters

Parameter Type Tags Description
promotion_id str Template, Required The ID of the loyalty promotion to cancel. You can cancel a
promotion that has an ACTIVE or SCHEDULED status.
program_id str Template, Required The ID of the base loyalty program.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Cancel Loyalty Promotion Response.

Example Usage

promotion_id = 'promotion_id0'

program_id = 'program_id0'

result = loyalty_api.cancel_loyalty_promotion(
    promotion_id,
    program_id
)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Create Loyalty Reward

Creates a loyalty reward. In the process, the endpoint does following:

  • Uses the reward_tier_id in the request to determine the number of points to lock for this reward.
  • If the request includes order_id, it adds the reward and related discount to the order.

After a reward is created, the points are locked and not available for the buyer to redeem another reward.

def create_loyalty_reward(self,
                         body)

Parameters

Parameter Type Tags Description
body Create Loyalty Reward Request Body, Required An object containing the fields to POST for the request.

See the corresponding object definition for field details.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Create Loyalty Reward Response.

Example Usage

body = {
    'reward': {
        'loyalty_account_id': '5adcb100-07f1-4ee7-b8c6-6bb9ebc474bd',
        'reward_tier_id': 'e1b39225-9da5-43d1-a5db-782cdd8ad94f',
        'order_id': 'RFZfrdtm3mhO1oGzf5Cx7fEMsmGZY'
    },
    'idempotency_key': '18c2e5ea-a620-4b1f-ad60-7b167285e451'
}

result = loyalty_api.create_loyalty_reward(body)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Search Loyalty Rewards

Searches for loyalty rewards. This endpoint accepts a request with no query filters and returns results for all loyalty accounts. If you include a query object, loyalty_account_id is required and status is optional.

If you know a reward ID, use the RetrieveLoyaltyReward endpoint.

Search results are sorted by updated_at in descending order.

def search_loyalty_rewards(self,
                          body)

Parameters

Parameter Type Tags Description
body Search Loyalty Rewards Request Body, Required An object containing the fields to POST for the request.

See the corresponding object definition for field details.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Search Loyalty Rewards Response.

Example Usage

body = {
    'query': {
        'loyalty_account_id': '5adcb100-07f1-4ee7-b8c6-6bb9ebc474bd'
    },
    'limit': 10
}

result = loyalty_api.search_loyalty_rewards(body)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Delete Loyalty Reward

Deletes a loyalty reward by doing the following:

  • Returns the loyalty points back to the loyalty account.
  • If an order ID was specified when the reward was created (see CreateLoyaltyReward), it updates the order by removing the reward and related discounts.

You cannot delete a reward that has reached the terminal state (REDEEMED).

def delete_loyalty_reward(self,
                         reward_id)

Parameters

Parameter Type Tags Description
reward_id str Template, Required The ID of the loyalty reward to delete.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Delete Loyalty Reward Response.

Example Usage

reward_id = 'reward_id4'

result = loyalty_api.delete_loyalty_reward(reward_id)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Retrieve Loyalty Reward

Retrieves a loyalty reward.

def retrieve_loyalty_reward(self,
                           reward_id)

Parameters

Parameter Type Tags Description
reward_id str Template, Required The ID of the loyalty reward to retrieve.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Retrieve Loyalty Reward Response.

Example Usage

reward_id = 'reward_id4'

result = loyalty_api.retrieve_loyalty_reward(reward_id)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

Redeem Loyalty Reward

Redeems a loyalty reward.

The endpoint sets the reward to the REDEEMED terminal state.

If you are using your own order processing system (not using the Orders API), you call this endpoint after the buyer paid for the purchase.

After the reward reaches the terminal state, it cannot be deleted. In other words, points used for the reward cannot be returned to the account.

def redeem_loyalty_reward(self,
                         reward_id,
                         body)

Parameters

Parameter Type Tags Description
reward_id str Template, Required The ID of the loyalty reward to redeem.
body Redeem Loyalty Reward Request Body, Required An object containing the fields to POST for the request.

See the corresponding object definition for field details.

Response Type

This method returns a ApiResponse instance. The body property of this instance returns the response data which is of type Redeem Loyalty Reward Response.

Example Usage

reward_id = 'reward_id4'

body = {
    'idempotency_key': '98adc7f7-6963-473b-b29c-f3c9cdd7d994',
    'location_id': 'P034NEENMD09F'
}

result = loyalty_api.redeem_loyalty_reward(
    reward_id,
    body
)
print(result)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)