移动支付常用的是微信支付和支付宝支付,所以本篇之封装了这两种,银联用的较少,所以就没有封装进去.
集成步骤这里就不在说了,我把微信支付和支付宝支付的文档放在这里自己去看: 微信支付官方文档 支付宝官方文档
上代码:
//单例 + (instancetype)shareManager; //处理跳转url,回到应用,需要在delegate中实现 - (BOOL)cdm_handleUrl:(NSURL *)handleUrl; //注册App,需要在 didFinishLaunchingWithOptions 中调用 - (void)cdm_registerApp; /* * @param orderMessage 传入订单信息,如果是字符串,则对应是跳转支付宝支付;如果传入PayReq 对象,这跳转微信支付,注意,不能传入空字符串或者nil * @param callBack 回调,有返回状态信息 */ - (void)cdm_payOrderMessage:(id)orderMessage callBack:(CDMPayCompleteCallBack)callBack;
在AppDelegate
处理回调
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]]; self.window.rootViewController = nav; [[PayManager shareManager] cdm_registerApp]; // Override point for customization after application launch. return YES; } /* * 最老的版本,最好也写上 */ - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { return [[PayManager shareManager] cdm_handleUrl:url]; } /* * iOS 9.0 之前 会调用 */ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [[PayManager shareManager] cdm_handleUrl:url]; } /* * iOS 9.0 以上(包括iOS9.0) */ - (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options{ return [[PayManager shareManager] cdm_handleUrl:url]; }
调用:
1.支付宝支付(数据用的是文档上面的数据):
//支付宝文档数据. NSString *orderMessage = @"app_id=2015052600090779&biz_content=%7B%22timeout_express%22%3A%2230m%22%2C%22seller_id%22%3A%22%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22total_amount%22%3A%220.02%22%2C%22subject%22%3A%221%22%2C%22body%22%3A%22%E6%88%91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22%2C%22out_trade_no%22%3A%22314VYGIAGG7ZOYY%22%7D&charset=utf-8&method=alipay.trade.app.pay&sign_type=RSA×tamp=2016-08-15%2012%3A12%3A15&version=1.0&sign=MsbylYkCzlfYLy9PeRwUUIg9nZPeN9SfXPNavUCroGKR5Kqvx0nEnd3eRmKxJuthNUx4ERCXe552EV9PfwexqW%2B1wbKOdYtDIb4%2B7PL3Pc94RZL0zKaWcaY3tSL89%2FuAVUsQuFqEJdhIukuKygrXucvejOUgTCfoUdwTi7z%2BZzQ%3D"; [[PayManager shareManager] cdm_payOrderMessage:orderMessage callBack:^(CDMPayStateCode stateCode, NSString *stateMsg) { NSLog(@"stateCode = %zd,stateMsg = %@",stateCode,stateMsg); }];
2.微信支付(文档数据):
PayReq* req = [[PayReq alloc] init]; req.partnerId = @"10000100"; req.prepayId= @"1101000000140415649af9fc314aa427"; req.package = @"Sign=WXPay"; req.nonceStr= @"a462b76e7436e98e0ed6e13c64b4fd1c"; req.timeStamp= @"1397527777".intValue; req.sign= @"582282D72DD2B03AD892830965F428CB16E7A256"; [[PayManager shareManager] cdm_payOrderMessage:req callBack:^(CDMPayStateCode stateCode, NSString *stateMsg) { NSLog(@"stateCode = %zd,stateMsg = %@",stateCode,stateMsg); }];