Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Support async queryPurchasesAsync()
  • Loading branch information
GaryQian committed Jul 7, 2022
commit 6cb46b1a3234d42683311e7566747b6f81df7fd8
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,26 @@ public void onConsumeResponse(BillingResult billingResult, String outToken) {
billingClient.consumeAsync(params, listener);
}

private void queryPurchases(String skuType, MethodChannel.Result result) {
private void queryPurchasesAsync(String skuType, MethodChannel.Result result) {
if (billingClientError(result)) {
return;
}

// Like in our connect call, consider the billing client responding a "success" here regardless
// of status code.
result.success(fromPurchasesResult(billingClient.queryPurchases(skuType)));
billingClient.queryPurchaseHistoryAsync(
skuType,
new PurchasesResponseListener() {
@Override
public void onQueryPurchasesResponse(
BillingResult billingResult, List<Purchase> purchasesList) {
final Map<String, Object> serialized = new HashMap<>();
serialized.put("billingResult", Translator.fromBillingResult(billingResult));
serialized.put(
"purchaseList", fromPurchaseList(purchasesList));
result.success(serialized);
}
});
}

private void queryPurchaseHistoryAsync(String skuType, final MethodChannel.Result result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,47 @@ public void launchBillingFlow_ok_Proration_with_null_OldSku() {
verify(result, never()).success(any());
}

@Test
public void launchBillingFlow_ok_Full() {
// Fetch the sku details first and query the method call
String skuId = "foo";
String oldSkuId = "oldFoo";
String purchaseToken = "purchaseTokenFoo";
String accountId = "account";
int prorationMode = BillingFlowParams.ProrationMode.IMMEDIATE_AND_CHARGE_FULL_PRICE;
queryForSkus(unmodifiableList(asList(skuId, oldSkuId)));
HashMap<String, Object> arguments = new HashMap<>();
arguments.put("sku", skuId);
arguments.put("accountId", accountId);
arguments.put("oldSku", oldSkuId);
arguments.put("purchaseToken", purchaseToken);
arguments.put("prorationMode", prorationMode);
MethodCall launchCall = new MethodCall(LAUNCH_BILLING_FLOW, arguments);

// Launch the billing flow
BillingResult billingResult =
BillingResult.newBuilder()
.setResponseCode(100)
.setDebugMessage("dummy debug message")
.build();
when(mockBillingClient.launchBillingFlow(any(), any())).thenReturn(billingResult);
methodChannelHandler.onMethodCall(launchCall, result);

// Verify we pass the arguments to the billing flow
ArgumentCaptor<BillingFlowParams> billingFlowParamsCaptor =
ArgumentCaptor.forClass(BillingFlowParams.class);
verify(mockBillingClient).launchBillingFlow(any(), billingFlowParamsCaptor.capture());
BillingFlowParams params = billingFlowParamsCaptor.getValue();
assertEquals(params.getSku(), skuId);
assertEquals(params.getOldSku(), oldSkuId);
assertEquals(params.getOldSkuPurchaseToken(), purchaseToken);
assertEquals(params.getReplaceSkusProrationMode(), prorationMode);

// Verify we pass the response code to result
verify(result, never()).error(any(), any(), any());
verify(result, times(1)).success(fromBillingResult(billingResult));
}

@Test
public void launchBillingFlow_clientDisconnected() {
// Prepare the launch call after disconnecting the client
Expand Down