iOS_微信支付
2015-08-31 14:22
483 查看
通过h5界面来调度微信支付:
1.在代理方法里面实现截取的json串(当然得判断有没有)
2.将json串解密
NSString * string = [self decryptUseDES:jsonStr key:S_key]; // jsonStr 为加密json
串
其中解密的方法:
//解密
- (NSString ) decryptUseDES:(NSString)cipherText key:(NSString*)key
{
NSData* cipherData = [GTMBase64 decodeString:cipherText];
unsigned char buffer[1024];
memset(buffer, 0, sizeof(char));
size_t numBytesDecrypted = 0;
Byte iv[] = {1,2,3,4,5,6,7,8};
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding,
[key UTF8String],
kCCKeySizeDES,
iv,
[cipherData bytes],
[cipherData length],
buffer,
1024,
&numBytesDecrypted);
NSString* plainText = nil;
if (cryptStatus == kCCSuccess) {
NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
plainText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
return plainText;
}
3.将解密后的json串转化成字典
NSDictionary * jsonDic = [self dictionaryWithJsonString:string];
//根据json串得到dic
- (NSDictionary )dictionaryWithJsonString:(NSString )jsonString {
if (jsonString == nil) {
return nil;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err) {
NSLog(@”json解析失败:%@”,err);
return nil;
} return dic;
}
4.然后用字典请求微信支付:
- (void)WXpaySendRequestWithDic:(NSDictionary *)dic{
//此处请求接口;
PayReq *request = [[PayReq alloc] init];
request.partnerId = dic[@”PARTNER_ID”];
request.prepayId= dic[@”prePayid”];
request.package = dic[@”package”];
request.nonceStr= dic[@”noncestr”];
request.timeStamp= [dic[@”timestamp”]intValue];
request.sign= dic[@”sign”];
[WXApi sendReq:request];
}
微信支付的SDK,这里连个代理都没有,所以,你想获取支付后的结果的返回信息,需要去AppDelegate里,添加如下代码,添加微信的代理:
(BOOL)application:(UIApplication )application openURL:(NSURL )url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WXApi handleOpenURL:url delegate:self];
}
然后,再实现这个方法:
-(void) onResp:(BaseResp*)resp
通过这个方法,你就可以获取微信支付后的信息了。但是如果这样,所有的支付代码都在AppDelegate里,不好,至少微信给的例子就是这样。我的方法是添加通知,代码如下:
-(void) onResp:(BaseResp*)resp
{
if ([resp isKindOfClass:[PayResp class]])
{
PayResp response = (PayResp )resp;
// NSString *strTitle = [NSString stringWithFormat:@”支付结果”];
// NSString *strMsg = [NSString stringWithFormat:@”errcode:%d”, response.errCode];
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle
// message:strMsg
// delegate:self
// cancelButtonTitle:@”OK”
// otherButtonTitles:nil, nil];
// [alert show];
switch (response.errCode) {
case WXSuccess: {
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@”success”];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
default: {
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@”fail”];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
}
}
}
然后,在支付那个VC,添加通知#pragma mark - tabBar隐藏
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if([WXApi isWXAppInstalled]) // 判断 用户是否安装微信
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:ORDER_PAY_NOTIFICATION object:nil];//监听一个通知
}
}#pragma mark - tabbar还原
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter]removeObserver:self];//移除通知
}
1.在代理方法里面实现截取的json串(当然得判断有没有)
2.将json串解密
NSString * string = [self decryptUseDES:jsonStr key:S_key]; // jsonStr 为加密json
串
其中解密的方法:
//解密
- (NSString ) decryptUseDES:(NSString)cipherText key:(NSString*)key
{
NSData* cipherData = [GTMBase64 decodeString:cipherText];
unsigned char buffer[1024];
memset(buffer, 0, sizeof(char));
size_t numBytesDecrypted = 0;
Byte iv[] = {1,2,3,4,5,6,7,8};
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding,
[key UTF8String],
kCCKeySizeDES,
iv,
[cipherData bytes],
[cipherData length],
buffer,
1024,
&numBytesDecrypted);
NSString* plainText = nil;
if (cryptStatus == kCCSuccess) {
NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
plainText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
return plainText;
}
3.将解密后的json串转化成字典
NSDictionary * jsonDic = [self dictionaryWithJsonString:string];
//根据json串得到dic
- (NSDictionary )dictionaryWithJsonString:(NSString )jsonString {
if (jsonString == nil) {
return nil;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err) {
NSLog(@”json解析失败:%@”,err);
return nil;
} return dic;
}
4.然后用字典请求微信支付:
- (void)WXpaySendRequestWithDic:(NSDictionary *)dic{
//此处请求接口;
PayReq *request = [[PayReq alloc] init];
request.partnerId = dic[@”PARTNER_ID”];
request.prepayId= dic[@”prePayid”];
request.package = dic[@”package”];
request.nonceStr= dic[@”noncestr”];
request.timeStamp= [dic[@”timestamp”]intValue];
request.sign= dic[@”sign”];
[WXApi sendReq:request];
}
微信支付的SDK,这里连个代理都没有,所以,你想获取支付后的结果的返回信息,需要去AppDelegate里,添加如下代码,添加微信的代理:
(BOOL)application:(UIApplication )application openURL:(NSURL )url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WXApi handleOpenURL:url delegate:self];
}
然后,再实现这个方法:
-(void) onResp:(BaseResp*)resp
通过这个方法,你就可以获取微信支付后的信息了。但是如果这样,所有的支付代码都在AppDelegate里,不好,至少微信给的例子就是这样。我的方法是添加通知,代码如下:
-(void) onResp:(BaseResp*)resp
{
if ([resp isKindOfClass:[PayResp class]])
{
PayResp response = (PayResp )resp;
// NSString *strTitle = [NSString stringWithFormat:@”支付结果”];
// NSString *strMsg = [NSString stringWithFormat:@”errcode:%d”, response.errCode];
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle
// message:strMsg
// delegate:self
// cancelButtonTitle:@”OK”
// otherButtonTitles:nil, nil];
// [alert show];
switch (response.errCode) {
case WXSuccess: {
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@”success”];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
default: {
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@”fail”];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
}
}
}
然后,在支付那个VC,添加通知#pragma mark - tabBar隐藏
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if([WXApi isWXAppInstalled]) // 判断 用户是否安装微信
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:ORDER_PAY_NOTIFICATION object:nil];//监听一个通知
}
}#pragma mark - tabbar还原
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter]removeObserver:self];//移除通知
}
相关文章推荐
- 微信分享
- 微信红包API接口(PHP)
- 每天一个小程序——统计字符的个数
- 微信API学习笔记
- 微信 redirect_uri 参数错误
- 使用Chrome修改user agent模拟微信内置浏览器
- 微信公众平台开发(四)——微信支付
- 微信公众平台开发之天气预报功能
- 一款基于微客服的仿微信的聊天软件
- 【微信开发】一获取用户授权(静默授权方式)
- 微信开发大坑汇总之微信支付篇
- 【wechat】微信开发——自定义菜单
- 微信公众平台开发之天气预报功能
- 微信公众号开发模型WeChat
- 微信图片预览
- iOS微信支付相关~~
- 生成微信公众号关注二维码
- iOS 微信第三方登录
- 基于Nodejs的微信消息加密与解密实现概要
- 用循环语句while或for循环编写一个验证登陆的小程序