Create Token
iOS
如欲了解最新版號 SDK 以及各版號之間的差異性,請參考 Github Release Page : TapPay iOS Github
Overview
- 透過前端呼叫 getPrime 取得 Prime , 把 Prime 送往後端伺服器
- 從您的後端伺服器將 Prime, frontend_redirect_url, backend_notify_url 送到 TapPay
- 您的後端取得 payment_url 送往前端使用 TPDPiWallet.redirect 開啟 Pi 錢包 App 付款
- 付款完成後,透過您設定的 universal link 回到您的 App,後端透過 backend_notify_url 路由中收到 TapPay 通知
名稱 | 類別 | 內容 |
---|---|---|
appID | int | 請參考 appid |
appKey | String | 請參考 appkey |
serverType | TPDServerType | 使用的伺服器種類 測試時請使用 Sandbox 環境 (TPDServerType.Sandbox) 實體上線後請切換至 Production 環境 (TPDServerType.Production) |
1 .在您的 AppDelegate 中 import TPDirect,並在 didFinishLaunchingWithOptions 中使用 TPDSetup 來設定環境
#import <TPDirect/TPDirect.h>
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[TPDSetup setWithAppId:AppId withAppKey:@"AppKey" withServerType:TPDServer_SandBox];
return YES;
}
import TPDirect
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
TPDSetup.setWithAppId(AppId, withAppKey: AppKey, with: TPDServerType.sandBox)
return true
}
isPiWalletAvailable
判斷是否有安裝 Pi 錢包 app
打開 Xcode info.plist file.
新增屬性 , 名稱設定為 LSApplicationQueriesSchemes , 並將類別設為 Array
新增一個 item , 將類別設為 String , 值設定為 pi
[TPDPiWallet isPiWalletAvailable];
TPDPiWallet.isPiWalletAvailable()
Setup universal link
設定 universal link
1. 打開 xCode TARGETS, Signing & Capabilites 頁面,按 + Capability 按鈕
2. 選擇 Associated Domains
3. 在 Associated Domains 按 + 號,新增您的 domain
4. Domain 的格式為 applinks:{your domain} (不用加上 https://)
5. 在您的 server 新增一個路徑 /.well-known
新增一個檔案 apple-app-site-association 不用副檔名
appID format : <TeamID>.<bundle identifier>
{
"applinks": {
"apps": [],
"details": [
{
"appID": "T9G64ZZGC4.Cherri.TPDPiWalletExample",
"paths": [ "*" ]
}
]
}
}
取得 teamID 的位置在 apple developer membership
取得 bundle identifier 在 xCode
Setup TPDPiWallet
使用此方法帶入您自定義的 universal link 初始化 TPDPiWallet 物件
TPDPiWallet * piWallet = [TPDPiWallet setupWithReturnUrl:@"URL"];
let piWallet = TPDPiWallet.setup(withReturnUrl: "URL")
Get Prime
呼叫 getPrime 函式 , 透過 onSuccessCallback . onFailureCallback 取得 Prime 或錯誤訊息 , 將 Prime 送至您的後端伺服器並呼叫 Pay By Prime API.
[[[piWallet onSuccessCallback:^(NSString * _Nullable prime) {
NSLog(@"Prime : %@",prime);
}] onFailureCallback:^(NSInteger status, NSString * _Nonnull message) {
NSLog(@"status : %ld , message : %@", status, message);
}] getPrime];
piWallet.onSuccessCallback { (Prime) in
print("Prime : \(Prime!)")
}.onFailureCallback { (status, msg) in
print("status : \(status), msg : \(msg)")
}.getPrime()
Redirect to Pi Wallet App
將 Pay By Prime 成功後取得的 payment_url 帶入
畫面將導轉至 Pi 錢包 付款畫面,您可以透過 callback 取得交易結果
[piWallet redirect:paymentUrl completion:^(TPDPiWalletResult * _Nonnull result) {
NSString *piWalletResult = [NSString stringWithFormat:@"status : %ld \n recTradeId : %@ \n bankTransactionId : %@ \n orederNumber : %@", (long)result.status, result.recTradeId, result.bankTransactionId, result.orderNumber];
NSLog(@"result : %@", piWalletResult);
}];
piWallet.redirect(payment_url) { (result) in
print("status : \(result.status), rec_trade_id : \(result.recTradeId), order_number : \(result.orderNumber), bank_transaction_id : \(result.bankTransactionId)")
}
Handle handlePiWalletUniversalLink
在 AppDelegate continueUserActivity 中實做 handlePiWalletUniversalLink 使用此方法來判斷 URL 來自 TapPay (iOS 13.0 以下請使用此方法)
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
NSURL * url = userActivity.webpageURL;
BOOL piWalletHandled = [TPDPiWallet handleUniversalLink:url];
if (piWalletHandled) {
return YES;
}
return NO;
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
let url = userActivity.webpageURL
let piWalletHandled = TPDPiWallet.handleUniversalLink(url)
if (piWalletHandled) {
return true
}
return false
}
Exception Handle
1. 使用此方法監聽是否有意外狀況發生 , 並在AppDelegate didFinishLaunchingWithOptions 中實作 tappayPiWalletExceptionHandler 函式
[TPDPiWallet addExceptionObserver:(@selector(tappayPiWalletExceptionHandler:))];
TPDPiWallet.addExceptionObserver(#selector(tappayPiWalletExceptionHandler(notofication:)))
2. 在 AppDelegate 加入 tappayPiWalletExceptionHandler 函式 , 發生意外狀況時接收通知 , 並實作 parseURL 取得交易結果
- (void)tappayPiWalletExceptionHandler:(NSNotification *)notification {
TPDPiWalletResult * result = [TPDPiWallet parseURL:notification];
NSLog(@"status : %@ , orderNumber : %@ , recTradeId : %@ , bankTransactionId : %@",result.status , result.orderNumber , result.recTradeId , result.bankTransactionId);
}
@objc func tappayPiWalletExceptionHandler(notofication: Notification) {
let result : TPDPiWalletResult = TPDPiWallet.parseURL(notofication)
print("status : \(result.status) , orderNumber : \(result.orderNumber) , recTradeid : \(result.recTradeId) , bankTransactionId : \(result.bankTransactionId) ")
}
Test Method in sandbox
請聯絡 TapPay Support
Email : Support@cherri.tech