বিলিং API আপনাকে ডিজিটাল পণ্য এবং সাবস্ক্রিপশন বিক্রি করতে দেয়। C# র্যাপারটি অন্তর্নিহিত Google Play বিলিং লাইব্রেরির জন্য একটি টাইপ-সেফ, অ্যাসিঙ্ক্রোনাস ইন্টারফেস প্রদান করে।
নেমস্পেস: PlayPcSdkManaged.Billing
ক্লায়েন্ট ক্লাস: BillingClient
ক্লায়েন্ট তৈরি করুন
একটি BillingClient তৈরি করতে সর্বদা কারখানা ব্যবহার করুন। এটি নিশ্চিত করে যে ইউনিটি-নিরাপদ কলব্যাকগুলি স্বয়ংক্রিয়ভাবে নিবন্ধিত হয়েছে।
using UnityEngine; using System; using System.Threading.Tasks; using System.Collections.Generic; // Required SDK Namespaces using PlayPcSdkManaged.Billing; using PlayPcSdkManaged.Unity; public class BillingManager : MonoBehaviour { private BillingClient _billingClient; public void SetupBilling() { try { // Creates the client with the required UnityBillingCallbacksHandler _billingClient = PlayPcSdkFactory.CreateBillingClient(); Debug.Log("Billing Client created successfully."); } catch (Exception ex) { Debug.LogError($"Failed to create Billing Client: {ex.Message}"); } } private void OnDestroy() { // Always dispose of the client to clean up native C++ resources _billingClient?.Dispose(); } }
পণ্যের বিবরণ জিজ্ঞাসা করুন
বিক্রয়ের জন্য আইটেমগুলি প্রদর্শন করার আগে, আপনাকে Google Play থেকে মূল্য, শিরোনাম এবং বিবরণের মতো বিশদ জিজ্ঞাসা করতে হবে।
public async Task GetProductDetailsAsync() { try { // Define the list of products you want to query var productList = new List{ // Use ProductType.InApp for consumables/non-consumables new ProductId { Id = "gem_pack_100", ProductType = ProductType.InApp }, // Use ProductType.Subs for subscriptions new ProductId { Id = "gold_subscription", ProductType = ProductType.InApp } }; var queryParams = new QueryProductDetailsParams { ProductIds = productList }; // Async call var result = await _billingClient.QueryProductDetailsAsync(queryParams); if (result.IsOk) { foreach (var product in result.Value.ProductDetailsList) { // The formatted price (e.g., "$0.99") is inside the ProductOffers list if (product.ProductOffers != null && product.ProductOffers.Count > 0) { var price = product.ProductOffers[0].FormattedPrice; var offerToken = product.ProductOffers[0].OfferToken; Debug.Log($"Product: {product.Title} | Price: {price} | Token: {offerToken}"); } } } else { Debug.LogError($"Query Failed: {result.Code} - {result.ErrorMessage}"); } } catch (Exception ex) { Debug.LogException(ex); } }
ক্রয় প্রবাহ চালু করুন
কেনাকাটা শুরু করতে, পণ্যের বিবরণ ধাপ থেকে OfferToken ব্যবহার করে LaunchPurchaseFlowAsync কল করুন।
public async Task BuyItemAsync(string offerToken) { try { var purchaseParams = new LaunchPurchaseFlowParams { OfferToken = offerToken, Quantity = 1, // Optional: Attach obfuscated IDs for fraud detection ObfuscatedAccountId = "user_12345_hash", ObfuscatedProfileId = "profile_abcde_hash" }; var result = await _billingClient.LaunchPurchaseFlowAsync(purchaseParams); if (result.IsOk) { var purchase = result.Value.ProductPurchaseDetails; Debug.Log($"Purchase Successful! Order ID: {purchase.OrderId}"); // IMPORTANT: You must now Acknowledge or Consume this purchase. // If you don't, Google Play will refund the user after a few days. if (!purchase.IsAcknowledged) { // Decide based on your game logic if it's consumable or permanent await HandlePurchaseAsync(purchase); } } else if (result.Code == BillingError.UserCanceled) { Debug.Log("User canceled the purchase flow."); } else { Debug.LogError($"Purchase Failed: {result.Code} - {result.ErrorMessage}"); } } catch (Exception ex) { Debug.LogException(ex); } }
বিদ্যমান কেনাকাটাগুলি জিজ্ঞাসা করুন (পুনরুদ্ধার করুন)
গেম স্টার্টআপে QueryPurchasesAsync এ কল করে আপনার ইতিমধ্যেই থাকা আইটেমগুলি পুনরুদ্ধার করুন, উদাহরণস্বরূপ, গেম পুনরায় ইনস্টল করার পরে, অথবা মুলতুবি লেনদেন পরীক্ষা করুন।
এই উদাহরণটি দেখায় যে কীভাবে ক্রয়কে স্থায়ী আইটেমের জন্য স্বীকৃতি বা ভোগ্যপণ্যের জন্য কনজিউমে রুট করতে হয়।
public async Task CheckExistingPurchasesAsync() { try { // Fetches all purchases owned by the user var result = await _billingClient.QueryPurchasesAsync(); if (result.IsOk) { foreach (var purchase in result.Value.ProductPurchaseDetails) { Debug.Log($"User owns: {purchase.ProductId} | State: {purchase.PurchaseState}"); // Process any purchase that hasn't been acknowledged yet if (purchase.PurchaseState == PurchaseState.Purchased && !purchase.IsAcknowledged) { await HandlePurchaseAsync(purchase); } } } else { Debug.LogError($"Restore Failed: {result.Code} - {result.ErrorMessage}"); } } catch (Exception ex) { Debug.LogException(ex); } } // Helper method to route purchases private async Task HandlePurchaseAsync(ProductPurchaseDetails purchase) { // Example logic: "gem_pack" is consumable, everything else is permanent if (purchase.ProductId.Contains("gem_pack")) { await ConsumeItemAsync(purchase.PurchaseToken); } else { await AcknowledgeItemAsync(purchase.PurchaseToken); } }
ক্রয় স্বীকার করুন
অব্যবহার্য জিনিসপত্র (যেমন, "প্রিমিয়াম আপগ্রেড" বা "লেভেল প্যাক") স্বীকার করতে হবে। এটি Google Play-কে নির্দেশ করে যে আপনি ব্যবহারকারীকে আইটেমটি দিয়েছেন।
public async Task AcknowledgeItemAsync(string purchaseToken) { try { var acknowledgeParams = new AcknowledgePurchaseParams { PurchaseToken = purchaseToken }; var result = await _billingClient.AcknowledgePurchaseAsync(acknowledgeParams); if (result.IsOk) { Debug.Log("Purchase Acknowledged. Usage rights granted permanently."); } else { Debug.LogError($"Acknowledge Failed: {result.Code} - {result.ErrorMessage}"); } } catch (Exception ex) { Debug.LogException(ex); } }
একটি ক্রয় গ্রহণ করুন
"১০০ রত্ন" বা "স্বাস্থ্যকর ঔষধ" এর মতো ভোগ্যপণ্য পুনঃক্রয়ের জন্য অবশ্যই সেবন করতে হবে।
public async Task ConsumeItemAsync(string purchaseToken) { try { var consumeParams = new ConsumePurchaseParams { PurchaseToken = purchaseToken }; var result = await _billingClient.ConsumePurchaseAsync(consumeParams); if (result.IsOk) { Debug.Log("Item Consumed. User can buy it again."); // Add the gems/coins to the user's inventory here } else { Debug.LogError($"Consume Failed: {result.Code} - {result.ErrorMessage}"); } } catch (Exception ex) { Debug.LogException(ex); } }