2

I am implementing code for in app purchase with itune connect and my server to download files.

I want to send product ids and identifierForVendor and transaction.bytes In JSON by using "POST". Here is Image that show hierarchy. enter image description here

I search on google and i found this part but its in NSstring and it sends by GET method

- (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction {
NSString *jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];
NSString *completeString = [NSString stringWithFormat:@"http://localhost:8888/amm/php_testing_json.php?receipt=%@", jsonObjectString];
NSURL *urlForValidation = [NSURL URLWithString:completeString];
NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation];
[validationRequest setHTTPMethod:@"GET"];
NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];

NSString *responseString = [[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding];
NSInteger response = [responseString integerValue];

return (response == 0);
 }

- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length {
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

NSMutableData *data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t *output = (uint8_t *)data.mutableBytes;

for (NSInteger i = 0; i < length; i += 3) {
    NSInteger value = 0;
    for (NSInteger j = i; j < (i + 3); j++) {
        value <<= 8;

        if (j < length) {
            value |= (0xFF & input[j]);
        }
    }

    NSInteger index = (i / 3) * 4;
    output[index + 0] =                    table[(value >> 18) & 0x3F];
    output[index + 1] =                    table[(value >> 12) & 0x3F];
    output[index + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
    output[index + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
}

return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

}

Any help is Appreciated Thank you in advance

Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77

1 Answers1

0

First make sure you want a synchronous request for your task. If you do, then actually you're almost there. But note that in order to send POST requests to your server, your server must support them. You shouldn't put the parameters right into the url, so in the following piece this is taken into consideration:

- (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction
{
    NSString *jsonObjectString = [self encode:(uint8_t*)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];
    NSString *completeString = @"http://localhost:8888/amm/php_testing_json"; // just an URL your server developer will give you
    NSURL *urlForValidation = [NSURL URLWithString:completeString];
    NSMutableURLRequest *validationRequest = [NSMutableURLRequest requestWithURL:urlForValidation cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0f];
    [validationRequest setHTTPMethod:@"POST"];
    [validationRequest setHTTPBody:[jsonObjectString dataUsingEncoding:NSUTF8StringEncoding]]; // the string with receipt we set into the http body, not straight into parameters in url
    // Now, when the request is ready, send it and wait for a response
    NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];

    NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
    if (responseString && [responseString length])
        return YES;
    return NO;
}

And of course, in the end you should check out the response string properly (depends on what you expect to see in that response in different cases), not just by its presence or absence :-) Hope this helps

makaron
  • 1,585
  • 2
  • 16
  • 30
  • thank you for help but how can i put identifireforvendor number , name of iphone , os and device , product id?? how can i create JSON by using this value? can you help me? – Swap-IOS-Android May 30 '13 at 15:47
  • Now I'm writing both server and client for in-app purchases and I can tell you (if I understand your question correctly) that it's the author of the *server*, who can help you. For apple's receipt validation it's enough just to send a receipt, all the rest server's author most likely uses for custom needs: server(local) receipt validation (preliminary validation before sending receipt for Apple's validation), logging, transactions database etc. – makaron May 30 '13 at 16:02
  • So for now, just for tests, you can fill the fields with fake hardcoded data (except *receipt*, it must be real. And perhaps productId (partially server could validate receipts locally as well) - it's the ID of the product which you were buying and as a result got your receipt). So send your request with fake data and see if you get the response you expect – makaron May 30 '13 at 16:03
  • ok i will try first to send transaction.bytes and length by json..but my server developer want these additional information also into that json..so i need to add it in my JSON but i dont know how can i add this string into JSON... should i need to use Nsserialization and NSdictionary to create JSON? – Swap-IOS-Android May 30 '13 at 16:06
  • 1
    In order to fully support work with JSON, you can use any of the frameworks available in the internet, there are a lot of them. In particular, there are a lot of proper topics around stackoverflow, e.g.: http://stackoverflow.com/questions/5813077/iphone-ios-json-parsing-tutorial or http://stackoverflow.com/questions/3562478/native-json-support-in-ios Usually they convert NSDictionary into a string with needed format. But if you just need to form this only json, perhaps it's easier just to form it by your hands with [NSString stringWithFormat:]. After all, JSON is just a string :-) – makaron May 30 '13 at 16:47