Introduction
Welcome to the AlipaySDK-Mirror documentation! This guide will help you understand and utilize the features of the Alipay SDK for iOS.
Getting Started
Installation
To install the Alipay SDK in your iOS project, follow these steps:
- Open your project in Xcode.
- Navigate to “File” > “Swift Packages” > “Add Package Dependency”.
- Enter the URL for the AlipaySDK-Mirror package:
https://github.com/alipay/alipay-sdk-python
. - Choose the desired version and click “Next”.
- Select the AlipaySDK-Mirror package from the list and click “Add Package”.
Setting Up Your Project
Now that you have installed the Alipay SDK, follow these steps to set it up within your project:
- Open your project’s
AppDelegate.swift
file. - Import the Alipay SDK module using
import AlipaySDK_Mirror
. - In the
func application(_:didFinishLaunchingWithOptions:)
method, add the following code:
“`swift
// Replace `YOUR_ALIPAY_APPID` with your own App ID
AlipaySDK_Mirror.defaultService().alipaySDKDefaultService().setAlipaySDKWithAppId(“YOUR_ALIPAY_APPID”)
“`
Integration Guide
To integrate Alipay SDK features into your iOS app, follow the guide below.
Payment
Alipay SDK provides various methods to initiate payments within your app:
Initialize Payment
To initialize a payment, use the following code snippet:
“`swift
AlipaySDK_Mirror.defaultService().alipaySDKDefaultService().payOrder(“YOUR_ORDER_STRING”) { resultDic in
// Handle payment result
}
“`
Handle Payment Result
You can handle the payment result using the provided callback block in the payOrder(_:)
method. The callback will provide a result dictionary containing the payment status and other relevant information.
“`swift
// Result dictionary keys:
// – “resultStatus”: The payment result status code
// – “result”: The payment result as a string
// – “memo”: Additional information about the payment result
// Example handling of payment result
let resultStatus = resultDic[“resultStatus”] as? String
switch resultStatus {
case “9000”:
print(“Payment successful”)
case “8000”:
print(“Payment processing”)
case “6001”:
print(“Payment cancelled by user”)
case “4000”:
print(“Payment failed”)
default:
print(“Payment result unknown”)
}
“`
Refunds
To process refunds using Alipay SDK, follow these steps:
Initialize Refund
Use the following code snippet to initialize a refund:
“`swift
AlipaySDK_Mirror.defaultService().alipaySDKDefaultService().refundOrder(“YOUR_ORDER_STRING”) { resultDic in
// Handle refund result
}
“`
Handle Refund Result
The refund result can be handled using the provided callback block in the refundOrder(_:)
method. The result dictionary will contain the refund status and other relevant information.
“`swift
// Result dictionary keys:
// – “resultStatus”: The refund result status code
// – “result”: The refund result as a string
// – “memo”: Additional information about the refund result
// Example handling of refund result
let resultStatus = resultDic[“resultStatus”] as? String
switch resultStatus {
case “9000”:
print(“Refund successful”)
case “8000”:
print(“Refund processing”)
case “6001”:
print(“Refund cancelled by user”)
case “4000”:
print(“Refund failed”)
default:
print(“Refund result unknown”)
}
“`
Additional Features
Request User Info
To request user information from Alipay, use the following code:
“`swift
AlipaySDK_Mirror.defaultService().alipaySDKDefaultService().auth_V2(withInfoStr: “YOUR_AUTH_INFO”) { resultDic in
// Handle user info response
}
“`
Handle User Info Response
The user info response can be handled using the provided callback block in the auth_V2(withInfoStr:)
method. The result dictionary will contain the user information and other relevant data.
“`swift
// Example handling of user info response
let resultCode = resultDic[“resultCode”] as? String
switch resultCode {
case “200”:
guard let userInfo = resultDic[“userInfo”] as? [String: Any],
let userId = userInfo[“userId”] as? String,
let nickname = userInfo[“nickName”] as? String else {
print(“User info malformed”)
return
}
print(“User ID: \(userId)”)
print(“Nickname: \(nickname)”)
case “400”:
print(“User info request failed”)
default:
print(“User info result unknown”)
}
“`
Conclusion
Congratulations! You have successfully integrated the Alipay SDK into your iOS app. With this powerful SDK, you can now leverage Alipay’s payment and refund capabilities, as well as request user information. Explore the AlipaySDK-Mirror documentation for more advanced features and options.