With the now.gg Payments Module for Cocos, you can implement in-app purchases within your game on the Cocos platform.
To integrate now.gg Payments:
Note: Your project’s minimum Android SDK version must not be lower than version 19.
The now.gg Payments Module for Cocos is included within the download package (nowgg_sdk_cocos-1.2.6.2001.zip
).
To add now.gg Payments module within your cocos project:
Once the now.gg Payments module has been imported, you should now add the required dependencies to your project.
if(ANDROID)
section of your CMakeLists.txt file:
list(APPEND GAME_HEADER proj.android/app/jni/nowggsdk/include/nowgg_sdk.h )
if(ANDROID)
section of your CMakeLists.txt file. This will add nowggsdk to your project:
add_library(nowggsdk STATIC IMPORTED) set_target_properties(nowggsdk PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/proj.android/app/jni/nowggsdk/${ANDROID_ABI}/libnowggsdk.a) target_link_libraries(${APP_NAME} nowggsdk)
gradle.properties
file located in proj.android directory:
android.useAndroidX=true
HelloWorldScene.h
class by navigating to cocosSDKsample > classes within the download package.
#include "../proj.android/app/jni/nowggsdk/include/nowgg_sdk.h"
class HelloWorld : public cocos2d::Scene, public nowgg::NowGGSDKListener
HelloWorldScene
class:
private: virtual void onInitialized(bool success) override; virtual void onPurchaseUpdated(const vector<nowgg::Product*> p) override; virtual void onPurchaseCancelled(int code, string msg) override; virtual void onPurchaseConsumed(string token) override;
Note: Add the definitions for the virtual functions mentioned above, in HelloWorldScene.cpp
.
build.gradle
file located in proj.android > app directory.
dependencies { implementation (files("libs/billingclient.aar")) implementation (files("libs/nowggsdk.aar")) implementation 'androidx.appcompat:appcompat:1.4.1' api 'com.google.code.gson:gson:2.8.6' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0' implementation 'com.squareup.retrofit2:converter-scalars:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' }
Once the module has been imported and the dependencies have been added, you should now implement now.gg Payments with your app/game.
To check if the now.gg IAP service is available on the device:
bool isIAPAvailable = NowGGSDK::isNowGGIapAvailable();
Note:
onInitialized
callback function.This step will allow you to add the in-app purchase products using product ids in your app/game and initialize the SDK.
Create a vector of all the productIds
that you wish to add and pass those in the init function, as illustrated below:
void nowGGIAPInit() { bool isIAPAvailable = NowGGSDK::isNowGGIapAvailable(); if (isIAPAvailable) { vector<string> productIds {"coin1", "coin2", "coin3"}; NowGGSDK::init("PAYMENT_ID", productIds); } }
Note:
PAYMENT_ID
is a unique identifier for your app.PAYMENT_ID
.
PAYMENT_ID
can be found within the App Details section of nowStudio – more information.After successful initialization, you can get the product details in your app. To get product details (SkuDetails) use the getProductDetails
function as illustrated below:
vector<SkuDetails*> skuDetails = NowGGSDK::getProductDetails();
Note: You should call the getProductDetails function only after INIT success.
After you have the product details, you can initiate a product purchase by calling the purchase()
function along with the product id, as illustrated below:
NowGGSDK::purchase(<product id of the product>, "inapp");
Note:
ProductId
is a unique identifier for the in-app product(s) that you have defined within nowStudio for your specific App (App Id).After a successful purchase, you can assign the purchased product to the user by using the consume()
function as illustrated below:
NowGGSDK::consume(<purchaseToken associated with a purchased product>);
Note:
purchaseToken
after a product has been purchased.onPurchaseConsumed
callback is called on successful operation.now.gg Payments utilize a public key to verify in-app purchases, enabling you to establish the authenticity of a purchase. It is recommended to authenticate every purchase prior to assigning the product to the user.
You can either verify purchase(s) locally or use a backend server. However, we recommend using your backend server to verify a purchase.
The following code segment illustrates the verification of purchase(s) using a backend server. You can refer to the Sample project for more details.
void VerifyValidSignatureOnBackendServer(std::string originalJson, std::string signature) { __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyValidSignatureOnBackendServer called with signature %s \n", signature.c_str()); cocos2d::network::HttpRequest* request = new cocos2d::network::HttpRequest(); request->setUrl("<your cloud url here>); request->setRequestType(cocos2d::network::HttpRequest::Type::POST); std::vector<std::string> headers; headers.push_back("Content-Type, application/x-www-form-urlencoded"); request->setHeaders(headers); request->setResponseCallback([](network::HttpClient* sender, network::HttpResponse* response) { if (!response->isSucceed()) { __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyValidSignatureOnBackendServer failed \n"); return; } __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyValidSignatureOnBackendServer success \n"); std::vector<char>* buffer = response->getResponseData(); char* json = (char*)malloc(buffer->size() + 1); std::string s2(buffer->begin(), buffer->end()); strcpy(json, s2.c_str()); __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyValidSignatureOnBackendServer response: %s \n", json); }); string data = "data=" + originalJson + "&signature=" + url_encode(signature); __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyValidSignatureOnBackendServer data:%s \n", data.c_str()); request->setRequestData(data.c_str(), strlen(data.c_str())); cocos2d::network::HttpClient::getInstance()->send(request); request->release(); }
The download package contains a sample implementation to verify a purchase using a backend server which you can use to write your implementation.
The sample implementation includes:
We have provided a sample implementation within the Sample Project to verify purchases locally. You can use it to write your implementation.
In the sample implementation:
VerifyPurchaseLocally
function of the PurchaseVerification
class.We have also included a Sample project in the download package. You can refer to this project to understand the IAP flow using now.gg Payments.
The sample project is included in the package in the cocosSDKSample
directory.
Payments Module
Payments Module
Document Rev. 1.0