您的位置:首页 > 移动开发 > 微信开发

微信支付、银联支付、支付宝

2015-12-14 17:18 489 查看

appdelegate中添加方法:

(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {

//    注册微信支付ID
[WXApi registerApp:APP_ID withDescription:@"app"];


}

(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);

}]; }

if ([url.host isEqualToString:@”platformapi”]){//支付宝钱包快登授权返回 authCode

[[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {

NSLog(@”result = %@”,resultDic);

}];

}

if ([url.host isEqualToString:@”pay”]){

[WXApi handleOpenURL:url delegate:self];

}

return YES;

}

(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);

}]; }

if ([url.host isEqualToString:@”platformapi”]){//支付宝钱包快登授权返回 authCode

[[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {

NSLog(@”result = %@”,resultDic);

}];

}

if ([url.host isEqualToString:@”pay”]){

[WXApi handleOpenURL:url delegate:self];

}

return YES;

}

pragma mark - 微信支付代理方法

-(void) onResp:(BaseResp*)resp {

NSString *strTitle = nil;

NSString *strMsg = nil;

if([resp isKindOfClass:[PayResp class]]){

if (resp.errCode == WXSuccess) {
//支付返回结果,实际支付结果需要去微信服务器端查询
NSString *result = nil;
NSString *hintInfo = nil;
result = @"支付结果";
hintInfo = @"支付结果:成功!";

strTitle = result;
strMsg = hintInfo;

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}


}

执行文件中添加方法:

pragma mark - 微信支付

(void)weixinPayWithMoney:(NSString )money WithAccount:(NSString )account {

if (![WXApi isWXAppInstalled]) {

[self alert:nil msg:_wxNoInstall];

return;

}

NSString *urlString = [NSString stringWithFormat:@”http://%@/WeChatPay/payServlet?number=%@&txnAmt=%@&type=0“, @”120.26.110.99”, account, money];

//解析服务端返回json数据

NSError *error;

//加载一个NSURL对象

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

//将请求的url数据放到NSData对象中

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

if ( response != nil) {

NSMutableDictionary *dict = NULL;

//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中

dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];

NSLog(@"url:%@",urlString);
if(dict != nil){
NSMutableString *retcode = [dict objectForKey:@"retcode"];
if (retcode.intValue == 0){
NSMutableString *stamp  = [dict objectForKey:@"timeStamp"];

//调起微信支付
PayReq* req             = [[PayReq alloc] init];
req.openID              = [dict objectForKey:@"appid"];
req.partnerId           = @"1250026601";
req.prepayId            = [dict objectForKey:@"prepay_id"];
req.nonceStr            = [dict objectForKey:@"nonceStr"];
req.timeStamp           = stamp.intValue;
req.package             = @"Sign=WXPay";
req.sign                = [dict objectForKey:@"sign"];
[WXApi sendReq:req];
//日志输出
NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",req.openID,req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
}else{
[self alert:_hintStr msg:_wrongNetStr];
}
}else{
[self alert:_hintStr msg:_wrongNetStr];
}


}else{

[self alert:_hintStr msg:_wrongNetStr];

}

}

//客户端提示信息

- (void)alert:(NSString )title msg:(NSString )msg

{

UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@”OK” otherButtonTitles:nil];

[alter show];


}

pragma mark - 支付宝充值

(void)alipayWithMoney:(NSString )money WithAccount:(NSString )account{

// 账号,不能修改

NSString *partner = @”“;

// 账号,不能修改

NSString *seller = @”“;

// 秘钥,不能修改

NSString *privateKey = @”“;

/*

*生成订单信息及签名

*/

//将商品信息赋予AlixPayOrder的成员变量

Order *order = [[Order alloc] init];

order.partner = partner;

order.seller = seller;

order.tradeNO = [self generateTradeNO]; //订单ID(由商家自行制定)

order.productName = @”充值”; //商品标题

// cpass账号作为商品描述,对应充值成功后返回的body

order.productDescription = account; //商品描述

order.amount = [NSString stringWithFormat:@”%@”,money]; //商品价格

// order.amount = @”0.01”;

// order.notifyURL = @”http://120.26.110.99/Alipay/backUrl“; //回调URL

order.notifyURL = [NSString stringWithFormat:@”http://%@“, [HOST_IP stringByAppendingString:@”/Alipay/backUrl”]];

order.service = @”mobile.securitypay.pay”;

order.paymentType = @”1”;

order.inputCharset = @”utf-8”;

order.itBPay = @”30m”;

order.showUrl = @”m.alipay.com”;

//应用注册scheme,在AlixPayDemo-Info.plist定义URL types

NSString *appScheme = @”“;

//将商品信息拼接成字符串

NSString *orderSpec = [order description];

NSLog(@”orderSpec = %@”,orderSpec);

//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode

id signer = CreateRSADataSigner(privateKey);

NSString *signedString = [signer signString:orderSpec];

//将签名成功字符串格式化为订单字符串,请严格按照该格式

NSString *orderString = nil;

if (signedString != nil) {

orderString = [NSString stringWithFormat:@”%@&sign=\”%@\”&sign_type=\”%@\”“,

orderSpec, signedString, @”RSA”];

[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
[Conference getBalance];

[[NSNotificationCenter defaultCenter] postNotificationName:CheckAccount object:nil];

}];


}

}

pragma mark ==============产生随机订单号==============

(NSString *)generateTradeNO

{

static int kNumber = 15;

NSString *sourceStr = @”0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ”;

NSMutableString *resultStr = [[NSMutableString alloc] ini
4000
t];

srand((unsigned int)time(0));

for (int i = 0; i < kNumber; i++)

{

unsigned index = rand() % [sourceStr length];

NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];

[resultStr appendString:oneStr];

}

return resultStr;

}

pragma mark - 银联支付

(void)unionPayWithMoney:(NSString )money WithAccount:(NSString )account {

// 后台获取tn

NSString url = [NSString stringWithFormat:@”UnionPay/IndexServlet?number=%@&txnAmt=%ld”, account, (long)[money integerValue] 100];

// NSString *url = [NSString stringWithFormat:@”UnionPay/IndexServlet?number=%@&txnAmt=%ld”, account, (long)1];

MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:HOST_IP customHeaderFields:nil];

MKNetworkOperation *op = [engine operationWithPath:url params:nil httpMethod:@”GET” ssl:NO];

[op addCompletionHandler:^(MKNetworkOperation *operation) {

NSString *responseString = [operation responseString];

NSLog(@”responseString :%@”, responseString);

[UPPayPlugin startPay:responseString mode:@”00” viewController:self delegate:self];

} errorHandler:^(MKNetworkOperation *operation, NSError *error) {

NSLog(@”error :%@”, error);

// [OMGToast showWithText:CustomLocalizedString(@”网络错误”, nil) duration:2.0f];

}];

[engine enqueueOperation:op];

}

pragma mark 银联支付结果代理方法

-(void)UPPayPluginResult:(NSString*)result {

NSLog(@”unionPay result:%@”, result);

[Conference getBalance];

}

info里面需要配置

*

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: