BogoPayManager.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // BogoPayModuleObjCManager.m
  3. // BogoPayModuleObjC
  4. //
  5. // Created by 范东 on 2020/3/14.
  6. //
  7. #import "BogoPayManager.h"
  8. #import <AlipaySDK/AlipaySDK.h>
  9. #import "BogoPayOrderModel.h"
  10. #import "BogoPayResponseModel.h"
  11. #import <MJExtension/MJExtension.h>
  12. #import <FDUIKitObjC/FDUIKitObjC.h>
  13. #import <WXApi.h>
  14. @interface BogoPayManager ()<WXApiDelegate>
  15. @end
  16. @implementation BogoPayManager
  17. + (BogoPayManager *)defaultManager{
  18. static BogoPayManager *defaultManager = nil;
  19. static dispatch_once_t predicate;
  20. dispatch_once(&predicate, ^{
  21. defaultManager = [[self alloc] init];
  22. });
  23. return defaultManager;
  24. }
  25. - (void)pay:(BogoPayType)payType orderModel:(BogoPayOrderModel *)orderModel{
  26. if (payType == BogoPayTypeAliPay) {
  27. [[AlipaySDK defaultService] payOrder:orderModel.pay_info fromScheme:@"bugupayv1" callback:^(NSDictionary *resultDic) {
  28. BogoPayResponseModel *model = [BogoPayResponseModel mj_objectWithKeyValues:resultDic];
  29. model.isSuccess = model.resultStatus.intValue == 9000;
  30. if (self.bogo_payResponseCallBack) {
  31. self.bogo_payResponseCallBack(model);
  32. }
  33. }];
  34. }else{
  35. NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
  36. NSArray *paramArray = [orderModel.pay_info componentsSeparatedByString:@"&"];
  37. for (NSString *param in paramArray) {
  38. if (param && param.length) {
  39. NSArray *parArr = [param componentsSeparatedByString:@"="];
  40. if (parArr.count == 2) {
  41. [paramsDict setObject:parArr[1] forKey:parArr[0]];
  42. }
  43. }
  44. }
  45. if(paramsDict != nil){
  46. NSMutableString *retcode = [paramsDict objectForKey:@"retcode"];
  47. if (retcode.intValue == 0){
  48. NSMutableString *stamp = [paramsDict objectForKey:@"timestamp"];
  49. //调起微信支付
  50. PayReq* req = [[PayReq alloc] init];
  51. req.partnerId = [paramsDict objectForKey:@"partnerid"];
  52. req.prepayId = [paramsDict objectForKey:@"prepayid"];
  53. req.nonceStr = [paramsDict objectForKey:@"noncestr"];
  54. req.timeStamp = stamp.intValue;
  55. req.package = [paramsDict objectForKey:@"package"];
  56. req.sign = [paramsDict objectForKey:@"sign"];
  57. [WXApi registerApp:[paramsDict objectForKey:@"appid"] universalLink:@"https://baidu.com/a/"];
  58. [WXApi sendReq:req completion:^(BOOL success) {
  59. //do nothing
  60. if (!success) {
  61. [[FDHUDManager defaultManager] show:@"支付失败" ToView:[UIApplication sharedApplication].keyWindow];
  62. }
  63. }];
  64. //日志输出
  65. NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[paramsDict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
  66. }
  67. else{
  68. [[FDHUDManager defaultManager] show:@"支付失败" ToView:[UIApplication sharedApplication].keyWindow];
  69. }
  70. }else{
  71. [[FDHUDManager defaultManager] show:@"支付失败" ToView:[UIApplication sharedApplication].keyWindow];
  72. }
  73. }
  74. }
  75. - (void)handlePayURL:(NSURL *)url callBack:(nonnull bogo_payResponseCallBack)bogo_payResponseCallBack{
  76. [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
  77. BogoPayResponseModel *model = [BogoPayResponseModel mj_objectWithKeyValues:resultDic];
  78. if (bogo_payResponseCallBack) {
  79. bogo_payResponseCallBack(model);
  80. }
  81. }];
  82. }
  83. - (void)onResp:(BaseResp *)resp{
  84. BogoPayResponseModel *model = [[BogoPayResponseModel alloc]init];
  85. if([resp isKindOfClass:[PayResp class]]){
  86. //支付返回结果,实际支付结果需要去微信服务器端查询
  87. NSString *strMsg;
  88. switch (resp.errCode) {
  89. case WXSuccess:
  90. strMsg = NSLocalizedString(@"支付结果:成功!", nil);
  91. NSLog(NSLocalizedString(@"支付成功-PaySuccess,retcode = %d", nil), resp.errCode);
  92. model.isSuccess = YES;
  93. break;
  94. default:
  95. strMsg = [NSString stringWithFormat:NSLocalizedString(@"支付结果:失败!retcode = %d, retstr = %@", nil), resp.errCode,resp.errStr];
  96. NSLog(NSLocalizedString(@"错误,retcode = %d, retstr = %@", nil), resp.errCode,resp.errStr);
  97. model.isSuccess = NO;
  98. break;
  99. }
  100. if (self.bogo_payResponseCallBack) {
  101. self.bogo_payResponseCallBack(model);
  102. }
  103. }else {
  104. }
  105. }
  106. @end