Integrate hyper SDK to your Swift App using hyperswitch-node
Use this guide to integrate Hyperswitch SDK to your iOS app. You can use the following app as a reference with your Hyperswitch credentials to test the setup. You can also checkout the app on Apple Testflight to test the payment flow.
Requirements
iOS 13.0 and above
CocoaPods
npm
1. Setup the server
1.1 Install the hyperswitch-node library
Install the package and import it in your code
$ npm install @juspay-tech/hyperswitch-node
1.2 Create a payment
Before creating a payment, import the hyperswitch-node dependencies and initialize it with your API key.
Add an endpoint on your server that creates a Payment. Creating a Payment helps to establish the intent of the customer to start a payment. It also helps to track the customer’s payment lifecycle, keeping track of failed payment attempts and ensuring the customer is only charged once. Return the client_secret obtained in the response to securely complete the payment on the client.
// Create a Payment with the order amount and currencyapp.post("/create-payment",async (req, res) => {try {constpaymentIntent=awaithyper.paymentIntents.create({ currency:"USD", amount:100, });// Send publishable key and PaymentIntent details to clientres.send({ clientSecret:paymentIntent.client_secret, }); } catch (err) {returnres.status(400).send({ error: { message:err.message, }, }); }});
2. Build checkout page on your app
2.1 Configure your repository with Hyperswitch dependency
CocoaPods Setup (only required if not already done)
Install the latest version of CocoaPods
To create a Podfile run the following command
pod init
SDK Setup
Add these lines to your Podfile:
#use_frameworks!#target 'YourAPP' do pod 'hyperswitch-sdk-ios'#end
Run the following command:
pod install
Remember that moving forward, you should open your project in Xcode using the .xcworkspace file rather than the .xcodeproj file.
To update to the latest version of the SDK, run:
pod install --repo-update
2.2 Setup the SDK and fetch a Payment
Set up the SDK using your publishable key. This is essential for initializing a PaymentSession.
Request your server to fetch a payment as soon as your view is loaded. Store the client_secret returned by your server. The PaymentSession will use this secret to complete the payment process.
var paymentSession: PaymentSession?paymentSession?.initPaymentSession(paymentIntentClientSecret: paymentIntentClientSecret)
@ObservedObjectvar model =BackendModel()@Publishedvar paymentSheet: PaymentSession?@Publishedvar paymentResult: PaymentSheetResult?// handle resultfunconPaymentCompletion(result: PaymentSheetResult) { DispatchQueue.main.async { self.paymentResult = result }}paymentSession?.initPaymentSession(paymentIntentClientSecret: paymentIntentClientSecret)
Handle Payment Result
Handle the payment result in the completion block and display appropriate messages to your customer based on whether the payment fails with an error or succeeds.
Create a card element view and pay button and handle the payment result in the completion block and display appropriate messages to your customer based on whether the payment fails with an error or succeeds.