此处拿微信支付来举例,支付宝支付同理
添加WXApiDelegate
//不同iOS版本的回调函数不同,但也大同小异,具体不再列举。
- (BOOL)application: (UIApplication * )application openURL: (NSURL * )url sourceApplication: (NSString * )sourceApplication annotation: (id)annotation
if ([WXApi handleOpenURL: url delegate:self]) {
return [WXApi handleOpenURL: url delegate:self];
}
}
只有实现了上述回调才会返回对应的支付结果
-(void) onResp: (BaseResp* )resp
{
NSString *strMsg = [NSString stringWithFormat: @”errcode: %d”, resp.errCode];
NSString *strTitle;
if([resp isKindOfClass: [SendMessageToWXResp class]])
{
strTitle = [NSString stringWithFormat: @"发送媒体消息结果"];
}
if([resp isKindOfClass: [PayResp class]]){
//支付返回结果,实际支付结果需要去微信服务器端查询
strTitle = [NSString stringWithFormat: @"支付结果"];
switch (resp.errCode) {
case WXSuccess: {
strMsg = @"支付结果:成功!";
NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object: @"success"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
default: {
strMsg = [NSString stringWithFormat: @"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object: @"fail"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
}
}
}