1. 支付功能
第一步:配置一些基本设置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//设置微信的appKey和appSecret
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:KWechatKey appSecret:KWechatAppSecret redirectURL:@"http://mobile.umeng.com/social"];
}
第二步:
- (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) {
NSLog(@"result = %@",resultDic);
NSString *dic = [resultDic objectForKey:@"result"];
NSData *jsonData = [dic dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:nil];
int staute = [resultDic intValueForKey:@"resultStatus"];
switch (staute) {
case 9000:
[self requsetPay:[jsonDic objectForKey:@"sign"]];
break;
case 8000:
SHOW_MESSAGE_VIEW(nil, @"正在处理中", @"确定", nil);
break;
case 4000:
SHOW_MESSAGE_VIEW(nil, @"订单支付失败", @"确定", nil);
break;
case 6001:
SHOW_MESSAGE_VIEW(nil, @"用户中途取消", @"确定", nil);
break;
case 6002:
SHOW_MESSAGE_VIEW(nil, @"网络连接出错", @"确定", nil);
break;
default:
break;
}
}];
} else if([[url absoluteString] hasPrefix:@"wx"]) {
return [WXApi handleOpenURL:url delegate:self];
}
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if ([[url absoluteString] hasPrefix:@"tencent"]) {
return [TencentOAuth HandleOpenURL:url];
}else if([[url absoluteString] hasPrefix:@"wb"]) {
return [WeiboSDK handleOpenURL:url delegate:self];
}else if([[url absoluteString] hasPrefix:@"wx"]){
return [WXApi handleOpenURL:url delegate:self];
}
}
第三步:
-(void)onResp:(BaseResp*)resp{
if([resp isKindOfClass:[PayResp class]])
{
//支付返回结果,实际支付结果需要去微信服务器端查询
PayResp * payResult = (PayResp *)resp;
[[NSNotificationCenter defaultCenter] postNotificationName:WXPAY_NOTIFICATION object:self userInfo:[NSDictionary dictionaryWithObject:payResult forKey:@"wxPayReturn"]];
}
}
第四步:
在支付页面中,添加通知喔~~
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weixinPayReturn:) name:WXPAY_NOTIFICATION object:nil];
//微信支付后的返回
-(void)weixinPayReturn:(NSNotification *)info
{
PayResp * result = [info.userInfo objectForKey:@"wxPayReturn"];
switch (result.errCode) {
case WXSuccess:
[self performSelector:@selector(checkThePayResult) withObject:self afterDelay:1.0];
[self showMyMessage:@"支付结果:成功!"];
break;
default:
[self showMyMessage: [NSString stringWithFormat:@"支付结果:失败"]];
NSLog(@"错误,retcode = %d, retstr = %@", result.errCode,result.errStr);
break;
}
}
-(void)checkThePayResult{
//处理服务器的请求了(一般是支付成功后的回调接口)
[self requsetPay:@""];
}
第五步:很重要的 (点击支付按钮的事件)
-(void)payMethodWithUserPay;
{
NSString *url;
if (style ==1) {
url = [NSString stringWithFormat:@"%@Pay/Wxpay?order_number=%@&user_id=%@&token=%@",HaoSe_URL,self.model.order_number,USERID,TOKEN];
}else{
url = [NSString stringWithFormat:@"%@Pay/getSignStr?order_number=%@&user_id=%@&token=%@",HaoSe_URL,self.model.order_number,USERID,TOKEN];
}
[commonSet post:url params:nil success:^(id json) {
NSDictionary *result = json;
if (style ==1) {
//调起微信支付
NSDictionary *dic = [result objectForKey:@"data"];
WXPayData* req = [[WXPayData alloc] init];
req.openID = [dic stringForKey:@"appId"];
req.partnerId = [dic stringForKey:@"partnerid"];//商户ID
req.prepayId = [dic stringForKey:@"prepayid"];
req.nonceStr = [dic stringForKey:@"noncestr"];
req.timeStamp = [dic intValueForKey:@"timestamp"];
req.package = @"Sign=WXPay";
// req.spKey = [result stringForKey:@"key"];
// req.tradeNo = [result stringForKey:@"tradeNo"];
//生成签名
// NSString *sign = [GlobalTools createMd5Sign:req];
NSString *sign = [dic stringForKey:@"sign"];
req.sign = sign;
// _tradeNo = req.tradeNo;
PayReq* payData = [[PayReq alloc] init];
payData = req;
if ([WXApi sendReq:payData])
{
NSLog(@"微信支付发送成功");
}else
{
NSLog(@"微信支付发送失败");
}
}else{
NSString *params = [[result objectForKey:@"data"]objectForKey:@"params"];
// Order *zhifuOrder = [[Order alloc] initWithDictionary:params];
// zhifuOrder.privateKey = [[result objectForKey:@"data"] objectForKey:@"key"];
// NSString *orderSpec = [zhifuOrder description];
//
NSString *key = [[result objectForKey:@"data"]objectForKey:@"key"];
//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
id signer = CreateRSADataSigner(key);
NSString *signedString = [signer signString:params];
NSString *appScheme = @"alisdkdemo";
//将签名成功字符串格式化为订单字符串,请严格按照该格式
NSString *orderString = nil;
if (key != nil) {
orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
params, signedString, @"RSA"];
[[NSUserDefaults standardUserDefaults]setObject:_model.order_number forKey:@"order"];
//调支付宝
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
int staute = [resultDic intValueForKey:@"resultStatus"];
NSString *dic = [resultDic objectForKey:@"result"];
switch (staute) {
case 9000:
[self requsetPay:dic];
break;
case 8000:
SHOW_MESSAGE_VIEW(nil, @"正在处理中", @"确定", nil);
break;
case 4000:
SHOW_MESSAGE_VIEW(nil, @"订单支付失败", @"确定", nil);
break;
case 6001:
SHOW_MESSAGE_VIEW(nil, @"用户中途取消", @"确定", nil);
break;
case 6002:
SHOW_MESSAGE_VIEW(nil, @"网络连接出错", @"确定", nil);
break;
default:
break;
}
}];
}
}
} failure:^(NSError *error) {
}];
}