2013-08-06 4 views
2

내 애플리케이션에서 지불 프로세스에 Braintree를 사용하고 있습니다.
[BTPaymentViewController paymentViewControllerWithVenmoTouchEnabled : NO]; 및 `Braintree 지불 프로세스를 사용하는 클라이언트 측 IOS 서버 서버

(void)paymentViewController:(BTPaymentViewController *)paymentViewController 
     didSubmitCardWithInfo:(NSDictionary *)cardInfo 
     andCardInfoEncrypted:(NSDictionary *)cardInfoEncrypted { 
     NSDictionary *dict=[self encryptFormData:cardInfo]; 
     [self savePaymentInfoToServer:dict]; 
} 

-(NSDictionary *) encryptFormData:(NSDictionary *) formData { 
    BTEncryption *braintree = [[BTEncryption alloc] initWithPublicKey: PUBLIC_KEY]; 
    NSMutableDictionary *encryptedParams = [[NSMutableDictionary alloc] init]; 

    [formData enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) { 
     [encryptedParams setObject: [braintree encryptString: object] forKey: key]; 
    }]; 

    return encryptedParams; 
} 

call to this method to post the data to localhost server for testing 
- (void) savePaymentInfoToServer:(NSDictionary *)paymentInfo { 
    NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"%@/card", SAMPLE_CHECKOUT_BASE_URL]]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 

    // You need a customer id in order to save a card to the Braintree vault. 
    // Here, for the sake of example, we set customer_id to device id. 
    // In practice, this is probably whatever user_id your app has assigned to this user. 
    // NSString *customerId = [[UIDevice currentDevice] identifierForVendor].UUIDString; 
    AppDelegate *appdelegate=(AppDelegate *) [[UIApplication sharedApplication]delegate]; 

    [paymentInfo setValue:appdelegate.referenceId forKey:@"bookingRefId"]; 
    [paymentInfo setValue:appdelegate.passengerId forKey:@"passengerId"]; 


    request.HTTPBody = [self postDataFromDictionary:paymentInfo]; 
    request.HTTPMethod = @"POST"; 
    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, NSData *body, NSError *requestError) 
    { 
     NSError *err = nil; 
     if (!response && requestError) { 
      NSLog(@"requestError: %@", requestError); 
      [self.paymentViewController showErrorWithTitle:@"Error" message:@"Unable to reach the network."]; 
      return; 
     } 

     NSDictionary *<b>responseDictionary</b> = [NSJSONSerialization JSONObjectWithData:body options:kNilOptions error:&err]; 
     NSLog(@"saveCardToServer: paymentInfo: %@ response: %@, error: %@", paymentInfo, responseDictionary, requestError); 

     if ([[responseDictionary valueForKey:@"success"] isEqualToNumber:@1]) { // Success! 
      // Don't forget to call the cleanup method, 
      // `prepareForDismissal`, on your `BTPaymentViewController` 
      [self.paymentViewController prepareForDismissal]; 
      // Now you can dismiss and tell the user everything worked. 
      [self dismissViewControllerAnimated:YES completion:^(void) { 
       [[[UIAlertView alloc] initWithTitle:@"Success" message:@"Saved your card!" delegate:nil 
            cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 

      }]; 

     } else { // The card did not save correctly, so show the error from server with convenenience method `showErrorWithTitle` 
      [self.paymentViewController showErrorWithTitle:@"Error saving your card" message:[self messageStringFromResponse:responseDictionary]]; 
     } 
    }]; 
}` 

이 responseDictionary를 포함하는 암호화를 위해이 방법을 사용하는가 null 오류 중 하나가 어디에 paymentInfo 사전을 보내는 나에게

답변

1

을 도울 수있는 문제를 해결하는 방법을 널 (즉 무엇인가 SAMPLE_CHECKOUT_BASE_URL)? Braintree에 의해 구축 된 예제 프로젝트는 마치 자신이 가지고있는 것처럼 백엔드를 시뮬레이션합니다. 해당 URL을 백엔드의 URL로 바꾸기를 원할 것입니다.

BTPaymentViewController은 클라이언트 쪽 신용 카드 체크 아웃 페이지를 제공하지만 백엔드는 여전히 트랜잭션을 실행해야합니다. 백엔드가 해당 트랜잭션을 실행하려면 해당 paymentInfo 사전을 서버에 보내야합니다.

iOS 앱용 백엔드를 아직 만들지 않은 경우 지급을 처리하기 위해 Braintree으로 신속하게 설정하고 승인받을 수 있습니다.