比较乱,在#if 0中,主要用作测试。
- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"-----completeTransaction--------");
// Your application should implement these two methods.
NSString *product = transaction.payment.productIdentifier;
if ([product length] > 0) {
NSArray *tt = [product componentsSeparatedByString:@"."];
NSString *bookid = [tt lastObject];
if ([bookid length] > 0) {
[self recordTransaction:bookid];
[self provideContent:bookid];
}
}
//[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
//交易验证
NSURL *recepitURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSLog(@"-----交易验证--------:%@", recepitURL);
NSData *receipt = [NSData dataWithContentsOfURL:recepitURL];
if(!receipt){
NSLog(@"-----数据有误 --------");
[self updatePurchaseStatus:2 errCode:108];
return;
}
NSError *error;
NSString *applicationUsername = transaction.payment.applicationUsername;
static int s_currStatus = 10;
if (s_currStatus == 0) {
NSString *keychain_tokendata = (NSString *)[KeyChainStore load:keychain_iap_tokendata];
NSString *keychain_applicationUsername = (NSString *)[KeyChainStore load:keychain_iap_username];
[KeyChainStore save:keychain_iap_tokendata data:[receipt base64EncodedStringWithOptions:0]];
[KeyChainStore save:keychain_iap_username data:applicationUsername];
s_currStatus=1;
NSLog([receipt base64EncodedStringWithOptions:0]);
NSLog(applicationUsername);
return;
}
if ([applicationUsername length] == 0||s_currStatus==1) {
//如果在keychain中获取不到相关信息 则按漏单处理
NSString *keychain_tokendata = (NSString *)[KeyChainStore load:keychain_iap_tokendata];
NSString *keychain_applicationUsername = (NSString *)[KeyChainStore load:keychain_iap_username];
if ([keychain_tokendata isEqualToString: [receipt base64EncodedStringWithOptions:0]] &&
[keychain_applicationUsername length] > 0) {
NSLog(@"tokendata in keychain 找到了丢失的applicationUsername");
applicationUsername = keychain_applicationUsername;
}else{
[self updatePurchaseStatus:2 errCode:109];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
return;
}
}else{ //如果初始的applicationUsername不为0 则是一次正常的充值行为 需要写入keychain
[KeyChainStore save:keychain_iap_tokendata data:[receipt base64EncodedStringWithOptions:0]];
[KeyChainStore save:keychain_iap_username data:applicationUsername];
}
//[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
//return;
/////////////////////////////
#if 0
//这里获得订单去验证
//验证时候 返回 "status":21002," 可能就是你发送的字符串的问题了
NSDictionary *requestContents2 = @{
@"receipt-data": [receipt base64EncodedStringWithOptions:0]
};
NSData *requestData2 = [NSJSONSerialization dataWithJSONObject:requestContents2
options:0
error:&error];
if (!requestData2) {
NSLog(@"-----数据有误 --------");
[self updatePurchaseStatus:2 errCode:110];
return;
}
NSString *strURL2=@"https://sandbox.itunes.apple.com/verifyReceipt";
//NSString *strURL2=@"https://buy.itunes.apple.com/verifyReceipt";
// /2校验订单/1apple订单
NSLog(@"strURL:%@",strURL2);
[NSURL URLWithString:strURL2];
NSURL *storeURL2 = [NSURL URLWithString:strURL2];
NSMutableURLRequest *storeRequest2 = [NSMutableURLRequest requestWithURL:storeURL2];
[storeRequest2 setHTTPMethod:@"POST"];
[storeRequest2 setHTTPBody:requestData2];
//[storeRequest setHTTPBody:[[receipt base64EncodedStringWithOptions:0] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"发给亚哲::%s", requestData2.bytes);
// Make a connection to the iTunes Store on a background queue.
NSOperationQueue *queue2 = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest2 queue:queue2
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
NSLog(@"-----apple服务器效验失败1--------");
/* ... Handle error ... */
} else {
NSError *error;
NSLog(@"kdljsakljdklasjdklasjdklsajdlk%@",data);
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!jsonResponse) {
/* ... Handle error ...*/
[self updatePurchaseStatus:2 errCode:111];
NSLog(@"-----apple服务器效验失败2--------");
}
else
{
NSLog(@"-----apple服务器效验完成--------");
NSLog(@"收到苹果服务器数据::%s", data.bytes);
if (jsonResponse) {
int e = [jsonResponse[@"e"] intValue];
NSLog(@"e = %d", e);
if (e == 0) {
int d = [jsonResponse[@"d"] intValue];
NSLog(@"d = %d", d);
// Remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
NSLog(@"-----completeTransaction:Remove the transaction from the payment queue--------");
[self updatePurchaseStatus:0 errCode:d];
}
else
{
//[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
if (e == 10) //订单已经处理过
{
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
[self updatePurchaseStatus:5 errCode:e];
}
}
}
/* ... Send a response back to the device ... */
//Parse the Response
}
}];
//return;
///////////////////////////////
#else
NSDictionary *requestContents = @{
@"receipt-data": [receipt base64EncodedStringWithOptions:0]
,@"code": applicationUsername
};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
options:0
error:&error];
if (!requestData) {
NSLog(@"-----数据有误 --------");
[self updatePurchaseStatus:2 errCode:110];
return;
}
[self updatePurchaseStatus:4 errCode:0];
//In the test environment, use https://sandbox.itunes.apple.com/verifyReceipt
//In the real environment, use https://buy.itunes.apple.com/verifyReceipt
// Create a POST request with the receipt data.
//NSURL *storeURL2 = [NSURL URLWithString:@"https://buy.itunes.apple.com/verifyReceipt"];
//NSURL *storeURL = [NSURL URLWithString:@"http://192.168.0.118/api.php/testusa"];
//NSURL *storeURL = [NSURL URLWithString:@"http://192.168.0.70/Pay/USA/229868/adsfasdf/2"];
//计算字符串的MD5值 获取1到x之间的整数的代码如下:
int randInt = (arc4random() % 80000) + 10000;
NSString *randStr = [NSString stringWithFormat:@"%d", randInt];
randStr = [@"u" stringByAppendingString:randStr];
NSString *SendStr =randStr;
NSLog(@"randStr:%@",randStr);
NSString *str1 = [randStr substringFromIndex:3];
NSString *str2 = [randStr substringToIndex:3];
randStr = [str1 stringByAppendingString:@"$asa!2.¥*2@2d^"];
randStr = [randStr stringByAppendingString:str2];
NSLog(@"str1:%@",str1);
NSLog(@"str2:%@",str2);
NSLog(@"randStr:%@",randStr);
//MD5 加密
const char* cStr = [randStr UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, (unsigned int)strlen(cStr), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
for (NSInteger i=0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:@"%02x", result[i]];
}
//MD5 加密完成 ret
//@"http://www.www.www/Pay/USA/229868/adsfasdf/2"
NSString *strMD5 =ret;
NSLog(@"%@",randStr);
NSLog(@"%@",strMD5);
NSString *strURL = [NSString stringWithFormat:@"http://api.beijingmj.com/Pay/app/%@/%@/2/1",SendStr,strMD5];
// /2校验订单/1apple订单
NSLog(@"strURL:%@",strURL);
[NSURL URLWithString:strURL];
NSURL *storeURL = [NSURL URLWithString:strURL];
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
[storeRequest setHTTPMethod:@"POST"];
[storeRequest setHTTPBody:requestData];
//[storeRequest setHTTPBody:[[receipt base64EncodedStringWithOptions:0] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"发给亚哲::%s", requestData.bytes);
// Make a connection to the iTunes Store on a background queue.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
NSLog(@"-----apple服务器效验失败1--------");
/* ... Handle error ... */
} else {
NSError *error;
NSLog(@"kdljsakljdklasjdklasjdklsajdlk%@",data);
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!jsonResponse) {
/* ... Handle error ...*/
[self updatePurchaseStatus:2 errCode:111];
NSLog(@"-----apple服务器效验失败2--------");
}
else
{
NSLog(@"-----apple服务器效验完成--------");
NSLog(@"收到亚哲数据::%s", data.bytes);
if (jsonResponse) {
int e = [jsonResponse[@"status"] intValue];
NSLog(@"e = %d", e);
if (e == 0) {
int d = [jsonResponse[@"d"] intValue];
NSLog(@"d = %d", d);
// Remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
NSLog(@"-----completeTransaction:Remove the transaction from the payment queue--------");
[self updatePurchaseStatus:0 errCode:d];
}
else
{
//[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
if (e == 10) //订单已经处理过
{
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
[self updatePurchaseStatus:5 errCode:e];
}
}
}
/* ... Send a response back to the device ... */
//Parse the Response
}
}];
#endif
}