(1)先创建MoneySelectView 和 PaySelectView
.h文件内容
@interface KKMoneySelectView : UIView
@property(nonatomic,strong)UIButton * button;
@property(nonatomic,copy)void(^selectBlock)(NSInteger flag);
.m文件内容
#import "KKMoneySelectView.h"
#import "UIColor+Factory.h"
@implementation KKMoneySelectView
-(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
[self createUI];
}
return self;
}
-(void)createUI{
NSArray *MoneyArrr = @[@"19.99/月",@"49.99/季",@"99.99/年"];
for (int index = 0; index < [MoneyArrr count];index ++) {
self.button =[UIButton buttonWithType: UIButtonTypeCustom];
self.button.frame = CGRectMake(15 + index *100, 10,90, self.frame.size.height-20);
[self.button setTitle:MoneyArrr[index] forState:UIControlStateNormal];
[self.button setBackgroundColor:[UIColor mainGrayColor]];
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[self addSubview:self.button];
[self.button.titleLabel setFont:[UIFont fontWithName:@"PingFangSC-Regular" size:15]];
[self.button addTarget:self action:@selector(down:) forControlEvents:UIControlEventTouchUpInside];
self.button.tag = index + 100;
self.button.layer.borderWidth = 1;
self.button.layer.cornerRadius = 10;
self.button.layer.borderColor=[UIColor mainGrayColor].CGColor;
}
}
/**
* 单选
*/
-(void)down:(UIButton *)bt{
for (int index = 0 ; index < 3; index ++) {
if (bt.tag== 100 + index) {
bt.selected = YES;
bt.layer.borderColor = [UIColorFromRGB(0x0fdcff)CGColor];
bt.layer.borderWidth= 1;
continue;
}
UIButton *btn=(UIButton*)[self viewWithTag:index + 100];
btn.selected = NO;
btn.layer.borderWidth = 1;
btn.layer.borderColor=[UIColor mainGrayColor].CGColor;
if (_selectBlock) {
_selectBlock(index);
}
}
};
payview(同上)
.h文件内容
@property(nonatomic,strong)UIButton * button;
@property(nonatomic,copy)void(^selectBlock)(NSInteger flag);
.m文件内容
#import "KKPayView.h"
#import "UIColor+Factory.h"
@implementation KKPayView
-(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
[self createUI];
}
return self;
}
-(void)createUI{
NSArray *MoneyArrr = @[@"vip_icon_weixin",@"vip_icon_zfb"];
for (int index = 0; index < [MoneyArrr count];index ++) {
self.button =[UIButton buttonWithType: UIButtonTypeCustom];
self.button.frame = CGRectMake(15 + index *100, 10,90, self.frame.size.height-20);
[self.button setImage:[UIImage imageNamed:MoneyArrr[index]] forState:UIControlStateNormal];
[self addSubview:self.button];
[self.button setBackgroundColor:[UIColor mainGrayColor]];
[self.button addTarget:self action:@selector(down:) forControlEvents:UIControlEventTouchUpInside];
self.button.tag = index + 100;
self.button.layer.borderWidth = 1;
self.button.layer.cornerRadius = 10;
self.button.layer.borderColor=[UIColor mainGrayColor].CGColor;
}
}
/**
* 单选
*/
-(void)down:(UIButton *)bt{
for (int index = 0 ; index < 3; index ++) {
if (bt.tag== 100 + index) {
bt.selected = YES;
bt.layer.borderColor = [UIColorFromRGB(0x0fdcff)CGColor];
bt.layer.borderWidth= 1;
continue;
}
UIButton *btn=(UIButton*)[self viewWithTag:index + 100];
btn.selected = NO;
btn.layer.borderWidth = 1;
btn.layer.borderColor=[UIColor mainGrayColor].CGColor;
if (_selectBlock) {
_selectBlock(index);
}
}
}
(2)支付Model
.h文件内容
#import "WXApiObject.h"#import "WXApi.h"@interface KKPayModel : NSObject/*
* 支付宝支付
*/
+(void)AliyPayOrder:(NSString *)AliOrderStr AndPriceStr:(NSString *)pricestr;
/*
* 微信支付
*/
+(void)WechatpayOrder:(NSString *)WxOrderStr AndPriceStr:(NSString *)pricestr;
.m文件内容
#import "KKPayModel.h"#import "Order.h"#import "DataSigner.h"#import#import "WXApiObject.h"
#import "WXApi.h"
@implementation KKPayModel
+(void)AliyPayOrder:(NSString *)AliOrderStr AndPriceStr:(NSString *)pricestr{
/*
*点击获取prodcut实例并初始化订单信息
*/
/*
*商户的唯一的parnter和seller。
*签约后,支付宝会为每个商户分配一个唯一的 parnter 和 seller。
*/
/*============================================================================*/
/*=======================需要填写商户app申请的===================================*/
/*============================================================================*/
NSString *partner = @"2088421837870096";
NSString *seller = @"pay@XXXXX.com";
NSString *privateKey = @"MIICXAIBAAKBgQCinHZRu/QRg8LpPEVCnjSXKSXsHyMUycOnxBp9VJXOK5pIR9md64R03PZ062jiAh+Qj8juUHxdjyl4gCof5FhFm/TP12A3EtB7498hBWyHNhq25SKtj2e/4CfOb7k7vyHZ72YEgRdjqkLrP5Hff0c4do0z6WIpy6aIgs9+XYPlIwIDAQABAoGAGzbENnmWJzg97nq/vJ5tfr7MMfAkFt6NeA5BwKffSlnRwDcGHH/F8yjntjmPnANu9OqbXgbqSV4kOp54bk3w17EriN5AwtxWEX4W5TsOFGqBcULLPFCIuwiL5ftcxoXZJOnuH2U3GVWd29vChnaD/IFxt2qFbOe/NysC0t6ReQECQQDQP2V5wz5VZCyL8+oqI/3vijmvh+x3hIu37IKB26RfFDvBP5AumlPgS0JA/pTG+JsqBHSDY88DMb9sDqUerReTAkEAx+YaZ9hS/vEfQhAbvGV9r138RS84sjD8HB6TRn7gsmgddHmEHa40RufBC091dKfMaeQ5C1GlucujUYZ0Ag1WMQJAVEWjcf/hxLqSEFy3oTAJfhtIttDhj4gy5k4ujhBtVxCgEdcAKOvJD5ZFDHo5iL9oQzukTttgFaHXHgpbJavUQwJACB3Ssqa47/sQXtlSlvqz+LvjM1fwgGO2yNmA91rNetD5aTJJ5/6bxslAMFpglYT+qsoGeuwsw3d+QSApc6/g8QJBAM7DTiGezb4gYzjSmdR/vm4KdQePVon6jY1qG1aPdWeJeWjJRkxEzcAYo9SKMGHqpeYIeZ4zapJbot09wVflwEc=";
/*============================================================================*/
/*============================================================================*/
/*============================================================================*/
//partner和seller获取失败,提示
if ([partner length] == 0 ||
[seller length] == 0 ||
[privateKey length] == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"缺少partner或者seller或者私钥。"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
return;
}
/*
*生成订单信息及签名
*/
//将商品信息赋予AlixPayOrder的成员变量
Order *order = [[Order alloc] init];
order.partner = partner;
order.seller = seller;
order.tradeNO =@"20160324012412412"; //订单ID(由商家自行制定)
order.productName =@"11"; //商品标题
order.productDescription =@"这是我提现的钱"; //商品描述
order.amount = [NSString stringWithFormat:@"%@",@"11"]; //商品价格
order.notifyURL = @"http://www.xxx.com"; //回调URL
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 = @"AlipayType";
[[AlipaySDK defaultService] payOrder:@"20160324012412412" fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
}];
}
-(void)jj{
}
- (NSString *)generateTradeNO
{
static int kNumber = 15;
NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString *resultStr = [[NSMutableString alloc] init];
srand(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;
}
+(void)WechatpayOrder:(NSString *)WxOrderStr AndPriceStr:(NSString *)pricestr{
//日志输出
//需要创建这个支付对象
PayReq *req = [[PayReq alloc] init];
//由用户微信号和AppID组成的唯一标识,用于校验微信用户
req.openID = @"wx1bf301b7c806001f";
// 商家id,在注册的时候给的
req.partnerId = @"10000100";
// 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你
req.prepayId = @"wx20160218122935e3753eda1f0066087993";
// 根据财付通文档填写的数据和签名
//这个比较特殊,是固定的,只能是即req.package = Sign=WXPay
req.package = @"Sign=WXPay";
// 随机编码,为了防止重复的,在后台生成
req.nonceStr = @"d1e6ecd5993ad2d06a9f50da607c971c";
// 这个是时间戳,也是在后台生成的,为了验证支付的
req.timeStamp = 1455769775;
// 这个签名也是后台做的
req.sign = @"F6DEE4ADD82217782919A1696500AF06";
//发送请求到微信,等待微信返回onResp
[WXApi sendReq:req];
}
@end
(3)在控制器中初始化(整体布局是个table)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * iden = @"iden";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:iden];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
cell.selectionStyle =UITableViewCellSelectionStyleNone;
__weak typeof (self)weakself = self;
switch (indexPath.section) {
case 0:{
break;
}
case 1:{
KKMoneySelectView * select = [[KKMoneySelectView alloc] initWithFrame:CGRectMake(0,
0,
self.view.frame.size.width,
self.view.frame.size.height / 9)];
[cell.contentView addSubview:select];
select.selectBlock = ^(NSInteger flag) {
switch (flag) {
case 0:{
weakself.priceStr = @"19.99";
break;
}
case 1:{
weakself.priceStr = @"49.99";
break;
}
case 2:{
weakself.priceStr = @"99.99";
break;
}
default:
break;
}
};
break;
}
case 2:{
KKPayView * kkpay = [[KKPayView alloc] initWithFrame:CGRectMake(0,
0,
self.view.frame.size.width,
self.view.frame.size.height / 9)];
[cell.contentView addSubview:kkpay];
kkpay.selectBlock = ^(NSInteger flag) {
switch (flag) {
case 0:{//微信
weakself.payType = @"1";
break;
}
case 1:{//支付宝
weakself.payType = @"2";
break;
}
default:
break;
}
};
break;
}
default:
break;
}
}
return cell;
}
支付按钮的click
-(void)payClick{
/*
* 支付宝支付
*/
if ([_payType isEqualToString:@"1"]) {
[KKPayModel AliyPayOrder:@"支付宝订单号" AndPriceStr:@"价格"];
}
/*
* 微信支付
*/
if ([_payType isEqualToString:@"2"]) {
[KKPayModel WechatpayOrder:@"微信订单号" AndPriceStr:@"价格"];
}
}
谢谢支持!!!