Skip to content

Commit

Permalink
Add support for redeeming purchases made towards Apple and Google
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Bylund committed Dec 13, 2023
1 parent 090daad commit 8f7aec5
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Runtime/Client/LootLockerEndPoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ public class LootLockerEndPoints
public static EndPointClass androidPurchaseVerification = new EndPointClass("v1/purchase", LootLockerHTTPMethod.POST);
public static EndPointClass pollingOrderStatus = new EndPointClass("v1/purchase/{0}", LootLockerHTTPMethod.GET);
public static EndPointClass activatingARentalAsset = new EndPointClass("v1/asset/instance/{0}/activate", LootLockerHTTPMethod.POST);

public static EndPointClass purchaseCatalogItem = new EndPointClass("purchase", LootLockerHTTPMethod.POST);
public static EndPointClass redeemAppleAppStorePurchase = new EndPointClass("redeem/store/apple", LootLockerHTTPMethod.POST);
public static EndPointClass redeemGooglePlayStorePurchase = new EndPointClass("redeem/store/google", LootLockerHTTPMethod.POST);

// EventTrigger
[Header("EventTrigger")]
Expand Down
88 changes: 88 additions & 0 deletions Runtime/Game/LootLockerSDKManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4281,6 +4281,94 @@ public static void LootLockerPurchaseCatalogItems(string walletId, LootLockerCat
LootLockerServerRequest.CallAPI(LootLockerEndPoints.purchaseCatalogItem.endPoint, LootLockerEndPoints.purchaseCatalogItem.httpMethod, body, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

/// <summary>
/// Redeem a purchase that was made successfully towards the Apple App Store for the current player
/// </summary>
/// <param name="transactionId">The id of the transaction successfully made towards the Apple App Store</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void LootLockerRedeemAppleAppStorePurchaseForPlayer(string transactionId, Action<LootLockerResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerResponse>());
return;
}
var body = LootLockerJson.SerializeObject(new LootLockerRedeemAppleAppStorePurchaseForPlayerRequest()
{
transaction_id = transactionId
});

LootLockerServerRequest.CallAPI(LootLockerEndPoints.redeemAppleAppStorePurchase.endPoint, LootLockerEndPoints.redeemAppleAppStorePurchase.httpMethod, body, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

/// <summary>
/// Redeem a purchase that was made successfully towards the Apple App Store for a character that the current player owns
/// </summary>
/// <param name="transactionId">The id of the transaction successfully made towards the Apple App Store</param>
/// <param name="characterId">The id of the character to redeem this transaction for</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void LootLockerRedeemAppleAppStorePurchaseForPlayer(string transactionId, int characterId, Action<LootLockerResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerResponse>());
return;
}
var body = LootLockerJson.SerializeObject(new LootLockerRedeemAppleAppStorePurchaseForCharacterRequest()
{
transaction_id = transactionId,
character_id = characterId
});

LootLockerServerRequest.CallAPI(LootLockerEndPoints.redeemAppleAppStorePurchase.endPoint, LootLockerEndPoints.redeemAppleAppStorePurchase.httpMethod, body, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

/// <summary>
/// Redeem a purchase that was made successfully towards the Apple App Store for the current player
/// </summary>
/// <param name="productId">The id of the product that this redemption refers to</param>
/// <param name="purchaseToken">The token from the purchase successfully made towards the Google Play Store</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void LootLockerRedeemGooglePlayStorePurchaseForPlayer(string productId, string purchaseToken, Action<LootLockerResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerResponse>());
return;
}
var body = LootLockerJson.SerializeObject(new LootLockerRedeemGooglePlayStorePurchaseForPlayerRequest()
{
product_id = productId,
purchase_token = purchaseToken
});

LootLockerServerRequest.CallAPI(LootLockerEndPoints.redeemGooglePlayStorePurchase.endPoint, LootLockerEndPoints.redeemGooglePlayStorePurchase.httpMethod, body, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

/// <summary>
/// Redeem a purchase that was made successfully towards the Apple App Store for a character that the current player owns
/// </summary>
/// <param name="productId">The id of the product that this redemption refers to</param>
/// <param name="purchaseToken">The token from the purchase successfully made towards the Google Play Store</param>
/// <param name="characterId">The id of the character to redeem this purchase for</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void LootLockerRedeemGooglePlayStorePurchaseForPlayer(string productId, string purchaseToken, int characterId, Action<LootLockerResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerResponse>());
return;
}
var body = LootLockerJson.SerializeObject(new LootLockerRedeemGooglePlayStorePurchaseForCharacterRequest()
{
product_id = productId,
purchase_token = purchaseToken,
character_id = characterId
});

LootLockerServerRequest.CallAPI(LootLockerEndPoints.redeemGooglePlayStorePurchase.endPoint, LootLockerEndPoints.redeemGooglePlayStorePurchase.httpMethod, body, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

#endregion

#region Collectables
Expand Down
61 changes: 61 additions & 0 deletions Runtime/Game/Requests/PurchaseRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace LootLocker.Requests
{
#region Legacy Purchasing
//TODO: Deprecate legacy purchasing

public class LootLockerPurchaseRequests
{
}
Expand Down Expand Up @@ -47,6 +50,7 @@ public class LootLockerPurchaseOrderStatus : LootLockerResponse
{
public string status { get; set; }
}
#endregion

/// <summary>
///
Expand Down Expand Up @@ -77,12 +81,68 @@ public class LootLockerPurchaseCatalogItemRequest
/// </summary>
public LootLockerCatalogItemAndQuantityPair[] items { get; set; }
}

/// <summary>
///
/// </summary>
public class LootLockerRedeemAppleAppStorePurchaseForPlayerRequest
{
#if LOOTLOCKER_SANDBOX_APPLE_APP_STORE
/// <summary>
/// Whether or not to use the app store sandbox for this redemption
/// </summary>
public string sandboxed { get; set; } = true;
#endif
/// <summary>
/// The id of the transaction successfully made towards the Apple App Store
/// </summary>
public string transaction_id { get; set; }
}

/// <summary>
///
/// </summary>
public class LootLockerRedeemAppleAppStorePurchaseForCharacterRequest : LootLockerRedeemAppleAppStorePurchaseForPlayerRequest
{
/// <summary>
/// The id of the character to redeem this transaction for
/// </summary>
public int character_id { get; set; }
}

/// <summary>
///
/// </summary>
public class LootLockerRedeemGooglePlayStorePurchaseForPlayerRequest
{
/// <summary>
/// The id of the product that this redemption refers to
/// </summary>
public string product_id { get; set; }
/// <summary>
/// The token from the purchase successfully made towards the Google Play Store
/// </summary>
public string purchase_token { get; set; }
}

/// <summary>
///
/// </summary>
public class LootLockerRedeemGooglePlayStorePurchaseForCharacterRequest : LootLockerRedeemGooglePlayStorePurchaseForPlayerRequest
{
/// <summary>
/// The id of the character to redeem this purchase for
/// </summary>
public int character_id { get; set; }
}
}

namespace LootLocker
{
public partial class LootLockerAPIManager
{
#region Legacy Purchasing
// TODO: Deprecate legacy purchasing
public static void NormalPurchaseCall(LootLockerNormalPurchaseRequest[] data, Action<LootLockerPurchaseResponse> onComplete)
{
if(data == null)
Expand Down Expand Up @@ -160,5 +220,6 @@ public static void ActivateRentalAsset(LootLockerGetRequest lootLockerGetRequest

LootLockerServerRequest.CallAPI(getVariable, endPoint.httpMethod, "", (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}
#endregion
}
}

0 comments on commit 8f7aec5

Please sign in to comment.