#import "IOAPayApi.h"
@implementation IOAPayRequestModel
- (void)setPayType:(PayType)payType {
_payType = payType;
if (_payType == kPayTypeALiPay) {
self.appScheme = @"IOAAlipaySDK";
// 测试
// self.payString = @"alipay_sdk=1.0&app_id=2018012502067343&biz_content=%7B%22subject%22%3A%22%E6%94%AF%E4%BB%98%E5%AE%9D%E6%94%AF%E4%BB%98%22%2C%22out_trade_no%22%3A%222018032100007%22%2C%22total_amount%22%3A%220.01%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22timeout_express%22%3A%2210m%22%2C%22seller_id%22%3A%222088001065568658%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay¬ify_url=api.ioa365.net%2Fapp%2Fapi%2FPayment%2Falipay_notify&sign_type=RSA2×tamp=2018-03-21+16%3A59%3A15&version=1.0&sign=SFyiWFqdkG98qarJFKfNjts8w2RS7ATwjCRyNnKYILDaVVFEnR%2F943QjK9WFaZgipx38rZuRf%2Bl4M74Hp2PO%2F%2F0ZfSKAntTU3DMLIVgt4YD42W1F2lOP3iXtkL5BHpPzt6YfDQueCz1zReeAWQXlyBDvvnjbJ9p67f2jt8jfqM6Enz6kWwY5cShyDD6iJQF0FKXdmSYA%2BiCO6IIHdiqKsRLHuBPb8lfSxJyY1%2FbaAUysIB3%2BiU6HWASUGadCVL769ivwHKwdjZQZUoFpjcfnneyZG3%2B4f%2FnlBrY1pKk3ZWy2UqpTtk0w0GofsF167dRz47J0lW7UufyM8uA%2BIhZ7Lw%3D%3D";
return;
}
if (_payType == kPayTypeUNPay) {
self.appScheme = @"UPPay";
// 测试
// self.payString = @"559018436594292239101";
return;
}
// 测试
// self.userInfo = @{
// @"appid":@"wx66a3135d1354b23b",
// @"noncestr":@"J8pJbaEg6AjDQ9kk",
// @"partnerid":@"1497576612",
// @"prepayid":@"wx20180321170621b3fbce61a20187009040",
// @"result_code":@"SUCCESS",
// @"return_code":@"SUCCESS",
// @"return_msg":@"OK",
// @"sign":@"29FFF7B63A71D3FB4C6866BDBC443F72",
// @"timestamp":@"1521623180",
// @"trade_type":@"APP",
// };
}
@end
@implementation IOAPayResponseModel
@end
@interface IOAPayApi() <WXApiDelegate>
@end
@implementation IOAPayApi
//单例方法
static IOAPayApi *manager = nil;
+ (instancetype)defaultPayManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [IOAPayApi new];
});
return manager;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [super allocWithZone:zone];
});
return manager;
}
//copy在底层 会调用copyWithZone:
- (instancetype)copyWithZone:(NSZone *)zone {
return [[self class] defaultPayManager];
}
+ (instancetype)copyWithZone:(struct _NSZone *)zone {
return [self defaultPayManager];
}
+ (instancetype)mutableCopyWithZone:(struct _NSZone *)zone {
return [self defaultPayManager];
}
- (instancetype)mutableCopyWithZone:(NSZone *)zone {
return [[self class] defaultPayManager];
}
#pragma mark - WeiChatPayDelegate
- (void)onResp:(BaseResp *)resp {
//启动微信支付的response
if (self.payRequestModel.payType == kPayTypeWXPay) {
if (self.callback) {
IOAPayResponseModel *payResponseModel = [IOAPayResponseModel new];
payResponseModel.result = 0;
if([resp isKindOfClass:[PayResp class]]){
//支付返回结果,实际支付结果需要去微信服务器端查询
switch (resp.errCode) {
case 0:
// payResoult = @"支付结果:成功!";
payResponseModel.result = 1;
break;
case -1:
payResponseModel.result = 0;
break;
case -2:
payResponseModel.result = 0;
break;
default:
break;
}
}
self.callback(payResponseModel);
}
}
}
#pragma mark - Public
// 是否安装了客户端
- (BOOL)isPayAppInstalled:(PayType)payType {
switch (payType) {
case kPayTypeWXPay:
return [WXApi isWXAppInstalled];
break;
case kPayTypeALiPay:
// 未提供接口 返回NO
return NO;
break;
case kPayTypeUNPay:
return [[UPPaymentControl defaultControl] isPaymentAppInstalled];
break;
default:
break;
}
return NO;
}
- (void)pay:(IOAPayRequestModel *)payRequestModel callback:(void (^)(IOAPayResponseModel *response))callback {
if (!payRequestModel) return;
self.callback = callback;
self.payRequestModel = payRequestModel;
switch (payRequestModel.payType) {
case kPayTypeWXPay:
[self wxPay:payRequestModel];
break;
case kPayTypeALiPay:
[self aliPay:payRequestModel];
break;
case kPayTypeUNPay:
[self unPay:payRequestModel];
break;
default:
break;
}
}
// 支付回调
- (void)payCallbackWithUrl:(NSURL *)url {
// 其他如支付等SDK的回调
if ([url.host isEqualToString:@"safepay"]) {
[self aliPayCallback:url];
}
else if ([url.host isEqualToString:@"pay"]) {
// 处理微信的支付结果
[self wxPayCallback:url];
}
else if ([url.host isEqualToString:@"uppayresult"]) {
[self unPayCallback:url];
}
}
#pragma mark - Pay
// 微信支付
- (void)wxPay:(IOAPayRequestModel *)payRequestModel {
PayReq *req = [[PayReq alloc] init];
NSDictionary *dataDic = payRequestModel.userInfo;
//由用户微信号和AppID组成的唯一标识,用于校验微信用户
req.openID = dataDic[@"appid"];
// 商家id,在注册的时候给的
req.partnerId = dataDic[@"partnerid"];
// 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你
req.prepayId = dataDic[@"prepayid"];
// 根据财付通文档填写的数据和签名
req.package = @"Sign=WXPay";
// 随机编码,为了防止重复的,在后台生成
req.nonceStr = dataDic[@"noncestr"];
// 这个是时间戳,也是在后台生成的,为了验证支付的
NSString * stamp = dataDic[@"timestamp"];
req.timeStamp = stamp.intValue;
// 这个签名也是后台做的
req.sign = dataDic[@"sign"];
//发送请求到微信,等待微信返回onResp
[WXApi sendReq:req];
}
// 支付宝
- (void)aliPay:(IOAPayRequestModel *)payRequestModel {
NSString *appScheme = payRequestModel.appScheme;
NSString *payString = payRequestModel.payString;
__weak __typeof(self)weakSelf = self;
[[AlipaySDK defaultService] payOrder:payString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
if (weakSelf.payRequestModel.payType == kPayTypeALiPay) {
if (weakSelf.callback) {
IOAPayResponseModel *payResponseModel = [IOAPayResponseModel new];
payResponseModel.userInfo = resultDic;
payResponseModel.result = [resultDic[@"result"] integerValue];
weakSelf.callback(payResponseModel);
}
}
}];
}
// 银联支付
- (void)unPay:(IOAPayRequestModel *)payRequestModel {
NSString *appScheme = payRequestModel.appScheme;
NSString *payString = payRequestModel.payString;
[[UPPaymentControl defaultControl] startPay:payString fromScheme:appScheme mode:@"01" viewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
//////
- (void)wxPayCallback:(NSURL *)url {
//跳转支付宝钱包进行支付,处理支付结果
[WXApi handleOpenURL:url delegate:self];
}
- (void)aliPayCallback:(NSURL *)url {
__weak typeof(self)weakSelf = self;
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
if (weakSelf.payRequestModel.payType == kPayTypeALiPay) {
if (weakSelf.callback) {
IOAPayResponseModel *payResponseModel = [IOAPayResponseModel new];
payResponseModel.userInfo = resultDic;
payResponseModel.result = [resultDic[@"result"] integerValue];
weakSelf.callback(payResponseModel);
}
}
}];
}
- (void)unPayCallback:(NSURL *)url {
__weak typeof(self)weakSelf = self;
[[UPPaymentControl defaultControl]handlePaymentResult:url completeBlock:^(NSString *code, NSDictionary *data) {
if (weakSelf.payRequestModel.payType == kPayTypeUNPay) {
if (weakSelf.callback) {
IOAPayResponseModel *payResponseModel = [IOAPayResponseModel new];
payResponseModel.userInfo = data;
if ([code isEqualToString:@"success"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"YINLIANPAYS" object:nil];
payResponseModel.result = [code boolValue];
}
else if([code isEqualToString:@"fail"]) {
//交易失败
[[NSNotificationCenter defaultCenter] postNotificationName:@"YINLIANPAYF" object:nil];
payResponseModel.result = [code boolValue];
}
else if([code isEqualToString:@"cancel"]) {
//交易取消
[[NSNotificationCenter defaultCenter] postNotificationName:@"YINLIANPAYC" object:nil];
payResponseModel.result = 0;
}
weakSelf.callback(payResponseModel);
}
}
}];
}
@end