This package provides the functionality of Payment through ZarinPal in Unity on Android
ZarinPal in-app purchases are the simplest solution to selling digital products or content on Android apps. So many app developers who want to sell digital goods or offer premium membership to users can simply use the it, in-app billing process for smooth and easy checkouts.
- Unity 2019.3 Or Later
- Android Target Platform installed for Unity
You can install package either directly from Unity's Package Manager by git Url or by downloading this repo and Putting it under Packages folder of your project.
By Git Url
Open Package Manager ( Window -> Package Manager ). In Package Manager window click on the top-left button with + icon, choose add package from git URL... , enter or copy the following link ( https://github.com/AliA74/ZarinPal-IAB-Unity-Plugin.git
)
Embedding in the project
If you don't want to use the previous method for installing the package, You can do the following:
- create a folder titled
com.zarinpal.iab
under Packages folder inside the root folder of your project. - clone/download this repository and put all the content inside the folder you created at previous step (
com.zarinpal.iab
)
This package requires some setup before it can be used correctly on Android. Make sure active build target is Android in Unity before continuing.
Open Player Settings (Edit -> Project Settings -> Player). Select Android Settings then select Publishing Settings. Check both Custom Main Gradle Template and Custom Gradle Properties Template
Open the file Assets/Plugins/Android/mainTemplate.gradle
by a text editor. Add the following line to the dependencies section:
implementation 'com.zarinpal:payment-provider:0.5.2'
(Optional) If your project and business is trusted to ZarinPal, SDK is able to provide Mobile Payment Gateway on your App so you can add the MPG dependency:
implementation 'com.zarinpal:mpg:0.5.2'
Note. You can change the version of the ZarinPal SDK inside the dependencies you just added. At the time of writing this document the latest version is 0.5.2
. You can check for newer versons here: https://github.com/ZarinPal/Android-SDK/releases
Open the file Assets/Plugins/Android/gradleTemplate.properties
by a text editor. Add the following lines to it if not already present:
android.useAndroidX=true
android.enableJetifier=true
Set Minimum Android API Level to Android 5.0 (API Level 21)
.
You can find this setting inside Android Player Settings (which we visited in Step-1) and in Other Settings.
If your eligible to have payment process through MPG and willing to use MPG, add usesCleartextTraffic
to application tag in your Assets/Plugins/Android/AndroidManifest.xml
:
<application
android:name="..."
android:usesCleartextTraffic="true"
....
\>
To use the ZarinPal API inside your scripts add a using directive at the top of your script:
using Zarinpal;
Hint. If Zarinpal
is not recognized you may have to add ZarinPalAssembly
to your Scripts Assembly Definition.
You have to call ZarinpalIAB.Init()
at first before calling any other method on ZarinpalIAB
.
Example:
void Start()
{
ZarinpalIAB.Init(showInvoice: true);
}
You can use following three static methods to make purchase:
ZarinpalIAB.PurchaseBySKU(string SkuID)
ZarinpalIAB.PurchaseByPaymentRequest(string merchantId, long amountInToman, string callbackURL, string description,string mobileNumber = null, string email = null)
ZarinpalIAB.PurchaseByAuthority(string Authority)
You can use following three static methods to query purchased sku(s):
ZarinpalIAB.QueryPurchasedSkusByMobile(string merchantId, string mobileNumber,string[] skuList)
ZarinpalIAB.QueryPurchasedSkusByEmail(string merchantId, string email, string[] skuList)
ZarinpalIAB.QueryPurchasedSkusCardPan(string merchantId, string cardPan, string[] skuList)
You can subscribe to the following events to get notified from purchase results and query results.
ZarinpalIAB.OnPurchase
ZarinpalIAB.OnQuery
ZarinpalIAB.OnQueryError