Skip to content
This repository was archived by the owner on Oct 20, 2020. It is now read-only.

Commit f969812

Browse files
c-h-cbrevik
authored andcommitted
Update to support RN 29+ (#21)
* Add support for React Native 29+ * Add instructions for RN 29 to Readme. Update rnpm link in package.json. Add back InAppBillingBridgePackage with no arguments. Update target React Native compilation dependency. * Use getCurrentActivity method everywhere instead of keeping a reference to an activity.
1 parent 6c33cb4 commit f969812

5 files changed

Lines changed: 79 additions & 66 deletions

File tree

README.md

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,59 +43,80 @@ With this, [rnpm](https://github.com/rnpm/rnpm) will do most of the heavy liftin
4343
}
4444
```
4545

46-
4. Edit `MainActivity.java`. Step 4.3 is only required if you are using a lower React Native version than 18.0 and/or your `MainActivity` class does not inherit from `ReactActivity`.
47-
1. Add `import com.idehub.Billing.InAppBillingBridgePackage;`
48-
2. Register package in ReactInstanceManager: `.addPackage(new InAppBillingBridgePackage(this))`
49-
3. Override `onActivityResult`:
50-
```java
51-
@Override
52-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
53-
mReactInstanceManager.onActivityResult(requestCode, resultCode, data);
54-
}
55-
```
56-
57-
Larger example:
58-
59-
```java
60-
// Step 1; import package:
61-
import com.idehub.Billing.InAppBillingBridgePackage;
62-
63-
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
64-
...
65-
66-
@Override
67-
protected void onCreate(Bundle savedInstanceState) {
68-
...
69-
70-
mReactInstanceManager = ReactInstanceManager.builder()
71-
.setApplication(getApplication())
72-
.setBundleAssetName("index.android.bundle")
73-
.setJSMainModuleName("index.android")
74-
.addPackage(new MainReactPackage())
75-
// Step 2; register package, with your and send in the MainActivity as a parameter (this):
76-
.addPackage(new InAppBillingBridgePackage(this))
77-
.setUseDeveloperSupport(BuildConfig.DEBUG)
78-
.setInitialLifecycleState(LifecycleState.RESUMED)
79-
.build();
80-
81-
...
82-
}
83-
// Step 3: For RN < v0.18, override onActivityResult
84-
@Override
85-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
46+
4. Update MainActivity or MainApplication depending on React Native version.
47+
- React Native version >= 0.29
48+
Edit `MainApplication.java`.
49+
1. Add `import com.idehub.Billing.InAppBillingBridgePackage;`
50+
2. Register package:
51+
```java
52+
@Override
53+
protected List<ReactPackage> getPackages() {
54+
return Arrays.<ReactPackage>asList(
55+
new MainReactPackage(),
56+
// add package here
57+
new InAppBillingBridgePackage()
58+
);
59+
}
60+
```
61+
- React Native version < 0.29
62+
Edit `MainActivity.java`. Step 4.3 is only required if you are using a lower React Native version than 18.0 and/or your `MainActivity` class does not inherit from `ReactActivity`.
63+
1. Add `import com.idehub.Billing.InAppBillingBridgePackage;`
64+
2. Register package in ReactInstanceManager: `.addPackage(new InAppBillingBridgePackage())`
65+
3. Override `onActivityResult`:
66+
```java
67+
@Override
68+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
8669
mReactInstanceManager.onActivityResult(requestCode, resultCode, data);
87-
}
88-
...
89-
```
70+
}
71+
```
72+
73+
Larger example:
74+
75+
```java
76+
// Step 1; import package:
77+
import com.idehub.Billing.InAppBillingBridgePackage;
78+
79+
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
80+
...
81+
82+
@Override
83+
protected void onCreate(Bundle savedInstanceState) {
84+
...
85+
86+
mReactInstanceManager = ReactInstanceManager.builder()
87+
.setApplication(getApplication())
88+
.setBundleAssetName("index.android.bundle")
89+
.setJSMainModuleName("index.android")
90+
.addPackage(new MainReactPackage())
91+
// Step 2; register package
92+
.addPackage(new InAppBillingBridgePackage())
93+
.setUseDeveloperSupport(BuildConfig.DEBUG)
94+
.setInitialLifecycleState(LifecycleState.RESUMED)
95+
.build();
96+
97+
...
98+
}
99+
// Step 3: For RN < v0.18, override onActivityResult
100+
@Override
101+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
102+
mReactInstanceManager.onActivityResult(requestCode, resultCode, data);
103+
}
104+
...
105+
```
90106
5. Add your Google Play license key as a line to your `android/app/src/main/res/values/strings.xml` with the name `RNB_GOOGLE_PLAY_LICENSE_KEY`. For example:
91107
```xml
92108
<string name="RNB_GOOGLE_PLAY_LICENSE_KEY">YOUR_GOOGLE_PLAY_LICENSE_KEY_HERE</string>
93109
```
94110
Alternatively, you can add your license key as a parameter when registering the `InAppBillingBridgePackage`, like so:
95111
```java
96-
.addPackage(new InAppBillingBridgePackage("YOUR_LICENSE_KEY", this))
112+
.addPackage(new InAppBillingBridgePackage("YOUR_LICENSE_KEY"))
113+
```
114+
or for React Native 29+
115+
```java
116+
new InAppBillingBridgePackage("YOUR_LICENSE_KEY")
97117
```
98118

119+
99120
## Testing with static responses
100121
If you want to test with static responses, you can use reserved productids defined by Google. These are:
101122
* android.test.purchased
@@ -113,7 +134,7 @@ In order to do this send in `null` as parameter, along with your Activity-instan
113134
[See the Google Play docs for more info on static responses](http://developer.android.com/google/play/billing/billing_testing.html#billing-testing-static).
114135

115136
## Testing with your own In-app products
116-
Testing with static responses is limited, because you are only able to test the `purchase` function. Therefore, testing with real In-app products is recommended. But before that is possible, you need to do the following:
137+
Testing with static responses is limited, because you are only able to test the `purchase` function. Therefore, testing with real In-app products is recommended. But before that is possible, you need to do the following:
117138
* I will assume you've already created your Google Play Developer account and an application there.
118139
* Now you need to create an In-app product under your application at the Google Play Developer Console and activate it (press the button at the top right).
119140
* Assuming you have installed this module (InApp Billing), you can write the JS code as explained in the Javascript API section. I suggest you to use `getProductDetails` function to see if it's the product is retrieved.
@@ -123,7 +144,7 @@ Testing with static responses is limited, because you are only able to test the
123144
* The final part is, you'll need to add testers for the channel you've published to. The web page will give you a signup URL (opt-in) after you've approved open testing. Visit this URL in the browser of your **testing device** (it must be a physical device, not a emulator) and signup, and download the app where it redirected.
124145
* Try to buy something with the device. The purchase will eventually be cancelled, but you can also do this manually through your Google Merchant wallet.
125146

126-
**Important**: You can only test on a physical Android device, not from an emulator.
147+
**Important**: You can only test on a physical Android device, not from an emulator.
127148

128149

129150
## Javascript API
@@ -164,7 +185,7 @@ InAppBilling.open()
164185
* **receiptSignature:** String
165186
* **receiptData:** String
166187
* **developerPayload:** String
167-
188+
168189
```javascript
169190
InAppBilling.purchase('android.test.purchased')
170191
.then((details) => {
@@ -198,7 +219,7 @@ InAppBilling.consumePurchase('your.inapp.productid').then(...);
198219
* **receiptSignature:** String
199220
* **receiptData:** String
200221
* **developerPayload:** String
201-
222+
202223
```javascript
203224
InAppBilling.subscribe('your.inapp.productid')
204225
.then((details) => {

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ android {
1313
}
1414

1515
dependencies {
16-
compile 'com.facebook.react:react-native:0.15.+'
16+
compile 'com.facebook.react:react-native:+'
1717
compile 'com.anjlab.android.iab.v3:library:1.0.+'
1818
}

android/src/main/java/com/idehub/Billing/InAppBillingBridge.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,23 @@
2626
public class InAppBillingBridge extends ReactContextBaseJavaModule implements ActivityEventListener, BillingProcessor.IBillingHandler {
2727
ReactApplicationContext _reactContext;
2828
String LICENSE_KEY = null;
29-
final Activity _activity;
3029
BillingProcessor bp;
3130

32-
public InAppBillingBridge(ReactApplicationContext reactContext, String licenseKey, Activity activity) {
31+
public InAppBillingBridge(ReactApplicationContext reactContext, String licenseKey) {
3332
super(reactContext);
3433
_reactContext = reactContext;
3534
LICENSE_KEY = licenseKey;
36-
_activity = activity;
3735

3836
reactContext.addActivityEventListener(this);
3937
}
4038

41-
public InAppBillingBridge(ReactApplicationContext reactContext, Activity activity) {
39+
public InAppBillingBridge(ReactApplicationContext reactContext) {
4240
super(reactContext);
4341
_reactContext = reactContext;
4442
int keyResourceId = _reactContext
4543
.getResources()
4644
.getIdentifier("RNB_GOOGLE_PLAY_LICENSE_KEY", "string", _reactContext.getPackageName());
4745
LICENSE_KEY = _reactContext.getString(keyResourceId);
48-
_activity = activity;
4946

5047
reactContext.addActivityEventListener(this);
5148
}
@@ -124,7 +121,7 @@ public void onBillingError(int errorCode, Throwable error) {
124121
public void purchase(final String productId, final String developerPayload, final Promise promise){
125122
if (bp != null) {
126123
if (putPromise(PromiseConstants.PURCHASE_OR_SUBSCRIBE, promise)) {
127-
boolean purchaseProcessStarted = bp.purchase(_activity, productId, developerPayload);
124+
boolean purchaseProcessStarted = bp.purchase(getCurrentActivity(), productId, developerPayload);
128125
if (!purchaseProcessStarted)
129126
rejectPromise(PromiseConstants.PURCHASE_OR_SUBSCRIBE, "Could not start purchase process.");
130127
} else {
@@ -156,7 +153,7 @@ public void consumePurchase(final String productId, final Promise promise) {
156153
public void subscribe(final String productId, final String developerPayload, final Promise promise){
157154
if (bp != null) {
158155
if (putPromise(PromiseConstants.PURCHASE_OR_SUBSCRIBE, promise)) {
159-
boolean subscribeProcessStarted = bp.subscribe(_activity, productId, developerPayload);
156+
boolean subscribeProcessStarted = bp.subscribe(getCurrentActivity(), productId, developerPayload);
160157
if (!subscribeProcessStarted)
161158
rejectPromise(PromiseConstants.PURCHASE_OR_SUBSCRIBE, "Could not start subscribe process.");
162159
} else {

android/src/main/java/com/idehub/Billing/InAppBillingBridgePackage.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.idehub.Billing;
22

3-
import android.app.Activity;
4-
53
import com.facebook.react.ReactPackage;
64
import com.facebook.react.bridge.JavaScriptModule;
75
import com.facebook.react.bridge.NativeModule;
@@ -14,28 +12,25 @@
1412

1513
public class InAppBillingBridgePackage implements ReactPackage {
1614

17-
public InAppBillingBridgePackage(String licenseKey, Activity activity) {
15+
public InAppBillingBridgePackage(String licenseKey) {
1816
_licenseKey = licenseKey;
1917
_licenseKeySetInConstructor = true;
20-
_activity = activity;
2118
}
2219

23-
public InAppBillingBridgePackage(Activity activity) {
24-
_activity = activity;
20+
public InAppBillingBridgePackage() {
2521
}
2622

2723
private String _licenseKey;
2824
private Boolean _licenseKeySetInConstructor = false;
29-
private Activity _activity;
3025

3126
@Override
3227
public List<NativeModule> createNativeModules(
3328
ReactApplicationContext reactContext) {
3429
List<NativeModule> modules = new ArrayList<>();
3530
if (!_licenseKeySetInConstructor)
36-
modules.add(new InAppBillingBridge(reactContext, _activity));
31+
modules.add(new InAppBillingBridge(reactContext));
3732
else
38-
modules.add(new InAppBillingBridge(reactContext, _licenseKey, _activity));
33+
modules.add(new InAppBillingBridge(reactContext, _licenseKey));
3934

4035
return modules;
4136
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"homepage": "https://github.com/idehub/react-native-billing#readme",
2929
"rnpm": {
3030
"android": {
31-
"packageInstance": "new InAppBillingBridgePackage(this)"
31+
"packageInstance": "new InAppBillingBridgePackage()"
3232
}
3333
}
3434
}

0 commit comments

Comments
 (0)