iOS

1.After the payment is completed, redirect to the webpage for 3D Secure authentication

After confirming the returned data after the payment contains the nextAction object and the web redirection addressredirectToUrl is not empty, the web page will be redirected to 3D Secure authentication.

[[MCAPIClient shared] confirmPaymentWithParams:params completionBlock:^(NSDictionary * _Nullable responseObject, NSError * _Nullable error) {
                
                    if (!error) { // Request succeeded
                        
                        // Determine whether to trigger 3d secure authentication 
                        NSDictionary *nextAction = [[responseObject objectForKey:@"data"] objectForKey:@"nextAction"];
                        
                        if (nextAction) { // If this object exists, 3D secure authentication  is required
                            
                            NSString *redirectToUrl = [nextAction objectForKey:@"redirectToUrl"];
                            NSString *paymentID = [[responseObject objectForKey:@"data"] objectForKey:@"id"];
                            NSString *clientSecret = [[responseObject objectForKey:@"data"] objectForKey:@"clientSecret"];
                            
                            // Load 3Ds page
                            MCThreeDSWebViewController *threeDSVC = [[MCThreeDSWebViewController alloc] init];
                            [threeDSVC loadURL:redirectToUrl paymentID:paymentID clientSecret:clientSecret];
                    
                            // 3D page request completion callback  msg:status responseObject:(retrievePayment)检索支付接口后台返回数据
                            threeDSVC.completeBlock = ^(NSString *msg, NSDictionary *responseObject){
                                
                                // Eliminate animation
                                [weakSelf requestCompleted:msg];
                                weakSelf.responseObject = responseObject;
                                
                            };
        
                            [weakSelf presentViewController:threeDSVC animated:YES completion:nil];
                            
                        }else { // Ordinary transaction
                            
                            //  transaction status
                            NSString *status = [[responseObject objectForKey:@"data"] objectForKey:@"status"];
                            // Prompt
                            NSString *msg = @"";
                            
                            if ([status isEqualToString:@"succeeded"]) {
                                
                                msg = @"success";
                                weakSelf.responseObject = responseObject;
                                
                            }else if ([status isEqualToString:@"failed"]) {
                                
                                msg = [[responseObject objectForKey:@"data"] objectForKey:@"errorMessage"];
                                
                            }else if ([status isEqualToString:@"pending"]) {
                                
                                msg = @"the payment is pending";
                                
                            }else if ([status isEqualToString:@"uncaptured"]) {
                                
                                msg = @"the payment is uncaptured";
                                
                            }else if ([status isEqualToString:@"canceled"]) {
                                
                                msg = @"the payment is canceled";
                                
                            }else {
                                
                                msg = @"the payment is pending";
                                
                            }
                            
                            // Eliminate animation
                             [weakSelf requestCompleted:msg];
                            
                        }
                    
                        NSLog(@"Charge confirmation:%@",responseObject);
                        
                    }else { // Request failed
                        
                        // Eliminate animation
                        [weakSelf requestCompleted:error.localizedDescription];
                        
                        NSLog(@"failed to confirm charge, code = %ld error message = %@",(long)error.code,error.localizedDescription);
                    }
                                
                }];

2.3D Secure authentication on web

After directing to the webpage, please check the current url address firstly. If the authenticated rules are met, retrievepayment interface will be called and the payment result will be returned to the customer.

If you click Return button during the verification process, an Exit prompt box will be displayed. Clicking to exit will call the retrievePayment interface, and then return the payment result to the customer. For details, please refer to the MCThreeDSWebViewController class under the MoneyCollect Example.

// Retrieve whether the url contains these 3 fields
- (void)getStringFromH5QuitWithURLStr:(NSString *)urlStr
{
    if ([urlStr rangeOfString:@"payment_id"].location != NSNotFound && [urlStr rangeOfString:@"payment_client_secret"].location != NSNotFound && [urlStr rangeOfString:@"source_redirect_slug"].location != NSNotFound) {  // 包含
        
        [self retrievePayment];
        
    }else { // Does not contain
        
    }
    
}

// Retrieve payment
- (void)retrievePayment
{
    __weak typeof(self) weakSelf = self;
    // Retrieve
    [[MCAPIClient shared] retrievePayment:_paymentID clientSecret:_clientSecret completionBlock:^(NSDictionary * _Nullable responseObject, NSError * _Nullable error) {
                
        // Prompt
        NSString *msg = @"";
        
        if (!error) {
            
            NSString *code = [responseObject objectForKey:@"code"];
            
            if ([code isEqualToString:@"success"]) {
                
                // Transaction status
                 NSString *status = [[responseObject objectForKey:@"data"] objectForKey:@"status"];
                
                if ([status isEqualToString:@"succeeded"]) {
                    
                    msg = @"success";
                    
                }else if ([status isEqualToString:@"failed"]) {
                    
                    msg = [[responseObject objectForKey:@"data"] objectForKey:@"errorMessage"];
                    
                }else if ([status isEqualToString:@"pending"]) {
                    
                    msg = @"the payment is pending";
                    
                }else if ([status isEqualToString:@"uncaptured"]) {
                    
                    msg = @"the payment is uncaptured";
                    
                }else if ([status isEqualToString:@"canceled"]) {
                    
                    msg = @"the payment is canceled";
                    
                }else {
                    
                    msg = @"the payment is pending";
                    
                }
                
            }else {
                
                msg = [responseObject objectForKey:@"msg"];
            }
            
        }else {
            msg = error.localizedDescription;
        }
         
        // Pass parameter
        if (weakSelf.completeBlock) {
            weakSelf.completeBlock(msg, responseObject);
        }
        
        [weakSelf.presentingViewController dismissViewControllerAnimated:YES completion:nil];
        
    }];
}

3.Get the payment result

/** The returned result of 3D Secure authentication completion */
@property (nonatomic, copy) void(^completeBlock)(NSString *msg, NSDictionary *responseObject);

Note:msg: prompt  responseObject: The data returned from server, please see <retrievePayment> Method above for analysis👆🏻

Last updated