Collecting Card Details on iOS

    Learn how to build a Stripe integration for iOS that uses Charges and tokenization.

    Accepting a card payment with Charges and the iOS SDK is a three-step process, with server-side and client-side actions.

    1. Securely collect payment information
    2. Tokenize the card information
    3. Use the token to create a charge on your server

    Step 1: Securely collect payment information

    Assemble an STPCardParams instance with your customer’s card information using the iOS SDK’s pre-built STPPaymentCardTextField or your own UI. If you’re using Apple Pay to collect payment information, skip to the next step.

    let cardParams = STPCardParams()
    cardParams.number = myPaymentCardTextField.cardNumber
    cardParams.expMonth = myPaymentCardTextField.expirationMonth
    cardParams.expYear = myPaymentCardTextField.expirationYear
    cardParams.cvc = myPaymentCardTextField.cvc
    STPCardParams *cardParams = [STPCardParams new];
    cardParams.number = myPaymentCardTextField.cardNumber;
    cardParams.expMonth = myPaymentCardTextField.expirationMonth;
    cardParams.expYear = myPaymentCardTextField.expirationYear;
    cardParams.cvc = myPaymentCardTextField.cvc;

    Step 2: Tokenize the card information

    The next step is sending the customer’s payment information to Stripe and exchanging it for a token, which you can send to your server and use to create a charge. Tokenization ensures that no sensitive card data ever needs to touch your server, so that your integration remains PCI-compliant. Use the STPAPIClient createTokenWithCard:completion: method to convert the STPCardParams instance into an STPToken instance which you can then pass to your server.

    STPAPIClient.shared().createToken(withCard: cardParams) { token, error in
        guard let token = token else {
            // Handle the error
            return
        }
        // Use the token in the next step
    }
    [[STPAPIClient shared] createTokenWithCard:cardParams completion:^(STPToken * _Nullable token, NSError * _Nullable error) {
        if (token == nil) {
            // Handle the error
            return;
        }
        // Use the token in the next step
    }];

    If you’re using Apple Pay, call the STPAPIClient createTokenWithPayment:completion: method with the PKPayment instead.

    Step 3: Use the token to create a charge on your server

    On your server, use the token to create a charge.

    curl https://api.stripe.com/v1/charges \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d amount=999 \
      -d currency=usd \
      -d description="Example charge" \
      -d source=tok_visa
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    # Token is created using Checkout or Elements!
    # Get the payment token ID submitted by the form:
    token = params[:stripeToken]
    charge = Stripe::Charge.create({
        amount: 999,
        currency: 'usd',
        description: 'Example charge',
        source: token,
    })
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    # Token is created using Checkout or Elements!
    # Get the payment token ID submitted by the form:
    token = request.form['stripeToken'] # Using Flask
    charge = stripe.Charge.create(
        amount=999,
        currency='usd',
        description='Example charge',
        source=token,
    )
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    // Token is created using Checkout or Elements!
    // Get the payment token ID submitted by the form:
    $token = $_POST['stripeToken'];
    $charge = \Stripe\Charge::create([
        'amount' => 999,
        'currency' => 'usd',
        'description' => 'Example charge',
        'source' => $token,
    ]);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    // Token is created using Checkout or Elements!
    // Get the payment token ID submitted by the form:
    String token = request.getParameter("stripeToken");
    Map<String, Object> params = new HashMap<>();
    params.put("amount", 999);
    params.put("currency", "usd");
    params.put("description", "Example charge");
    params.put("source", token);
    Charge charge = Charge.create(params);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    // Token is created using Checkout or Elements!
    // Get the payment token ID submitted by the form:
    const token = request.body.stripeToken; // Using Express
    (async () => {
      const charge = await stripe.charges.create({
        amount: 999,
        currency: 'usd',
        description: 'Example charge',
        source: token,
      });
    })();
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    // Token is created using Checkout or Elements!
    // Get the payment token ID submitted by the form:
    token := r.FormValue("stripeToken")
    params := &stripe.ChargeParams{
        Amount: stripe.Int64(999),
        Currency: stripe.String(string(stripe.CurrencyUSD)),
        Description: stripe.String("Example charge"),
    }
    params.SetSource(token)
    ch, _ := charge.New(params)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    // Token is created using Checkout or Elements!
    // Get the payment token submitted by the form:
    var token = model.Token; // Using ASP.NET MVC
    var options = new ChargeCreateOptions {
        Amount = 999,
        Currency = "usd",
        Description = "Example charge",
        Source = token,
    };
    var service = new ChargeService();
    Charge charge = service.Create(options);
    

    Next steps

    Now you have a complete iOS integration built with the Charges API and tokenization. Next, create a charge.

    Was this page helpful? Yes No

    Send

    Thank you for helping improve Stripe's documentation. If you need help or have any questions, please consider contacting support.

    Questions?

    We're always happy to help with code or other questions you might have. Search our documentation, contact support, or connect with our sales team. You can also chat live with other developers in #stripe on freenode.

    On this page