支付宝支付:
把支付宝的sdk和依赖的库集成到项目里,然后配置支付宝的跳转url,这个确保唯一,可以设置成boundle id
配置支付宝的白名单
向服务器请求加密后的商品信息,在成功的获取到加密后的商品信息后,调用支付宝的支付接口,为了方便可以直接把支付宝和微信支付提取成工具类
工具类的单例
+ (SeparatePayment *)shareSeparatePayment;
调用支付宝的支付接口,传入加密的商品信息
/**
* 支付宝支付
*
* @param orderInfor 支付宝支付信息
* @param toPayToCompleteOrGiveUp 回调
*/
- (void)accordingToTheOrderInformationToPayTreasureToPay:(NSString *)orderInfor
toPayToCompleteOrGiveUp:(ToPayToCompleteOrGiveUp)toPayToCompleteOrGiveUp;
成功的回调
typedef void(^ToPayToCompleteOrGiveUp) (NSDictionary * dic);
.m实现工具方法
- (void)accordingToTheOrderInformationToPayTreasureToPay:(NSString *)orderInfor
toPayToCompleteOrGiveUp:(ToPayToCompleteOrGiveUp)toPayToCompleteOrGiveUp
{
// 应用注册scheme,在plist定义URL types
NSString *appScheme = @"*******";
UIWindow* windowtemp = ((AppDelegate *)([UIApplication sharedApplication].delegate)).window;
if (windowtemp.hidden) {
windowtemp.hidden = NO;
}
else
{
NSLog(@"no hidden");
}
self.toPayToCompleteOrGiveUp = toPayToCompleteOrGiveUp;
[[AlipaySDK defaultService] payOrder:orderInfor fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"%@", resultDic);
windowtemp.hidden = NO;
self.toPayToCompleteOrGiveUp(resultDic);
}];
}
工具类完成,只需要在控制器中调用,监听支付宝支付结果的回调,处理结果
// NOTE: 9.0以后使用新API接口- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options
返回的信息
if ([errorCode intValue] == 6001) {
NSLog(@"取消支付");
}else if ([errorCode intValue] == 9000){
NSLog(@"支付成功");
// 需要延迟1秒向服务器校验支付结果,如果服务器返回的结果也是成功,就像用户展示支付成功
}
因为支付加密介意放在后台,所以客户端没什么事,做起来比较简单
微信支付:
同样需要配置URL types和白名单
/**
* 微信支付回调
*
* @param wxPayBean 支付信息
* @param toPayToCompleteOrGiveUp 回调
*/
- (void)weChatPayOrderInfor:(NSDictionary *)wxPayBean
toPayToCompleteOrGiveUp:(ToPayToCompleteOrGiveUp)toPayToCompleteOrGiveUp;
- (void)afterTheSuccessOfTheParseWeChatCallbackInformation:(BaseResp*)resp;
.m实现工具类
- (void)weChatPayOrderInfor:(NSDictionary *)wxPayBean toPayToCompleteOrGiveUp:(ToPayToCompleteOrGiveUp)toPayToCompleteOrGiveUp
{
self.toPayToCompleteOrGiveUp = toPayToCompleteOrGiveUp;
[WXApi registerApp:WXAppKey withDescription:@"demo 2.0"];
BeanWxPayBean *beanWxPay = [BeanWxPayBean objectWithKeyValues:wxPayBean];
PayReq *req = [[PayReq alloc] init];
req.openID = beanWxPay.appid;
req.partnerId = beanWxPay.partnerid;
req.prepayId = beanWxPay.prepayid;
req.nonceStr = beanWxPay.noncestr;
req.timeStamp = beanWxPay.timestamp;
req.package = beanWxPay.package;
req.sign = beanWxPay.sign;
NSLog(@"\n%@\n%@\n%@\n%@\n%u\n%@\n%@", req.openID, req.partnerId, req.prepayId, req.nonceStr, (unsigned int)req.timeStamp, req.package, req.sign);
[WXApi sendReq:req];
}
#pragma mark - 微信支付的回调
-(void)onResp:(BaseResp*)resp
{
[self afterTheSuccessOfTheParseWeChatCallbackInformation:resp];
}
#pragma mark - 微信支付的回调
-(void)afterTheSuccessOfTheParseWeChatCallbackInformation:(BaseResp*)resp
{
if([resp isKindOfClass:[PayResp class]]){
//支付返回结果,实际支付结果需要去微信服务器端查询
switch (resp.errCode) {
case WXSuccess:{
// 处理支付成功
}
case WXErrCodeUserCancel:{
// 处理支付取消
}
default:{
// 需要回调,在控制器处理支付成功后的代码
}
}
}
self.toPayToCompleteOrGiveUp = nil;
}
网友评论