移動支付常用的是微信支付和支付寶支付,所以本篇之封裝了這兩種,銀聯用的較少,所以就沒有封裝進去.
集成步驟這里就不在說了,我把微信支付和支付寶支付的文檔放在這里自己去看: 微信支付官方文檔 支付寶官方文檔
上代碼:
//單例 + (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); }];
Demo地址:https://github.com/domanc/PayManager.git
