1.前言
软件界面如下:
分为官方测试和真实开发项目的两种情况
2.导入微信和支付宝的sdk
7549D052-E6ED-4F81-9FF2-29ADAB30223F.png3.导入依赖库
6F815F78-2651-4C54-9A1D-86393A618009.png4.编写代码
AppDelegate.m文件
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
@interface AppDelegate ()<WXApiDelegate>
实现微信代理的方法
#pragma mark 微信支付的代理方法
- (void)onResp:(BaseResp *)resp {
NSString *strMsg = [NSString stringWithFormat:@"errcode:%d",resp.errCode];
NSString *strTitle;
if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];
}
if ([resp isKindOfClass:[PayResp class]]) {
// 支付返回结果,实际支付结果需要去微信服务器端查询
strTitle = [NSString stringWithFormat:@"支付结果"];
switch (resp.errCode) {
case WXSuccess:
strMsg = @"支付结果:成功!";
break;
default:
strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d,retstr = %@",resp.errCode,resp.errStr];
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
注册微信
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:@"wxb4ba3c02aa476ea1" withDescription:@"注册微信app"];//此处是官方测试的appKey
return YES;
}
ViewController.m代码
#import "DataSigner.h"
#import "Order.h"
#import "ViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
@interface ViewController ()<WXApiDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 20, 200, 44)];
[button1 setTitle:@"支付宝模拟支付" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor redColor];
[button1 addTarget:self action:@selector(testAliPay) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
UIButton* button2 = [[UIButton alloc] initWithFrame:CGRectMake(50, 120, 200, 44)];
[button2 addTarget:self action:@selector(AliPay) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:@"支付宝真实项目" forState:UIControlStateNormal];
button2.backgroundColor = [UIColor greenColor];
[self.view addSubview:button2];
UIButton* button3 = [[UIButton alloc] initWithFrame:CGRectMake(50, 220, 200, 44)];
[button3 addTarget:self action:@selector(testWexinPay) forControlEvents:UIControlEventTouchUpInside];
[button3 setTitle:@"微信模拟支付" forState:UIControlStateNormal];
button3.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:button3];
UIButton* button4 = [[UIButton alloc] initWithFrame:CGRectMake(50, 320, 200, 44)];
[button4 addTarget:self action:@selector(wexinPay) forControlEvents:UIControlEventTouchUpInside];
[button4 setTitle:@"微信真实项目" forState:UIControlStateNormal];
button4.backgroundColor = [UIColor blueColor];
[self.view addSubview:button4];
}
真实项目
#pragma mark 公司项目 微信支付
- (void)wexinPay
{
//1.url公司
NSURL* url = [NSURL URLWithString:公司后台接口地址];
//2.定义请求
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:5];
//后台请求的格式为post方式,json参数就根据后台要求的写
request.HTTPMethod = @"POST";
NSDictionary* json = @{
@"OrderId" : @"R0020001161026000011",//订单号
@"PayChannel" : @"WXPAY_APP"//支付渠道:微信支付
};
NSData* data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
//跟服务器的交互,都是传递json
request.HTTPBody = data;
NSURLSession* session = [NSURLSession sharedSession];
// //3.创建session连接任务
NSURLSessionDataTask* task = [session dataTaskWithRequest:request completionHandler:^(NSData* __nullable data, NSURLResponse* __nullable response, NSError* __nullable error) {
//反序列化,解析,从二进制数据转为JSON格式数据
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
id dict = [[dict objectForKey:@"data"] objectForKey:@"WeiXinPayAppPackage"];//这个也是根据后台的写,反正能读取到数据传入以下内容就可以拉起微信支付
if(dict != nil){
NSLog(@"%@", dict);
NSMutableString *stamp = [dict objectForKey:@"timestamp"];
//调起微信支付
PayReq *req = [[PayReq alloc] init];
req.partnerId = [dict objectForKey:@"partnerid"]; //微信支付分配的商户号,收款账号
req.prepayId = [dict objectForKey:@"prepayid"]; //微信返回的支付交易会话ID
req.nonceStr = [dict objectForKey:@"noncestr"]; //随机字符串
req.timeStamp = stamp.intValue; //时间戳
req.package = [dict objectForKey:@"package"]; //商家根据财付通文档填写的数据和签名
req.sign = [dict objectForKey:@"sign"]; //签名
[WXApi sendReq:req];
}
else
NSLog(@"%@",error);
}];
//4.开启任务,因为默认任务是挂起的
[task resume];
}
真实项目:支付宝支付
#pragma mark 公司项目 支付宝支付
- (void)AliPay
{
NSString* appScheme = @"alisdkdemo"; //这里设置支付宝回调
//二、登录访问
//1.url
NSURL* url = [NSURL URLWithString:公司后台接口地址];
//2.定义请求
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:5];
request.HTTPMethod = @"POST";
NSDictionary* json = @{
@"OrderId" : @"R0020001160926000015",
@"PayChannel" : @"ALIPAY_APP"//支付方式:支付宝
};
NSData* data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
//跟服务器的交互,都是传递json
request.HTTPBody = data;
NSURLSession* session = [NSURLSession sharedSession];
//3.创建session连接任务
NSURLSessionDataTask* task = [session dataTaskWithRequest:request completionHandler:^(NSData* __nullable data, NSURLResponse* __nullable response, NSError* __nullable error) {
//反序列化,解析,从二进制数据转为JSON格式数据
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
if(result != nil){
//支付宝拉起支付,只需要后台返回一个字符串,字符串包含了签名,订单等信息
NSString* orderString = [[[result objectForKey:@"data"] objectForKey:@"AliPayPackage"] objectForKey:@"Body"];
//把字符串传入,就可以拉起支付宝支付
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary* resultDic) {
NSLog(@"reslut = %@", resultDic);
}];
}
else
NSLog(@"error");
}];
//4.开启任务,因为默认任务是挂起的
[task resume];
}
模拟的测试就不写了,其他文章有单独介绍
现在监听支付处理结果
AppDelegate.m编写代码
//iOS9之前,监听微信app跳转回本app
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
//监听微信app跳转回本app
if ([url.host isEqualToString:@"pay"]) {
// 处理微信的支付结果
NSLog(@"微信支付:%@",url);
}
//监听支付宝app跳转回本app
if ([url.host isEqualToString:@"safepay"]) {
// 跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"支付宝支付:%@",resultDic);
}];
}
return YES;
}
//iOS9之后, 监听微信app跳转回本app
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options{
//这里判断是否发起的请求为微信支付,如果是的话,用WXApi的方法调起微信客户端的支付页面(://pay 之前的那串字符串就是你的APPID,)
if ([url.host isEqualToString:@"pay"]) {
// 处理微信的支付结果
NSLog(@"微信支付:%@",url);
}
if ([url.host isEqualToString:@"safepay"]) {
// 跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"支付宝支付:%@",resultDic);
}];
}
return YES;
}
网友评论