# iOS

{% tabs %}
{% tab title="Prebuilt checkout page" %}

### **1.Create a Customer**

Pass a customerId to MoneyCollect to attach to the customer.

```
   // Use your customerId 
   var customerId = "cus_1452880617225281538"
```

### **2.**&#x43;onstruct dat&#x61;**,** and pay in PaymentSheet model

When creating a payment method, you can specify customer parameters and automatically attach the payment method to an existing customer.

```
  /** Merchant ID */
   @property (nonatomic,copy) NSString *customerID;
   /** Billing details */
   @property (nonatomic,strong) MCBillingDetails *billingDetails;
   /** Create payment parameter object */
   @property (nonatomic,strong) MCCreatePaymentParams *createPaymentParams;


    // Initiate controller
    MCSelectPaymentCardVC *selectPaymentCardVC = [[MCSelectPaymentCardVC alloc] init];
    selectPaymentCardVC.delegate = self;

    // Build request parameters
    selectPaymentCardVC.customerID = [MCConfigurationFile getCustomerID];
    selectPaymentCardVC.billingDetails = [MCConfigurationFile getBillingDetailsModel];
    selectPaymentCardVC.createPaymentParams = [self constructMCCreatePaymentParams];
    
    // Display view
    [selectPaymentCardVC present:self];
```

{% hint style="info" %}
The details of using constructMCCreatePaymentParams method to construct data, please  refer to MCPaymentSheetCheckoutVC class under  Example.
{% endhint %}

### 3.Start Hosted Payment Page mode

* Tap “Checkout” button to pay in <mark style="color:green;">Hosted Payment Page</mark> mode.&#x20;
* In Hosted payment page，<mark style="color:purple;">select the previously saved card</mark> or <mark style="color:purple;">add a new card</mark>.&#x20;
* Finally, tap the PayNow button to complete the payment.

### **4.Customer selects "save this card" during payment**

* **4.1 Select "Save this card for your future use" on the Add a card page.**

```
NSString *setupFutureUsage = isSelected ? @"on" : @"off";
_createPaymentParams.setupFutureUsage = setupFutureUsage;
```

Select the payment card in the future, when using setupFutureUsage set to "on", otherwise to "off"

* **4.2 Click "Add a card.**
* **4.3 After create a payment method and attach the payment method to customer will save the card details to customer.**

### **5.**&#x52;eturn the payment result

```
@protocol MCSelectPaymentCardVCDelegate <NSObject>
/** Agent method for successful transaction */
- (void)successfulTransactionWithResponseObject:(NSDictionary *_Nullable)responseObject;
@end;

#pragma mark - MCSelectPaymentCardVCDelegate
- (void)successfulTransactionWithResponseObject:(NSDictionary *)responseObject
{
    // Transaction status
    NSString *status = [[responseObject objectForKey:@"data"] objectForKey:@"status"];
    
    if ([status isEqualToString:@"succeeded"]) {
        
        NSLog(@"success");
        
    }else if ([status isEqualToString:@"failed"]) {
        
        NSLog(@"%@",[[responseObject objectForKey:@"data"] objectForKey:@"errorMessage"]);
        
    }else if ([status isEqualToString:@"pending"]) {
        
        NSLog(@"pending");
        
    }else if ([status isEqualToString:@"uncaptured"]) {
        
        NSLog(@"uncaptured");
        
    }else if ([status isEqualToString:@"canceled"]) {
        
        NSLog(@"canceled");
        
    }else {
        
        NSLog(@"pending");
        
    }
}
```

{% endtab %}

{% tab title="Custom payment flow" %}

#### **1.Create a Customer**

Pass a <mark style="color:blue;">`customerId`</mark> to MoneyCollect to attach to the customer.

```
   // Use your customerId 
   var customerId = "cus_1452880617225281538"
```

#### **2.**&#x43;onstruct data, and add payment methods with PaymentSheet customizable models

When creating a payment method, you can specify `customer parameters` to automatically attach the payment method to an existing customer.

