Implement Fulfillment on a Game Server
Overview
This article walks you through how to implement code redemption and how to enable your game server to grant items to your players.
Code redemption
A player can redeem a campaign code to receive entitlements, such as games, in-game items, or coins. To redeem a code, call RedeemCode from the Fulfillment API. The code will be redeemed and the player will receive their entitlements under the following conditions:
- The code exists in the game namespace
- The maximum redemption limit hasn't been reached
- The time and date of the redemption falls within the redemption period
- Unreal Engine
- Unity
- Go Extend SDK
- Python Extend SDK
- Java Extend SDK
- C# Extend SDK
FString Code = FString("MyRedeemCode");
FString Region = FString("US"); // Leave it blank if you want to use the default value from user token
FString Language = FString("en"); // Leave it blank if you want to use the default value from user token
FRegistry::Fulfillment.RedeemCode(Code, Region, Language, THandler<FAccelByteModelsFulfillmentResult>::CreateLambda([](const FAccelByteModelsFulfillmentResult& Result)
{
// Do something if RedeemCode succeeds
}),
FErrorHandler::CreateLambda([](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something if RedeemCode fails
UE_LOG(LogTemp, Log, TEXT("Error RedeemCode, Error Code: %d Error Message: %s"), ErrorCode, *ErrorMessage);
}));
string code= "some-code";
string region = "US";
string language = "en";
AccelByteSDK.GetClientRegistry().GetApi().GetFulfillment().RedeemCode(code, region, language, result =>
{
if (result.IsError)
{
// Do something if RedeemCode fails
Debug.Log($"Error RedeemCode, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
// Do something if RedeemCode succeeds
}
});
fulfillmentService := &platform.FulfillmentService{
Client: factory.NewPlatformClient(&repository.ConfigRepositoryImpl{}),
TokenRepository: &repository.TokenRepositoryImpl{},
}
namespace := "mygame"
userId := "myuserid"
code := "somecode"
body := platformclientmodels.FulfillCodeRequest {
Code: &code,
Region: "US",
Language: "en",
}
input := &fulfillment.PublicRedeemCodeParams{
Body: &body,
Namespace: namespace,
UserID: userId,
}
result, err := fulfillmentService.PublicRedeemCodeShort(input)
import accelbyte_py_sdk.api.platform as platform_service
import accelbyte_py_sdk.api.platform.models as platform_models
result, error = platform_service.public_redeem_code(
body=platform_models.FulfillCodeRequest()
.with_code("MyRedeemCode")
.with_language("en")
.with_region("US"),
user_id="********************************",
if error:
exit(error)
Fulfillment fulfillmentWrapper = new Fulfillment(sdk);
String userId = "<user-id>";
FulfillmentResult response;
try {
FulfillCodeRequest reqBody = FulfillCodeRequest.builder()
.code("<some-code>")
.region("US")
.language("en")
.build();
response = fulfillmentWrapper.publicRedeemCode(PublicRedeemCode.builder()
.namespace("<namespace>")
.userId(userId)
.body(reqBody)
.build());
} catch (Exception e) {
// Do something when failed
return;
}
if (response == null) {
// Null response from server
} else {
// Do something when successful
}
string userId = "<user-id>";
var response = sdk.Platform.Fulfillment.PublicRedeemCodeOp
.SetBody(new FulfillCodeRequest()
{
Code = "<some-code>",
Region = "US",
Language = "en"
})
.Execute(sdk.Namespace, userId);
if (response != null)
{
//do something if success
}
Game server grants items to players
The FulfillUserItem() function can be used to allow the game server to trigger the Fulfillment service and grant an item to a player. This can be used to immediately grant a player an item when they earn a particular achievement, or to grant every player in a match a special item for a particular game mode but have that item disappear from the player's inventory when the match is over.
- Unreal Engine
- Unity
- Go Extend SDK
- Python Extend SDK
- Java Extend SDK
- C# Extend SDK
#include "GameServerApi/AccelByteServerEcommerceApi.h"
...
FString UserId = FString("SomeUserId");
FAccelByteModelsFulfillmentRequest FulfillmentRequest;
FulfillmentRequest.ItemId = FString("SomeItemId");
FulfillmentRequest.OrderNo = FString("SomeOrderNo");
FulfillmentRequest.Language = FString("en");
FulfillmentRequest.Region = FString("US");
FulfillmentRequest.Quantity = 1;
FulfillmentRequest.Source = EAccelByteItemSource::ACHIEVEMENT;
FRegistry::ServerEcommerce.FulfillUserItem(UserId, FulfillmentRequest, THandler<FAccelByteModelsFulfillmentResult>::CreateLambda([](const FAccelByteModelsFulfillmentResult& Result)
{
// Do something if FulfillUserItem succeeds
}),
FErrorHandler::CreateLambda([](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something if FulfillUserItem fails
UE_LOG(LogTemp, Log, TEXT("Error FulfillUserItem, Error Code: %d Error Message: %s"), ErrorCode, *ErrorMessage);
}));
using AccelByte.Server;
...
string userId = "SomeUserId";
FulfillmentRequest fulfillmentRequest = new FulfillmentRequest
{
itemId = "SomeItemId",
orderNo = "SomeOrderNo",
language = "en",
region = "US",
quantity = 1,
source = ItemSource.ACHIEVEMENT
};
AccelByteSDK.GetServerRegistry().GetApi().GetEcommerce().FulfillUserItem(userId, fulfillmentRequest, result =>
{
if (result.IsError)
{
// Do something if FulfillUserItem fails
Debug.Log($"Error FulfillUserItem, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
// Do something if FulfillUserItem succeeds
}
});
fulfillmentService := &platform.FulfillmentService{
Client: factory.NewPlatformClient(&repository.ConfigRepositoryImpl{}),
TokenRepository: &repository.TokenRepositoryImpl{},
}
namespace := "mygame"
userId := "myuserid"
quantity := int32(1)
body := platformclientmodels.FulfillmentRequest {
ItemID: "SomeItemId",
OrderNo: "SomeOrderNo",
Language: "en",
Region: "US",
Quantity: &quantity,
Source: platformclientmodels.FulfillmentRequestSourceACHIEVEMENT,
}
input := &fulfillment.FulfillItemParams{
Body: &body,
Namespace: namespace,
UserID: userId,
}
result, err := fulfillmentService.FulfillItemShort(input)
import accelbyte_py_sdk.api.platform as platform_service
import accelbyte_py_sdk.api.platform.models as platform_models
result, error = platform_service.fulfill_item(
body=platform_models.FulfillmentRequest()
.with_quantity(1)
.with_item_id("SomeItemId")
.with_order_no("SomeOrderNo")
.with_language("en")
.with_region("US"),
.with_source("ACHIEVEMENT"),
user_id="********************************",
if error:
exit(error)
string userId = "<user-id>";
var response = sdk.Platform.Fulfillment.FulfillItemOp
.SetBody(new FulfillmentRequest()
{
ItemId = "SomeItemId",
OrderNo = "SomeOrderNo",
Language = "en",
Region = "US",
Quantity = 1,
Source = FulfillmentRequestSource.ACHIEVEMENT
})
.Execute(sdk.Namespace, userId);
if (response != null)
{
//do something if success
}