这段代码的重点是使用名为 MobileAppTracking 的服务跟踪购买,其中一些被正确跟踪。但是该服务显示此代码正在发送一堆额外的购买电话,iTunes Connect 中没有记录。以下大部分只是来自典型 IOS storekit 的样板代码。具体来说,此代码来自 InAppPurchaseManager,这是一个用于 IOS 购买的 IOS 插件。
这是我正在使用的插件: https ://github.com/phonegap/phonegap-plugins/tree/master/iPhone/InAppPurchaseManager
这是我修改后的代码:
// SKPaymentTransactionObserver methods
// called when the transaction status is updated
//
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
NSString *state, *error, *transactionIdentifier, *transactionReceipt, *productId;
NSInteger errorCode;
SKPayment *thePayment;
for (SKPaymentTransaction *transaction in transactions)
{
error = state = transactionIdentifier = transactionReceipt = productId = @"";
errorCode = 0;
BOOL shouldTrackEvent = false; // maybe this should just be for successful purchases.
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchasing:
continue;
case SKPaymentTransactionStatePurchased:
state = @"PaymentTransactionStatePurchased";
transactionIdentifier = transaction.transactionIdentifier;
transactionReceipt = [[transaction transactionReceipt] base64EncodedString];
productId = transaction.payment.productIdentifier;
//thePayment = transaction.payment;
//NSLog(@"localCurr=", thePayment.currency );
//NSLog(@"localCurr=", thePayment.localizedTitle );
shouldTrackEvent = true;
break;
case SKPaymentTransactionStateFailed:
state = @"PaymentTransactionStateFailed";
error = transaction.error.localizedDescription;
errorCode = transaction.error.code;
NSLog(@"error %d %@", errorCode, error);
break;
case SKPaymentTransactionStateRestored:
state = @"PaymentTransactionStateRestored";
transactionIdentifier = transaction.originalTransaction.transactionIdentifier;
transactionReceipt = [[transaction transactionReceipt] base64EncodedString];
productId = transaction.originalTransaction.payment.productIdentifier;
break;
default:
NSLog(@"Invalid state");
continue;
}
NSLog(@"state: %@", state);
NSArray *callbackArgs = [NSArray arrayWithObjects:
NILABLE(state),
[NSNumber numberWithInt:errorCode],
NILABLE(error),
NILABLE(transactionIdentifier),
NILABLE(productId),
NILABLE(transactionReceipt),
nil];
NSString *js = [NSString stringWithFormat:@"plugins.inAppPurchaseManager.updatedTransactionCallback.apply(plugins.inAppPurchaseManager, %@)", [callbackArgs JSONSerialize]];
NSLog(@"js: %@", js);
[self writeJavascript: js];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// The Rest is all tracking code. P
// default tracking event name
NSString *eventName = @"purchase";
// self.products is the dictionary (NString, SKProduct) to be created by the user
NSDictionary *dictProducts = true; //(NSDictionary *)savedValidProducts;
// get the product associated with this transaction
//SKProduct *product = (SKProduct *)([dictProducts objectForKey:transaction.payment.productIdentifier]);
// assign the currency code extracted from the transaction
NSString *currencyCode = @"";// [product.priceLocale objectForKey:NSLocaleCurrencyCode];
// transaction.payment.productIdentifier
// The problem with not retrieving the real price is, what if they purchase in another country??
float unitPrice = 0; //[product.price floatValue];
NSString *title;
title = @"Tester";
NSLog(@"productIdentifier: %@", transaction.payment.productIdentifier);
if( [transaction.payment.productIdentifier isEqualToString:@"2_REFILLS"] ) {
title = @"2 Energy Refills";
unitPrice = 1.99;
}
if( [transaction.payment.productIdentifier isEqualToString:@"6_REFILLS"] ) {
title = @"6 Energy Refills";
unitPrice = 4.99;
}
if( [transaction.payment.productIdentifier isEqualToString:@"15_REFILLS"] ) {
title = @"15 Energy Refills";
unitPrice = 9.99;
}
// extract transaction product quantity
int quantity = 1; //transaction.payment.quantity; // extract unit price of the product
// assign revenue generated from the current product
float revenue = unitPrice * quantity;
// create MAT tracking event item
NSDictionary *dictItem = @{ @"item" : title, // product.localizedTitle,
@"unit_price" : [NSString stringWithFormat:@"%f", unitPrice],
@"quantity" : [NSString stringWithFormat:@"%i", quantity],
@"revenue" : [NSString stringWithFormat:@"%f", revenue]
};
NSArray *arrEventItems = @[ dictItem ];
if(shouldTrackEvent) {
NSLog(@"Event Item = %@", arrEventItems);
// track the purchase transaction event
// Total event revenue = sum of even item revenues in arrEventItems + extraRevenue
float extraRevenue = 0; // default to zero
[[MobileAppTracker sharedManager] trackActionForEventIdOrName:eventName
eventIsId:NO
eventItems:arrEventItems
referenceId:transaction.transactionIdentifier
revenueAmount:extraRevenue
currencyCode:currencyCode
transactionState:transaction.transactionState];
NSLog(@"Transaction event tracked: %@", eventName);
}
}
}