Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+createPayment:viewController:appURLScheme:withCompletion: 的 completion 不會被執行 #16

Open
xareelee opened this issue Jul 30, 2015 · 0 comments

Comments

@xareelee
Copy link

Using Pingpp Version 2.0.6

在進行支付時,+createPayment:viewController:appURLScheme:withCompletion: 中 completion block 在支付結束跳轉回 App 時不會被執行。

[Pingpp createPayment:charge viewController:self appURLScheme:APP_URL_SCHEME withCompletion:^(NSString *result, PingppError *error) {
  // ======== 此 block 稱為  Block A ========
  // !!!: 若在 -application:openURL:sourceApplication:annotation: 內實作
  // `+[Pingpp handleOpenURL:withCompletion:]` 且 completion 有值的話,
  // 則永遠不會跳到此段 comletion block。
  //
  // 若要跳到此段 block 則 +handleOpenURL:withCompletion: 的 completion 必須
  // 為 nil。

  if (error) {
    // Handle error
  } else if ([result isEqualToString:@"success"]) {
    NSLog(@"支付成功!");
    // Do something    
  }

}];

發現原因是原本要在 -application:openURL:sourceApplication:annotation: 內實作的 +[Pingpp handleOpenURL:withCompletion:] 所傳入的 completion,會覆蓋掉原本 +createPayment:viewController:appURLScheme:withCompletion: 的 completion。

唯有當 +[Pingpp handleOpenURL:withCompletion:] 的 completion 的值為 nil 時,才會回去執行原本 +createPayment:viewController:appURLScheme:withCompletion: 的 completion block。

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
  // (I) 以下這段會覆蓋掉原本的 block A
  BOOL handleURL = [Pingpp handleOpenURL:url withCompletion:^(NSString *result, PingppError *error) {
    // 支付完後會執行本 block,而不會再執行 Block A
  }];


  // (II) 若傳入 nil 則會讓 Block A  被執行
  BOOL handleURL = [Pingpp handleOpenURL:url withCompletion:nil];
}

我認為這邊的設計有問題,根據 +handleOpenURL:withCompletion: 的文件說明:

@param completionBlock 支付结果回调 Block,保证跳转支付过程中,当 app 被 kill 掉时,能通过这个接口得到支付结果

換句話說,該 completion 應該是只有先前的 completion block (Block A) 不存在時,才執行 openURL 的 completion,進行 deep-link-like 的模式跳轉頁面 (以下為 pseudo-code):

// 目前的作法 
if (openURLCompletionBlock) {
  openURLCompletionBlock(result, error);
} else if (createPaymentCompletionBlock) {
  createPaymentCompletionBlock(result, error);
}

// 應該的作法:把 block 調用順序反過來
if (createPaymentCompletionBlock) {
  createPaymentCompletionBlock(result, error);
} else if (openURLCompletionBlock) {
  openURLCompletionBlock(result, error);
} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant