earnPoints( )
Adds points to a loyalty account.
Description
The earnPoints()
function returns a Promise that resolves to the updated loyalty account.
Only a positive amount can be added using the earnPoints()
function, to manually adjust an account's balance for a negative amount, use adjustPoints()
.
The earnPoints()
function allows customers to manually earn points to their loyalty accounts. To use this function you must include an appId
and an idempotencyKey
. Any string can be set as the appId
or idempotencyKey
. In contrast to when an account earns points through an action taken on your site, the appId
automatically sets to the source app that generates the points. The transaction type
is "EARN"
for points earned this way.
Note: Only visitors with Manage Loyalty permissions can earn loyalty points.
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Syntax
function earnPoints(accountId: string, options: EarnPointsOptions): Promise<EarnPointsResponse>
earnPoints Parameters
NAME
TYPE
DESCRIPTION
Loyalty account ID.
Earn points info.
Returns
Return Type:
NAME
TYPE
DESCRIPTION
Updated loyalty account.
Was this helpful?
1import { accounts } from 'wix-loyalty.v2';23/* Sample accountId value: '6d2c421e-3c1a-4464-bf2f-df13a3fc8aea'4 *5 * Sample options value:6 * {7 * 'amount': 50,8 * 'description': 'Subscribed to newsletter.',9 * 'appId': '553c79f3-5625-4f38-b14b-ef7c0d1e87df',10 * 'idempotencyKey': 'bfdc785c-bbc6-447d-b987-220ca649a3b2'11 * }12 */1314export async function myEarnPointsFunction() {15 try {16 const updatedAccount = await accounts.earnPoints(accountId, options);1718 const newBalance = updatedAccount.account.points.balance;19 const transactionId = updatedAccount.latestTransaction._id;2021 console.log('Success! New balance is ', newBalance);22 return updatedAccount;23 } catch (error) {24 console.error(error);25 }26}2728/* Promise resolves to:29 * {30 * "account": {31 * "_id": "6d2c421e-3c1a-4464-bf2f-df13a3fc8aea",32 * "contactId": "7fa15d5e-49c4-44ef-8111-4cb0a04ca4db",33 * "memberId": "7fa15d5e-49c4-44ef-8111-4cb0a04ca4db",34 * "points": {35 * "balance": 185,36 * "earned": 185,37 * "adjusted": 0,38 * "redeemed": 039 * },40 * "latestTransaction": {41 * "_id": "4dd51f9d-2664-420a-9802-4df7811a5221",42 * "amount": 50,43 * "type": "EARN",44 * "description": "Subscribed to newsletter.",45 * "_createdDate": "2022-11-28T14:48:59.887Z",46 * "appId": "553c79f3-5625-4f38-b14b-ef7c0d1e87df",47 * "idempotencyKey": "bfdc785c-bbc6-447d-b987-220ca649a3b2"48 * },49 * "rewardAvailable": true,50 * "_createdDate": "2022-11-09T15:18:53.582Z",51 * "_updatedDate": "2022-11-28T14:48:59.940Z",52 * "revision": "6",53 * "tier": {54 * "_updatedDate": "2022-11-28T14:48:59.939Z",55 * "points": 18556 * }57 * }58 * }59 */60
1import { Permissions, webMethod } from 'wix-web-module';2import { accounts } from 'wix-loyalty.v2';34/* Sample accountId value: '6d2c421e-3c1a-4464-bf2f-df13a3fc8aea'5 *6 * Sample options value:7 * {8 * 'amount': 50,9 * 'description': 'Subscribed to newsletter.',10 * 'appId': '553c79f3-5625-4f38-b14b-ef7c0d1e87df',11 * 'idempotencyKey': 'bfdc785c-bbc6-447d-b987-220ca649a3b2'12 * }13 */1415export const myEarnPointsFunction = webMethod(Permissions.Anyone, async () => {16 try {17 const updatedAccount = await accounts.earnPoints(accountId, options);1819 const newBalance = updatedAccount.account.points.balance;20 const transactionId = updatedAccount.latestTransaction._id;2122 console.log('Success! New balance is ', newBalance);23 return updatedAccount;24 } catch (error) {25 console.error(error);26 }27});2829/* Promise resolves to:30 * {31 * "account": {32 * "_id": "6d2c421e-3c1a-4464-bf2f-df13a3fc8aea",33 * "contactId": "7fa15d5e-49c4-44ef-8111-4cb0a04ca4db",34 * "memberId": "7fa15d5e-49c4-44ef-8111-4cb0a04ca4db",35 * "points": {36 * "balance": 185,37 * "earned": 185,38 * "adjusted": 0,39 * "redeemed": 040 * },41 * "latestTransaction": {42 * "_id": "4dd51f9d-2664-420a-9802-4df7811a5221",43 * "amount": 50,44 * "type": "EARN",45 * "description": "Subscribed to newsletter.",46 * "_createdDate": "2022-11-28T14:48:59.887Z",47 * "appId": "553c79f3-5625-4f38-b14b-ef7c0d1e87df",48 * "idempotencyKey": "bfdc785c-bbc6-447d-b987-220ca649a3b2"49 * },50 * "rewardAvailable": true,51 * "_createdDate": "2022-11-09T15:18:53.582Z",52 * "_updatedDate": "2022-11-28T14:48:59.940Z",53 * "revision": "6",54 * "tier": {55 * "_updatedDate": "2022-11-28T14:48:59.939Z",56 * "points": 18557 * }58 * }59 * }60 */61