Skip to content

Commit

Permalink
Amazon IAP support added
Browse files Browse the repository at this point in the history
  • Loading branch information
iaphub committed Sep 27, 2020
1 parent 97f56d2 commit 6e9f0f8
Show file tree
Hide file tree
Showing 9 changed files with 835 additions and 41 deletions.
47 changes: 47 additions & 0 deletions README_AMAZON.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Amazon IAP Support
------------------
The guide assumes that `react-native-iap` is implemented in your app and it works with Google Play with no issues.
Here are the additional steps to add Amazon IAP support.
- Add In-App Items for your app:
1. Create "In-App Items" using Amazon Developer portal for your app. Amazon put up detailed instructions at https://developer.amazon.com/docs/in-app-purchasing/iap-create-and-submit-iap-items.html
2. Add new SKU strings to your `RNIap.getProducts` or `RNIap.getSubscriptions` calls

- App configuration
1. The current version of Amazon IAP SDK does not play well with R8 optimization. (https://developer.amazon.com/docs/in-app-purchasing/iap-obfuscate-the-code.html).
Add the code below in `android/app/proguard-rules.pro`
```
-dontwarn com.amazon.**
-keep class com.amazon.** {*;}
-keepattributes *Annotation*
```

2. Update AndroidManifest.xml to add the following:
```
<application>
...
<receiver android:name = "com.amazon.device.iap.ResponseReceiver"
android:permission = "com.amazon.inapp.purchasing.Permission.NOTIFY" >
<intent-filter>
<action android:name = "com.amazon.inapp.purchasing.NOTIFY" />
</intent-filter>
</receiver>
...
</application>
```

Testing in development
----------------------
The `react-native-iap` checks the install source of the app and determines the usage of Amazon IAP API based on that.
The development environment does not set the install source, the default is to use Google Play as a fallback in this case.
If the fallback needs to be changed to use Amazon Appstore while testing, the following code snippet needed just before calling `RNIap.initConnection` for the first time in the app:
```
if (__DEV__) {
RNIap.setFallbackInstallSourceAndroid(RNIap.InstallSourceAndroid.AMAZON);
}
RNIap.initConnection(...)
```
Amazon offers the `App Tester` tool to make in-app purchases testing easier. More information can be found at https://developer.amazon.com/docs/in-app-purchasing/iap-app-tester-user-guide.html

Server Validation
-----------------
Amazon IAP API supports validation of in-app purchases on a remote server side. More information is available at https://developer.amazon.com/docs/in-app-purchasing/iap-rvs-for-android-apps.html
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ repositories {
dependencies {
implementation 'com.facebook.react:react-native:+'
implementation 'com.android.billingclient:billing:3.0.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
// implementation files('libs/in-app-purchasing-2.0.76.jar')
def supportLibVersion = safeExtGet('supportLibVersion', safeExtGet('supportVersion', null))
def androidXVersion = safeExtGet('androidXVersion', null)
if (supportLibVersion && androidXVersion == null) {
Expand Down
Binary file added android/libs/in-app-purchasing-2.0.76.jar
Binary file not shown.
23 changes: 23 additions & 0 deletions android/src/main/java/com/dooboolab/RNIap/DoobooUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.util.Log;

import com.android.billingclient.api.BillingClient;
import android.content.Context;
import android.content.pm.PackageManager;
import com.facebook.react.bridge.ObjectAlreadyConsumedException;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReadableArray;
Expand Down Expand Up @@ -36,6 +38,10 @@ public class DoobooUtils {
public static final String E_DEVELOPER_ERROR = "E_DEVELOPER_ERROR";
public static final String E_BILLING_RESPONSE_JSON_PARSE_ERROR = "E_BILLING_RESPONSE_JSON_PARSE_ERROR";

public static final String APPSTORE_UNKNOWN = "UNKNOWN";
public static final String APPSTORE_GOOGLE = "GOOGLE_PLAY";
public static final String APPSTORE_AMAZON = "AMAZON";

private HashMap<String, ArrayList<Promise>> promises = new HashMap<>();
private static DoobooUtils instance = new DoobooUtils();

Expand Down Expand Up @@ -263,4 +269,21 @@ public JSONArray convertArrayToJson(ReadableArray readableArray) throws JSONExce
}
return array;
}

public final String getInstallSource(Context context) {
Context appContext = context.getApplicationContext();
PackageManager pkgManager = appContext.getPackageManager();
String installerPackageName = pkgManager.getInstallerPackageName(appContext.getPackageName());

if (installerPackageName == null) {
return APPSTORE_UNKNOWN;
} else if ("com.android.vending".equals(installerPackageName)) {
return APPSTORE_GOOGLE;
} else if (installerPackageName.startsWith("com.amazon.")) {
return APPSTORE_AMAZON;
} else {
Log.d(TAG, "Unknown installer source: " + installerPackageName);
}
return APPSTORE_UNKNOWN;
}
}
Loading

0 comments on commit 6e9f0f8

Please sign in to comment.