(1)先創(chuàng)建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 =@"這是我提現(xiàn)的錢"; //商品描述
? ? 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{
? ? //日志輸出
? ? //需要創(chuàng)建這個支付對象
? ? 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";
? ? //發(fā)送請求到微信邮屁,等待微信返回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:@"價格"];
? ? }
}
謝謝支持K莼觥A谚鄙!