The CardPayments module in the PayPal SDK enables Credit and Debit card payments in your app.
Follow these steps to add Card payments:
You will need to set up authorization to use the PayPal Payments SDK. Follow the steps in Get Started to create a client ID.
You will need a server integration to create an order and capture the funds using PayPal Orders v2 API.
In Xcode, add the PayPal SDK as a package dependency to your Xcode project. Enter https://github.com/paypal/paypal-ios as the package URL. Tick the CardPayments
checkbox to add the Card Payments library to your app.
Include the CardPayments
sub-module in your Podfile
:
pod 'PayPal/CardPayments'
Create a CoreConfig
using an client id:
let config = CoreConfig(clientID: "<CLIENT_ID>", environment: .sandbox)
Create a CardClient
to approve an order with a Card payment method:
let cardClient = CardClient(config: config)
When a user initiates a payment flow, call v2/checkout/orders
to create an order and obtain an order ID:
Request
curl --location --request POST 'https://api.sandbox.paypal.com/v2/checkout/orders/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <ACCESS_TOKEN>' \
--data-raw '{
"intent": "<CAPTURE|AUTHORIZE>",
"purchase_units": [
{
"amount": {
"currency_code": "USD",
"value": "5.00"
}
}
]
}'
Response
{
"id":"<ORDER_ID>",
"status":"CREATED"
}
The id
field of the response contains the order ID to pass to your client.
Create a Card
object containing the user's card details.
let card = Card(
number: "4111111111111111",
expirationMonth: "01",
expirationYear: "2025",
securityCode: "123",
cardholderName: "Jane Smith",
billingAddress: Address(
addressLine1: "123 Main St.",
addressLine2: "Apt. 1A",
locality: "city",
region: "IL",
postalCode: "12345",
countryCode: "US"
)
)
Attach the card and the order ID from step 4 to a CardRequest
. Strong Consumer Authentication (SCA) is enabled by default. You can optionally set sca
to .always
if you want to require 3D Secure for every transaction.
let cardRequest = CardRequest(
orderID: "<ORDER_ID>",
card: card,
sca: .scaAlways // default value is .scaWhenRequired
)
Call cardClient.approveOrder()
to approve the order.
Implement CardDelegate
in your ViewController
to listen for result notifications from the SDK:
extension MyViewController: CardDelegate {
func approveOrder(cardRequest: CardRequest) {
cardClient.delegate = self
cardClient.approveOrder(request: cardRequest)
}
func card(_ cardClient: CardClient, didFinishWithResult result: CardResult) {
// order was successfully approved and is ready to be captured/authorized (see step 7)
}
func card(_ cardClient: CardClient, didFinishWithError error: CoreSDKError) {
// handle the error by accessing `error.localizedDescription`
}
func cardDidCancel(_ cardClient: CardClient) {
// 3D Secure auth was canceled by the user
}
func cardThreeDSecureWillLaunch(_ cardClient: CardClient) {
// 3D Secure auth will launch
}
func cardThreeDSecureDidFinish(_ cardClient: CardClient) {
// 3D Secure auth did finish successfully
}
}
If you receive a successful result in the client-side flow, you can then capture or authorize the order.
Call authorize
to place funds on hold:
curl --location --request POST 'https://api.sandbox.paypal.com/v2/checkout/orders/<ORDER_ID>/authorize' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <ACCESS_TOKEN>' \
--data-raw ''
Call capture
to capture funds immediately:
curl --location --request POST 'https://api.sandbox.paypal.com/v2/checkout/orders/<ORDER_ID>/capture' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <ACCESS_TOKEN>' \
--data-raw ''
Follow these instructions to prepare your integration to go live.