如何从ios中的braintree结果中支付MetghodNounce

时间:2016-12-08 07:25:28

标签: ios braintree

我正在使用braintree ios sdk并使用他们的Drop in UI添加客户卡详细信息。我成功获得用户界面,在客户提交信息后,如何从结果对象获取paymentMEthodNounce。这是我的代码。

- (void)showDropIn:(NSString *)clientTokenOrTokenizationKey {
BTDropInRequest *request = [[BTDropInRequest alloc] init];
BTDropInController *dropIn = [[BTDropInController alloc] initWithAuthorization:clientTokenOrTokenizationKey request:request handler:^(BTDropInController * _Nonnull controller, BTDropInResult * _Nullable result, NSError * _Nullable error) {

    if (error != nil) {
        NSLog(@"ERROR");
    } else if (result.cancelled) {
        NSLog(@"CANCELLED");
    } else {

        // Use the BTDropInResult properties to update your UI
        // result.paymentOptionType
        // result.paymentMethod
        // result.paymentIcon
        // result.paymentDescription
    }
}];
[self presentViewController:dropIn animated:YES completion:nil];}

我认为当客户提交结果时,支付nounce将是结果对象??如果它认为是正确的,那么如何将该nounce转换为变量以将其发送到我的服务器。我在ios中相当新,所以任何帮助都将受到赞赏。

3 个答案:

答案 0 :(得分:0)

您实施BTDropInViewControllerDelegate以获取成功时的付款方式nonce,并在任何一种情况下取​​消Drop In UI:

- (void)dropInViewController:(__unused BTDropInViewController *)viewController didSucceedWithPaymentMethod:(BTPaymentMethod *)paymentMethod {
    [self postNonceToServer:paymentMethod.nonce]; // Send payment method nonce to your server
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)dropInViewControllerDidCancel:(__unused BTDropInViewController *)viewController {
    [self dismissViewControllerAnimated:YES completion:nil];
}

将付款方式nonce发送到服务器

- (void)postNonceToServer:(NSString *)paymentMethodNonce {
    // Update URL with your server
    NSURL *paymentURL = [NSURL URLWithString:@"https://your-server.example.com/checkout"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:paymentURL];
    request.HTTPBody = [[NSString stringWithFormat:@"payment_method_nonce=%@", paymentMethodNonce] dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPMethod = @"POST";

    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // TODO: Handle success and failure
    }] resume];
}

答案 1 :(得分:0)

BTDropInRequest *request = [[BTDropInRequest alloc] init];
request.amount = @"10";
BTDropInController *dropIn = [[BTDropInController alloc] initWithAuthorization:clientTokenOrTokenizationKey request:request handler:^(BTDropInController * _Nonnull controller, BTDropInResult * _Nullable result, NSError * _Nullable error) {

    if (error != nil) {
        NSLog(@"ERROR");
    } else if (result.cancelled) {
        NSLog(@"CANCELLED");
    } else {

        BTPaymentMethodNonce *selectedNonce = result.paymentMethod;
       [self postNonceToServer:self.selectedNonce.nonce];

    }
}];
[self presentViewController:dropIn animated:YES completion:nil];

答案 2 :(得分:0)

对于新的iOS v4 SDK,我也必须导入Braintree,否则我无法访问paymentMethod的{​​{1}}属性。

BTDropInResult