The PayPal Web Payments module in the PayPal SDK enables PayPal payments via a webview in your app.
Follow these steps to add PayPal Web 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 to capture funds using the 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 PayPalWebPayments
checkbox to add the PayPal Web Payments library to your app.
Include the PayPalWebPayments
sub-module in your Podfile
:
pod 'PayPal/PayPalWebPayments'
Create a CoreConfig
using an client id:
let config = CoreConfig(clientID: "<CLIENT_ID>", environment: .sandbox)
Create a PayPalWebCheckoutClient
to approve an order with a PayPal payment method:
let payPalClient = PayPalWebCheckoutClient(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.
Configure your PayPalWebCheckoutRequest
and include the order ID generated in step 4:
let payPalRequest = PayPalWebCheckoutRequest(orderID: "<ORDER_ID>")
You can also specify one of the following funding sources for your order: PayPal
(default), PayLater
or PayPalCredit
.
Click here for more information on PayPal Pay Later
To start the PayPal Web Checkout flow, call payPalWebCheckoutClient.start(payPalWebCheckoutRequest)
.
Implement PaypalWebCheckoutDelegate
in your ViewController
to listen for result notifications from the SDK:
extension MyViewController: PayPalWebCheckoutDelegate {
func checkoutWithPayPal() {
payPalClient.delegate = self
payPalClient.start(request: payPalRequest)
}
// MARK: - PayPalWebCheckoutDelegate
func payPal(_ payPalClient: PayPalWebCheckoutClient, didFinishWithResult result: PayPalWebCheckoutResult) {
// order was successfully approved and is ready to be captured/authorized (see step 7)
}
func payPal(_ payPalClient: PayPalWebCheckoutClient, didFinishWithError error: CoreSDKError) {
// handle the error by accessing `error.localizedDescription`
}
func payPalDidCancel(_ payPalClient: PayPalWebCheckoutClient) {
// the user canceled
}
}
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: Basic <ENCODED_CLIENT_ID>' \
--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: Basic <ENCODED_CLIENT_ID>' \
--data-raw ''
Note: Be sure that the endpoint you are calling aligns with the intent set on the order created in step 3.
Follow the Create sandbox account instructions to create a PayPal test account. When prompted to login with PayPal during the payment flow on your mobile app, you can log in with the test account credentials created above to complete the Sandbox payment flow.
Follow these instructions to prepare your integration to go live.