Create Token
iOS
如欲了解最新版號 SDK 以及各版號之間的差異性,請參考 Github Release Page : TapPay iOS Github
Overview
- 透過前端呼叫 getPrime 取得 Prime , 把 Prime 送往後端伺服器
- 從您的後端伺服器將 Prime , frontend_redirect_url , backend_notify_url 送到 TapPay
- 您的後端取得 payment_url 送往前端使用 TPDLinePay.redirectWithUrl , 讓使用者使用 LINE Pay 付款
- 付款完成, 透過您設定的 Return URL 回到您的 App , 後端透過 backend_notify_url 路由中收到 TapPay 通知
- 為提高偽卡交易的準確度, 若可以請開啟IDFA功能
TPDSetup
+ (instancetype _Nonnull)setWithAppId:(int)appId
withAppKey:(NSString *_Nonnull)appKey
withServerType:(TPDServerType)serverType;
class func setWithAppKey(_ appKey: String, withAppId appId: Int32, with serverType: TPDServerType) -> Self
名稱 | 類別 | 內容 |
---|---|---|
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
}
2 .點選左邊您的專案後,在 General -> Linked Frameworks and Libraries 加入 SafariServices.framework
3 .為提高偽卡交易的準確度,若可以請開啟IDFA功能
Setup URL Scheme
自定義您的 App Scheme
打開 Xcode info.plist file.
新增屬性 , 名稱設定為 URL types
在 URL types , item 0 新增屬性 , 名稱設定為 URL identifier , 自定義的 URL Scheme 名字
在 URL types , item 0 新增屬性 , 名稱設定為 URL Scheme , 並自定義您的 App Scheme
打開 Xcode info.plist file.
新增屬性 , 名稱設定為 LSApplicationQueriesSchemes , 並將類別設為 Array
新增一個 item , 將類別設為 String , 值設定為 line
isLinePayAvailable
您可以使用此方法確認裝置是否可以使用 LINE App
[TPDLinePay isLinePayAvailable];
TPDLinePay.isLinePayAvailable()
Install LINE App
使用此方法開啟 App Store 安裝 LINE App 頁面
[TPDLinePay installLineApp];
TPDLinePay.installLineApp()
Setup TPDLine Pay
使用此方法帶入您自定義的 URL Scheme 初始化 TPDLinePay 物件
Return URL 格式 : “Your Custom URL Scheme://Your Custom path”
TPDLinePay *linePay = [TPDLinePay setupWithReturnUrl:@"Your Custom URL Scheme://Your Custom path"];
let linePay = TPDLinePay.setup(withReturnUrl: "Your Custom URL Scheme://Your Custom path")
Get Prime
呼叫 getPrime 函式 , 透過 onSuccessCallback . onFailureCallback 取得 Prime 或錯誤訊息 , 將 Prime 送至您的後端伺服器並呼叫 Pay By Prime API.
[[[linePay onSuccessCallback:^(NSString * _Nullable prime) {
NSLog(@"Prime : %@",prime);
}]onFailureCallback:^(NSInteger status, NSString * _Nonnull message) {
NSLog(@"status : %ld , message : %@", status, message);
}] getPrime];
linePay.onSuccessCallback { (Prime) in
print("Prime : \(Prime!)")
}.onFailureCallback { (status, msg) in
print("status : \(status), msg : \(msg)")
}.getPrime()
Redirect to LINE Pay Payment Page
將 Pay By Prime 成功後取得的 payment_url 帶入
畫面將導轉至 LINE Pay 付款畫面,您可以透過 callback 取得交易結果
[linePay redirectWith:paymentUrl ViewController:self completion:^(TPDLinePayResult * _Nonnull result) {
NSLog(@"status : %@ , orderNumber : %@ , recTradeId : %@ , bankTransactionId : %@",result.status , result.orderNumber , result.recTradeId , result.bankTransactionId);
}];
linePay.redirect(with: paymentURL, viewController: self, completion: { (result) in
print("stauts : \(result.status) , recTradeId : \(result.recTradeId) , bankTransactionId : \(result.bankTransactionId) , order_number : \(result.orderNumber)")
})
Handle URL
在 AppDelegate openURL 中實做 handleURL 使用此方法來判斷 URL 來自 TapPay
(iOS 9.0 以上請使用此方法)
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
BOOL tapPayHandled = [TPDLinePay handleURL:url];
if (tapPayHandled){
return YES;
}
return NO;
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let tapPayHandled = TPDLinePay.handle(url)
if (tapPayHandled) {
return true
}
return false
}
(iOS 9.0 以下請使用此方法)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
BOOL tapPayHandled = [TPDLinePay handleURL:url];
if (tapPayHandled) {
return YES;
}
return NO;
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
let tapPayHandled = TPDLinePay.handle(url)
if (tapPayHandled) {
return true
}
return false
}
Exception Handle
1. 使用此方法監聽是否有意外狀況發生 , 並在AppDelegate didFinishLaunchingWithOptions 中實作 tappayLinePayExceptionHandler 函式
[TPDLinePay addExceptionObserver:(@selector(tappayLinePayExceptionHandler:))];
TPDLinePay.addExceptionObserver(#selector(tappayLinePayExceptionHandler(notofication:)))
2. 在 AppDelegate 加入 tappayLinePayExceptionHandler 函式 , 發生意外狀況時接收通知 , 並實作 parseURL 取得交易結果
- (void)tappayLinePayExceptionHandler:(NSNotification *)notification {
TPDLinePayResult * result = [TPDLinePay parseURL:notification];
NSLog(@"status : %@ , orderNumber : %@ , recTradeId : %@ , bankTransactionId : %@",result.status , result.orderNumber , result.recTradeId , result.bankTransactionId);
}
@objc func tappayLinePayExceptionHandler(notofication: Notification) {
let result : TPDLinePayResult = TPDLinePay.parseURL(notofication)
print("status : \(result.status) , orderNumber : \(result.orderNumber) , recTradeid : \(result.recTradeId) , bankTransactionId : \(result.bankTransactionId) ")
}