支付宝
- 导入SDK
cocoapods导入pod 'AliPay', 或者手动拖都可以,不详细说。 - 设置相关配置
- 调用支付
在需要调用的界面包含头文件 #import
[[AlipaySDK defaultService] payOrder:@"后台返回给你的值" fromScheme:@"开始设置的url schemes" callback:^(NSDictionary *resultDic) {
NSString *resultStatus = resultDic[@"resultStatus"];
if ([resultStatus integerValue] == 9000){
//网页支付成功
}else{
//网页支付失败
}
}];
- 客户端支付结果回调
AppDelegate.m中包含头文件 #import
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
if ([url.host isEqualToString:@"safepay"]) {
//跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
if ([resultDic[@"resultStatus"] integerValue] == 9000){
//成功
}else {
//失败
}
}];
return YES;
}else{
//这里判断是否发起的请求为微信支付,如果是的话,用WXApi的方法调起微信客户端的支付页面(://pay 之前的那串字符串就是你的APPID,)
return [WXApi handleOpenURL:url delegate:self];
}
}
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options
{
if ([url.host isEqualToString:@"safepay"]) {
//跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
if ([resultDic[@"resultStatus"] integerValue] == 9000){
//成功
}else {
//失败
}
}];
return YES;
}else{
//这里判断是否发起的请求为微信支付,如果是的话,用WXApi的方法调起微信客户端的支付页面(://pay 之前的那串字符串就是你的APPID,)
return [WXApi handleOpenURL:url delegate:self];
}
}
微信
- 导入SDK
cocoapods 手动托都可以 不详细说 -
设置相关配置
Appdelegate.m 中先注册 包含头文件#import "WXApi.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
//注册
[WXApi registerApp:@"获取到的appkey" withDescription:@"随意写"];
return YES;
}
- 调用支付
在需要调用支付的界面包含头文件 #import "WXApi.h"
#pragma mark --- 微信
- (void)payWithWeChat:(NSDictionary *)para{
//字典是是后台返回给你的数据 对应的键值对可能不一样,根据实际情况来写
NSString *appid = [NSString stringWithFormat:@"%@", para[@"ios"][@"appid"]];
NSString *partnerId = [NSString stringWithFormat:@"%@", para[@"ios"][@"partnerid"]];
NSString *prepayid = [NSString stringWithFormat:@"%@", para[@"ios"][@"prepayid"]];
NSString *package = [NSString stringWithFormat:@"%@", para[@"ios"][@"package"]];
NSString *nonceStr = [NSString stringWithFormat:@"%@", para[@"ios"][@"noncestr"]];
NSString *sign = [NSString stringWithFormat:@"%@", para[@"ios"][@"sign"]];
UInt32 timeStamp = 0;
if (para[@"ios"][@"timestamp"]) {
timeStamp = [para[@"ios"][@"timestamp"] unsignedIntValue];
}
//需要创建这个支付对象
PayReq *req = [[PayReq alloc] init];
//由用户微信号和AppID组成的唯一标识,用于校验微信用户
req.openID = appid;
// 商家id,在注册的时候给的
req.partnerId = partnerId;
// 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你
req.prepayId = prepayid;
// 根据财付通文档填写的数据和签名
//这个比较特殊,是固定的,只能是即req.package = Sign=WXPay
req.package = package;
// 随机编码,为了防止重复的,在后台生成
req.nonceStr = nonceStr;
// 这个是时间戳,也是在后台生成的,为了验证支付的
req.timeStamp =timeStamp;
// 这个签名也是后台做的
req.sign = sign;
//发送请求到微信,等待微信返回onResp
[WXApi sendReq:req];
}
#pragma mark - sign加密(一般都是后台加密)
-(NSString *)createMD5SingForPayWithAppID:(NSString *)appid_key partnerid:(NSString *)partnerid_key prepayid:(NSString *)prepayid_key package:(NSString *)package_key noncestr:(NSString *)noncestr_key timestamp:(UInt32)timestamp_key{
NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
[signParams setObject:appid_key forKey:@"appid"];//微信appid 例如wxfb132134e5342
[signParams setObject:noncestr_key forKey:@"noncestr"];//随机字符串
[signParams setObject:package_key forKey:@"package"];//扩展字段 参数为 Sign=WXPay
[signParams setObject:partnerid_key forKey:@"partnerid"];//商户账号
[signParams setObject:prepayid_key forKey:@"prepayid"];//此处为统一下单接口返回的预支付订单号
[signParams setObject:[NSString stringWithFormat:@"%u",timestamp_key] forKey:@"timestamp"];//时间戳
NSMutableString *contentString =[NSMutableString string];
NSArray *keys = [signParams allKeys];
//按字母顺序排序
NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
//拼接字符串
for (NSString *categoryId in sortedArray) {
if ( ![[signParams objectForKey:categoryId] isEqualToString:@""]
&& ![[signParams objectForKey:categoryId] isEqualToString:@"sign"]
&& ![[signParams objectForKey:categoryId] isEqualToString:@"key"]
)
{
[contentString appendFormat:@"%@=%@&", categoryId, [signParams objectForKey:categoryId]];
}
}
//添加商户密钥key字段 API 密钥
[contentString appendFormat:@"key=%@", @"商户秘钥"];
NSString *result = [self md5String:contentString];//md5加密
return result;
}
- (NSString *)md5String:(NSString *)string
{
if(string == nil || [string length] == 0) return nil;
unsigned char digest[CC_MD5_DIGEST_LENGTH], i;
CC_MD5([string UTF8String], (int)[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding], digest);
NSMutableString *ms = [NSMutableString string];
for(i=0;i
- 微信支付结果回调
请参考开始支付宝结果回调,两个方法中有写到微信,接下来就只写微信支付结果的处理,也是在appdelegate中实现
#pragma mark - 微信支付
//9.0前的方法,为了适配低版本 保留
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [WXApi handleOpenURL:url delegate:self];
}
//微信SDK自带的方法,处理从微信客户端完成操作后返回程序之后的回调方法,显示支付结果的
-(void) onResp:(BaseResp*)resp
{
//启动微信支付的response
NSString *payResoult = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
if([resp isKindOfClass:[PayResp class]]){
//支付返回结果,实际支付结果需要去微信服务器端查询
switch (resp.errCode) {
case 0:
payResoult = @"支付结果:成功!";
break;
case -1:
payResoult = @"支付结果:失败!";
break;
case -2:
payResoult = @"用户已经退出支付!";
break;
default:
payResoult = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
break;
}
}
}
附
可以在获取支付结果成功或失败的时候发送通知,以便在支付的界面处理成功或失败的操作
//支付宝发送支付结果通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"AliPay" object:resultDic[@"resultStatus"]];
//在调用支付界面接受通知(viewDidLoad中实现)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alipay:) name:@"AliPay" object:nil];
//支付宝客户端支付成功通知
- (void)alipay:(NSNotification *)not{
if ([not.object isEqualToString:@"9000"]) {
//支付成功
}else if ([not.object isEqualToString:@"6001"]){
//用户取消支付
}else{
//支付失败
}
[[NSNotificationCenter defaultCenter] removeObserver:not];
}
//微信发送支付结果通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"WechatPay" object:[NSString stringWithFormat:@"%d", resp.errCode]];
//在调用支付界面接受通知(viewDidLoad中实现)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wechatPayResult:) name:@"WechatPay" object:nil];
#pragma mark - 微信回调
- (void)wechatPayResult:(NSNotification *)not{
if ([not.object integerValue] == 0) {
//支付成功
}else if ([not.object integerValue] == -1){
//支付失败
}else if ([not.object integerValue] == -2){
//用户退出支付
}
[[NSNotificationCenter defaultCenter] removeObserver:not];
}