3.IOS客户端开发
导入微信开发包


添加URL Types

在AppDelegate.m中注册应用
#import "AppDelegate.h"
#import "XSTabBarViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
@interface AppDelegate ()<WXApiDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// [NSThread sleepForTimeInterval:2.0];
// 进入主控制器
self.window = [[UIWindow alloc] init];
self.window.frame = [UIScreen mainScreen].bounds;
self.window.rootViewController = [[XSTabBarViewController alloc] init];
[self.window makeKeyAndVisible];
//向微信注册应用。
[WXApi registerApp:@"wxfb96c2a9b531be26"];
return YES;
}
-(void) onResp:(BaseResp*)resp
{
// NSLog(@" ----onResp %@",resp);
/*
ErrCode ERR_OK = 0(用户同意)
ERR_AUTH_DENIED = -4(用户拒绝授权)
ERR_USER_CANCEL = -2(用户取消)
code 用户换取access_token的code,仅在ErrCode为0时有效
state 第三方程序发送时用来标识其请求的唯一性的标志,由第三方程序调用sendReq时传入,由微信终端回传,state字符串长度不能超过1K
lang 微信客户端当前语言
country 微信用户当前国家信息
*/
if ([resp isKindOfClass:[SendAuthResp class]]) //判断是否为授权请求,否则与微信支付等功能发生冲突
{
SendAuthResp *aresp = (SendAuthResp *)resp;
if (aresp.errCode== 0)
{
// NSLog(@"code %@",aresp.code);
[[NSNotificationCenter defaultCenter] postNotificationName:@"wechatDidLoginNotification" object:self userInfo:@{@"code":aresp.code}];
}
}else{ // 支付请求回调
//支付返回结果,实际支付结果需要去微信服务器端查询
NSString *strMsg = [NSString stringWithFormat:@"支付结果"];
NSString *respcode = @"0";
switch (resp.errCode) {
case WXSuccess:
strMsg = @"支付结果:成功!";
// NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
respcode = @"1";
break;
default:
strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
// NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
respcode = @"0";
break;
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"wechatDidPayNotification" object:self userInfo:@{@"respcode":respcode}];
}
}
//iOS 9.0 之前的处理方法不保证正确,如有错误还望指正
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(@"iOS 9.0 ֮ǰ");
return [self applicationOpenURL:url];
}
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
NSLog(@"iOS 9.0 之后");
return [self applicationOpenURL:url];
}
- (BOOL)applicationOpenURL:(NSURL *)url
{
if([[url absoluteString] rangeOfString:@"wxfb96c2a9b531be26://pay"].location == 0){
return [WXApi handleOpenURL:url delegate:self];
}
if ([url.host isEqualToString:@"safepay"])
{
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:nil];
return YES;
}
return YES;
}
}
在必须支付的Controller中接受微信支付通知
- (void)viewDidLoad {
[super viewDidLoad];
// 接受微信支付通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wechatDidPayNotification:) name:@"wechatDidPayNotification" object:nil];
}
向服务器端获取统一订单,并拉起微信进行支付
-(void)weixinPay
{
NSString *userUrlStr = [NSString stringWithFormat:@"%@?sid=%@&account=%@&desc=%@", WX_PREPAY_URL, self.student.sid,self.payJinE,self.student.nid];
NSURL *url = [NSURL URLWithString:userUrlStr];
// NSLog(@"userUrlStr = %@", userUrlStr);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[MBProgressHUD showMessage:@"跳转中,请稍候"];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
[MBProgressHUD hideHUD];
// NSLog(@"微信支付的response = %@", operation.responseString);
NSData *JSONData = [operation.responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *userDict = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
// 调用微信支付
PayReq *request = [[PayReq alloc] init];
/** 商家向财付通申请的商家id */
request.partnerId = [userDict objectForKey:@"partnerid"];
/** 预支付订单 */
request.prepayId= [userDict objectForKey:@"prepayid"];
/** 商家根据财付通文档填写的数据和签名 */
request.package = [userDict objectForKey:@"package"];
/** 随机串,防重发 */
request.nonceStr= [userDict objectForKey:@"noncestr"];
/** 时间戳,防重发 */
request.timeStamp= [[userDict objectForKey:@"timestamp"] intValue];
/** 商家根据微信开放平台文档对数据做的签名 */
request.sign= [userDict objectForKey:@"sign"];
self.sign = request.sign;
self.ordnum = [userDict objectForKey:@"ordnum"];
[WXApi sendReq: request];
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[MBProgressHUD hideHUD];
NSLog(@"发生错误!%@",error);
}];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
}
// 微信支付结果
-(void)wechatDidPayNotification:(NSNotification *)notification
{
// NSLog(@"wechatDidPayNotification");
NSDictionary *nameDictionary = [notification userInfo];
NSString *respcode = [nameDictionary objectForKey:@"respcode"];
if([respcode isEqualToString:@"1"]){
// 支付成功,更新用户信息
[self payDidFinish];
}else{
// 支付失败,
[self setupAlertControllerWithTitle:@"微信支付结果" messge:@"本次支付未完成,您可以稍后重试!" confirm:@"好的"];
}
}
以上就是本文的全部内容,希望对你们的学习有所帮助,也期望你们多多支持脚本之家。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/sanxing/article-121389-2.html
霉菌给了中国加大岛礁建设的绝佳良机