Initiate Payment
When a user selects a payment method and confirms their intent to pay, the transaction process is initiated from your backend. This is achieved by making a server-to-server call to the Create Order API. The API responds with a unique Order Token and Merchant OrderID, which are essential credentials required to securely initialize the payment SDK for that specific transaction.
public func startTransaction(request: PhonePePayment.B2BPGTransactionRequest, on viewController: UIViewController, completion: @escaping PhonePePayment.PPTransactionCompletion)| Parameter Name | Type | Description |
|---|---|---|
request | B2BPGTransactionRequest | The object to which you should pass merchantId, orderId, token, appSchema, paymentMode. |
viewController | UIViewController | The UIView controller instance that you want to present the checkout page. |
The B2BPGTransactionRequest is an object that must be passed to the SDK. The SDK processes this request and returns a response once the payment is completed. The completion handler provides the state of the payment, after which the Status API should be called to confirm the final transaction status.
In B2BPGTransactionRequest, We need 5 parameters to init the object.
public struct B2BPGTransactionRequest {
public init(merchantId: String, orderId: String,
token: String,
appSchema: String,
paymentMode: PhonePePayment.PaymentMode)
}| Parameter Name | Type | Description |
|---|---|---|
merchantId | String | The merchant Id provided by PhonePe |
orderId | String | The unique Merchant Order ID |
token | String | The order token received in the backend from Create Order API call. |
appSchema | String | Your App schema created under the URL Schema of info.plist file. This helps to redirect back to the merchant app after the user has completed the payment. |
paymentMode | Enum | Pass the details of the Payment mode selected by the user. |
The Payment mode should be of ppeIntent type.
public init(type withConstraint: String?, id: String? = nil)The detail Payment mode code syntax looks like below
PaymentMode.ppeIntent(request: PPEIntentPaymentMode(type: selectedInstrument.type, id: selectedInstrument.id))| Parameter Name | Type | Description |
|---|---|---|
id | String | The respective Id of the instrument selected by the user on the checkout page. |
type | String | The respective type of the instrument selected by the user on the checkout page |
Example
let paymentMode = PaymentMode.ppeIntent(request: PPEIntentPaymentMode(type: selectedInstrument.type, id: selectedInstrument.id))
let request = B2BPGTransactionRequest(merchantId: "MERCHANT-ID",
orderId: "ORDER-ID",
token: "ORDER_TOKEN",
appSchema: "YOUR-APP-SCHEMA",
paymentMode: paymentMode)
ppPayment.startTransaction(request: request, on: vc) { _, state in
print(state)
}After the startTransaction method is called, the SDK handles all payment flows and returns a callback upon completion. Once the callback is received, call the Order Status API to retrieve the final status of the payment.
What’s Next?
In the next section, you will learn how to fetch the current status of an order. This is a step to confirm whether a payment is successful, pending, or failed, ensuring the final transaction status is updated accurately in your application.