Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
5 changes: 5 additions & 0 deletions packages/in_app_purchase/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.3.4+3

* Update package:e2e reference to use the local version in the flutter/plugins
repository.

## 0.3.4+2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line after the version

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff seems odd, CHANGELOG probably needs to be re-based


* Update package:e2e reference to use the local version in the flutter/plugins
Expand Down
16 changes: 10 additions & 6 deletions packages/in_app_purchase/ios/Classes/FIAPReceiptManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ @implementation FIAPReceiptManager

- (NSString *)retrieveReceiptWithError:(FlutterError **)error {
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [self getReceiptData:receiptURL];
NSError *err;
NSData *receipt = [self getReceiptData:receiptURL error:&err];
if (err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add an xctest to test this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

friendly ping @LHLL

*error = [FlutterError errorWithCode:[[NSString alloc] initWithFormat:@"%li", (long)err.code]
message:err.domain
details:err.userInfo];
return nil;
}
if (!receipt) {
*error = [FlutterError errorWithCode:@"storekit_no_receipt"
message:@"Cannot find receipt for the current main bundle."
details:nil];
return nil;
}
return [receipt base64EncodedStringWithOptions:kNilOptions];
}

- (NSData *)getReceiptData:(NSURL *)url {
return [NSData dataWithContentsOfURL:url];
- (NSData *)getReceiptData:(NSURL *)url error:(NSError **)error {
return [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:error];
}

@end
2 changes: 1 addition & 1 deletion packages/in_app_purchase/ios/Tests/Stubs.m
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ - (instancetype)initWithMap:(NSDictionary *)map {

@implementation FIAPReceiptManagerStub : FIAPReceiptManager

- (NSData *)getReceiptData:(NSURL *)url {
- (NSData *)getReceiptData:(NSURL *)url error:(NSError **)error {
NSString *originalString = [NSString stringWithFormat:@"test"];
return [[NSData alloc] initWithBase64EncodedString:originalString options:kNilOptions];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,18 @@ class AppStoreConnection implements InAppPurchaseConnection {
@override
Future<PurchaseVerificationData> refreshPurchaseVerificationData() async {
await SKRequestMaker().startRefreshReceiptRequest();
String receipt = await SKReceiptManager.retrieveReceiptData();
return PurchaseVerificationData(
localVerificationData: receipt,
serverVerificationData: receipt,
source: IAPSource.AppStore);
try {
String receipt = await SKReceiptManager.retrieveReceiptData();
return PurchaseVerificationData(
localVerificationData: receipt,
serverVerificationData: receipt,
source: IAPSource.AppStore);
} catch (e) {
print(
'Something is wrong while fetching the receipt, this normally happens when the app is '
'running on a simulator: $e');
return null;
}
}

/// Query the product detail list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class SKReceiptManager {
/// There are 2 ways to do so. Either validate locally or validate with App Store.
/// For more details on how to validate the receipt data, you can refer to Apple's document about [`About Receipt Validation`](https://developer.apple.com/library/archive/releasenotes/General/ValidateAppStoreReceipt/Introduction.html#//apple_ref/doc/uid/TP40010573-CH105-SW1).
/// If the receipt is invalid or missing, you can use [SKRequestMaker.startRefreshReceiptRequest] to request a new receipt.
static Future<String> retrieveReceiptData() {
return channel.invokeMethod<String>(
static Future<String> retrieveReceiptData() async {
return await channel.invokeMethod<String>(
'-[InAppPurchasePlugin retrieveReceiptData:result:]');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:in_app_purchase/src/channel.dart';
import 'package:in_app_purchase/store_kit_wrappers.dart';
import 'package:test/test.dart' show TypeMatcher;
import 'sk_test_stub_objects.dart';

void main() {
Expand Down Expand Up @@ -76,6 +77,12 @@ void main() {
String receiptData = await SKReceiptManager.retrieveReceiptData();
expect(receiptData, 'receipt data');
});

test('should get null receipt if any exceptions are raised', () async {
fakeIOSPlatform.getReceiptFailTest = true;
expect(() async => SKReceiptManager.retrieveReceiptData(),
throwsA(TypeMatcher<PlatformException>()));
});
});

group('sk_payment_queue', () {
Expand Down Expand Up @@ -128,11 +135,15 @@ class FakeIOSPlatform {
FakeIOSPlatform() {
channel.setMockMethodCallHandler(onMethodCall);
getProductRequestFailTest = false;
getReceiptFailTest = false;
}
// get product request
List startProductRequestParam;
bool getProductRequestFailTest;

// get receipt request
bool getReceiptFailTest;

// refresh receipt request
int refreshReceipt = 0;
Map refreshReceiptParam;
Expand Down Expand Up @@ -161,6 +172,9 @@ class FakeIOSPlatform {
return Future<void>.sync(() {});
// receipt manager
case '-[InAppPurchasePlugin retrieveReceiptData:result:]':
if (getReceiptFailTest) {
throw ("some arbitrary error");
}
return Future<String>.value('receipt data');
// payment queue
case '-[SKPaymentQueue canMakePayments:]':
Expand Down