```
#pragma mark - Button Click Event
- (void)selectCardButtonClick:(UIButton *)sender
{
    // Initiate objective
    MCAddCardViewController *addCardVC = [[MCAddCardViewController alloc] init];
    addCardVC.delegate = self;
    
    // Construct parameter
    addCardVC.customerID = [MCConfigurationFile getCustomerID];
    
    //Display view
    [addCardVC present:self];
    
}
```

#### **3.Customer selects&#x20;**<mark style="color:purple;">**`save this card`**</mark>**&#x20;during payment**

![](/files/Sw54xVq1lUklf8iE712f)

* 3.1 Select <mark style="color:purple;">`Save this card for your future use`</mark> on the Add a card page.

```
   if (isSelected) { // Save card details
    [[MCAPIClient shared] attachPaymentMethodToCustomer:[MCConfigurationFile getCustomerID] paymentMethod:ID completionBlock:^(NSDictionary * _Nullable responseObject, NSError * _Nullable error) {
       if (!error) { // Request succeeded
          NSLog(@"Save card(Add a card,Add payment methods):%@",responseObject);
       }else {
          NSLog(@"failed to save this card,code = %ld error message = %@",error.code,error.localizedDescription);
       }
      }];
     }
```

Select the payment card in the future, when setupFutureUsage set to <mark style="color:purple;">`on`</mark>, otherwise it will set to <mark style="color:purple;">`off`</mark>.

* 3.2 Click <mark style="color:purple;">`Add a card`</mark>.
* 3.3 After create a payment method and attach the payment method to customer will save the card details to customer.

#### **4.Customer selects the card to make payment**

![](/files/o5d42crbpFLkX7T3ej2j)

Click on the <mark style="color:purple;">`Pay Now`</mark> button to complete the transaction.

#### **5.**&#x52;eturn the payment result

The payment result is obtained by calling the <mark style="color:blue;">`confirmPayment`</mark> API for payment confirmation, and then the result data is passed to the component.

```
  [[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, will trigger 3D Secure authentication
                            
                            NSString *redirectToUrl = [nextAction objectForKey:@"redirectToUrl"];
                            NSString *paymentID = [[responseObject objectForKey:@"data"] objectForKey:@"id"];
                            NSString *clientSecret = [[responseObject objectForKey:@"data"] objectForKey:@"clientSecret"];
                            
                            // Load 3D secure page
                            MCThreeDSWebViewController *threeDSVC = [[MCThreeDSWebViewController alloc] init];
                            [threeDSVC loadURL:redirectToUrl paymentID:paymentID clientSecret:clientSecret];
                            
                            threeDSVC.completeBlock = ^(NSString *msg, NSDictionary *responseObject){
                                
                                // Eliminate animation
                                [weakSelf requestCompletedMsg:msg];
                                
                            };
                            
                            [weakSelf presentViewController:threeDSVC animated:YES completion:nil];
                            
                        }else { // Ordinary transaction
                            
                            // Transaction status
                            NSString *status = [[responseObject objectForKey:@"data"] objectForKey:@"status"];
                            // Hint
                            NSString *msg = @"";
                            
                            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";
                                
                            }
                            
                            // Eliminate animation
                            [weakSelf requestCompletedMsg:msg];
                        }
                    
                        NSLog(@"Confirm charge:%@",responseObject);
                        
                    }else { // Request failed
                        
                        // Eliminate animation
                        [weakSelf requestCompletedMsg:error.localizedDescription];
                        
                        NSLog(@"failed to confirm charge,code = %ld Error message = %@",error.code,error.localizedDescription);
                    }
                                
                }];


#pragma mark - End
- (void)requestCompletedMsg:(NSString *)msg
{
    _bottomPayNow.msg = msg;
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.bottomView.y -= weakSelf.bottomPayNow.textH;
        weakSelf.bottomView.height += weakSelf.bottomPayNow.textH;
    }];
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.moneycollect.com/docs/payment/save-a-card-during-payment/ios.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
