创建个单利把第三方支付等的信息整合
.h文件
//爱贝支付的appid(爱贝后台对应app的id)
#define iappayAppid @"xxxxxxx"
#define WXpayAppid @"xxxxxxx"
typedef void(^IPayBlock)(NSString *msg);
#import
#import
#import "WXApi.h"
#import "PayWayModel.h"
#import
typedef void(^AliCallback)(NSDictionary *resultDic);
@interface IPayTool : NSObject
@property (nonatomic, copy)AliCallback aliCallback;
//初始化
+ (IPayTool *)shareInstance;
//设置爱贝支付
-(void)setIAppay;
#pragma mark -- 爱贝支付
-(void)makePayForTransid:(NSString *)transid Success:(IPayBlock)success Faile:(IPayBlock)faile;
#pragma mark -- 微信支付
-(void)makePayByWeiXinWithDict:(NSDictionary *)dict Success:(IPayBlock)success Faile:(IPayBlock)faile;
#pragma mark -- 支付宝支付
-(void)makePayByAlipayWithOrderString:(NSString *)orderString Success:(IPayBlock)success Faile:(IPayBlock)faile;
.m文件
#import "IPayTool.h"
@interface IPayTool ()
@property (nonatomic, copy)IPayBlock paySuccess;
@property (nonatomic, copy)IPayBlock payFaile;
@end
@implementation IPayTool
static IPayTool *instance = nil;
+ (IPayTool *)shareInstance {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[IPayTool alloc] init];
});
return instance;
}
//设置爱贝支付
-(void)setIAppay{
//1.0商户在爱贝注册的应用ID
NSString *mOrderUtilsAppId = iappayAppid;
////渠道号
//NSString *mOrderUtilsChannel = @"";
//设置爱贝SDK的SSO回调(第三方返回使用)
NSString *appUrlScheme = @"com.china.xxxx";
[[IapppayKit sharedInstance] setAppURLScheme:appUrlScheme];
[[IapppayKit sharedInstance] setAppId:mOrderUtilsAppId];
//[[IapppayKit sharedInstance] setChannelId:mOrderUtilsChannel];
//2.0微信支付
[WXApi registerApp:WXpayAppid];
//3.0支付宝
}
#pragma mark -- 爱贝支付
-(void)makePayForTransid:(NSString *)transid Success:(IPayBlock)success Faile:(IPayBlock)faile{
self.paySuccess = success;
self.payFaile = faile;
NSString *appid = iappayAppid;
NSString *transInfo = [NSString stringWithFormat:@"transid=%@&appid=%@",transid,appid];
[[IapppayKit sharedInstance] makePayForTrandInfo:transInfo payResultDelegate:self];
}
#pragma mark -- 微信支付
-(void)makePayByWeiXinWithDict:(NSDictionary *)dict Success:(IPayBlock)success Faile:(IPayBlock)faile{
self.paySuccess = success;
self.payFaile = faile;
//调起微信支付
PayReq* req = [[PayReq alloc] init];
req.partnerId = [dict objectForKey:@"partnerid"];
req.prepayId = [dict objectForKey:@"prepay_id"];
req.nonceStr = [dict objectForKey:@"nonce_str"];
req.package = [dict objectForKey:@"package"];
req.sign = [dict objectForKey:@"sign"];
req.timeStamp = [[dict objectForKey:@"timestamp"] intValue];//时间戳
[WXApi sendReq:req];
}
#pragma mark -- 支付宝支付
-(void)makePayByAlipayWithOrderString:(NSString *)orderString Success:(IPayBlock)success Faile:(IPayBlock)faile{
NSString *appScheme = @"com.china.whty";//支付成功之后返回的APP
//提交给支付宝的订单信息
if (orderString != nil) {
//提交
WEAK_SELF
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {//此处回调是没安装支付宝调用网页的回调
[self aliPayCallBackWithDic:resultDic Success:success Faile:faile];
}];
//支付宝的回调
self.aliCallback = ^(NSDictionary *resultDic) {
[weakSelf aliPayCallBackWithDic:resultDic Success:success Faile:faile];
};
}
}
#pragma mark - 爱贝的回调
- (void)iapppayKitRetPayStatusCode:(IapppayKitPayRetCode)statusCode
resultInfo:(NSDictionary *)resultInfo
{
LOG(@"statusCode : %d, resultInfo : %@", (int)statusCode, resultInfo);
if (statusCode == IapppayKitPayRetSuccessCode)
{
NSString *message = @"支付成功";
if (self.paySuccess) {
self.paySuccess(message);
}
}
else if (statusCode == IapppayKitPayRetFailedCode)
{
//支付失败
NSMutableString *message = [NSMutableString string];
[message appendString:@"支付失败"];
if (resultInfo != nil)
{
id retcode = [resultInfo objectForKey:@"RetCode"];
NSString *errorMsg = [resultInfo objectForKey:@"ErrorMsg"];
[message appendFormat:@":code:%@\n(%@)",retcode,errorMsg];
}
if (self.payFaile) {
self.payFaile(message);
}
}
else
{
//支付取消
NSMutableString *message = [NSMutableString string];
[message appendString:@"支付取消"];
if (resultInfo != nil)
{
id retcode = [resultInfo objectForKey:@"RetCode"];
NSString *errorMsg = [resultInfo objectForKey:@"ErrorMsg"];
[message appendFormat:@":code:%@\n(%@)",retcode,errorMsg];
}
}
}
#pragma mark - 微信支付回调
-(void)onResp:(BaseResp*)resp{
if ([resp isKindOfClass:[PayResp class]]){
PayResp*response=(PayResp*)resp;
switch(response.errCode){
case WXSuccess:
{//服务器端查询支付通知或查询API返回的结果再提示成功
NSString *message = @"微信支付成功";
if (self.paySuccess) {
self.paySuccess(message);
}
}
break;
default:
{
NSDictionary *params = @{@"-1":@"普通错误类型",
@"-2":@"用户点击取消并返回",
@"-3":@"发送失败",
@"-4":@"授权失败",
@"-5":@"微信不支持",};
NSString *key = [NSString stringWithFormat:@"%i",response.errCode];
NSString *errorStr = [NSString stringWithFormat:@"%@",params[key]];
NSString *message = [NSString stringWithFormat:@"微信支付失败:%@",errorStr];
if (self.payFaile) {
self.payFaile(message);
}
}
break;
}
}
}
#pragma mark - 支付宝回调
-(void)aliPayCallBackWithDic:(NSDictionary *)resultDic Success:(IPayBlock)success Faile:(IPayBlock)faile{
if([resultDic[@"resultStatus"] isEqualToString:@"9000"]){//9000:支付成功
NSData *jsonData = [resultDic[@"result"] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *orderDic = [NSJSONSerialization JSONObjectWithData:jsonData options:(NSJSONReadingMutableContainers) error:nil];
NSDictionary * responseDic;
if (orderDic) {
responseDic = orderDic[@"alipay_trade_app_pay_response"];
}
if (responseDic) {
NSString *trade_no = responseDic[@"trade_no"];//支付成功
if (success && trade_no) {
success(trade_no);
}else{
NSString *message = @"支付失败:支付宝订单号生成失败";
if (faile) {
faile(message);
}
}
}
}else{
NSDictionary *params = @{@"8000":@"正在处理中...",
@"4000":@"订单支付失败",
@"5000":@"重复请求",
@"6001":@"用户中途取消",
@"6002":@"网络连接出错",
@"6004":@"支付结果未知",
};
NSString *key = [NSString stringWithFormat:@"%@",resultDic[@"resultStatus"]];
NSString *errorStr = [NSString stringWithFormat:@"%@",params[key]];
if (errorStr == nil) {
errorStr = @"其他支付错误";
}
NSString *message = [NSString stringWithFormat:@"支付宝支付失败:%@",errorStr];
if (faile) {
faile(message);
}
}
}
@end
这块还有点去处理的地方,就是方便代码集中需要统一去处理一下各种第三方的回调
在appdelegate.m的收到回调的方法中统一处理
#pragma mark -- 第三方回调相关 (支付,友盟)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
[PaysTool payHandleOpenURL:url];
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
return result;
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
[PaysTool payHandleOpenURL:url];
return [[UMSocialManager defaultManager] handleOpenURL:url];
}
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
[URLSchemTool handleOpenURL:url];
[PaysTool payHandleOpenURL:url];
return [[UMSocialManager defaultManager] handleOpenURL:url];
}
[PaysTool payHandleOpenURL:url];在这里集中代码
#pragma mark -- //支付回调
+(void)payHandleOpenURL:(NSURL *)url{
[WXApi handleOpenURL:url delegate:(id)[IPayTool shareInstance]];
[[IapppayKit sharedInstance] handleApplicationOpenUrl:url];
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
LOG(@"result = %@",resultDic);//返回的支付结果 //【由于在跳转支付宝客户端支付的过程中,商户 app 在后台很可能被系统 kill 了,所以 pay 接 口的 callback 就会失效,请商户对 standbyCallback 返回的回调结果进行处理,就是在这个方法 里面处理跟 callback 一样的逻辑】
if ([IPayTool shareInstance].aliCallback) {
[IPayTool shareInstance].aliCallback(resultDic);
}
}];
}
[IPayTool shareInstance]就是最上边创建的支付工具,在里边发请求同时处理回调,让代码集中,方便处理实际的业务