//结算
- (void)buttonAction:(UIButton *)sender
{
pragma 获取用户当前IP
//调起微信支付
if ([self.payStr isEqualToString:@"weixin"]) {
//判断是否安装微信
BOOL hadInstalledWeixin = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]];
if (YES == hadInstalledWeixin) {
NSLog(@"安装微信");
// 显示正在网络请求 加载显示
self.hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:self.hud];
self.hud.labelText = @"GoPaying";
//改变了加载Loading的样式
self.hud.labelFont = [UIFont fontWithName:@"American Typewriter" size:16 * ScreenWidth];
[self.hud show:YES];
NSDictionary *dic = @{@"orderId":self.CashId};
[[NetworkSingleton sharedManager] postResultWithParameter:dic url:DetailOrdr_Api successBlock:^(id responseBody) {
if ([@"0" isEqualToString:[responseBody objectForKey:@"delFlag"]]) {
NSString *payPrice = [NSString stringWithFormat:@"%.f", [self.sumOrder floatValue] * 100];
NSInteger order = [payPrice integerValue];
NSString *PayNum = [NSString stringWithFormat:@"%ld", (long)order];
NSDictionary *dict = @{@"channel":@"wx", @"amount":PayNum, @"client_ip":[self getIPAddress:YES], @"currency":@"cny", @"subject":@"场馆预定", @"body":@"场馆", @"order_no":self.CashId};
[[NetworkSingleton sharedManager] postResultWithParameter:dict url:OnlinePay_Api successBlock:^(id responseBody) {
NSDictionary *dic = (NSDictionary *)responseBody;
NSData* data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
PayCashViewController * __weak weakSelf = self;
NSString* charge = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[Pingpp createPayment:charge
viewController:weakSelf
appURLScheme:kUrlScheme
withCompletion:^(NSString *result, PingppError *error) {
if ([result isEqualToString:@"success"]) {
[self.hud hide:YES];
self.payStr = nil;
// 支付成功
NSLog(@"支付成功");
SuccessPayViewController *success = [[SuccessPayViewController alloc] init];
success.PassSelect = self.PassSelect;
success.consDate = self.consDate;
success.venueId = self.venueId;
success.projectId = self.projectId;
[self.navigationController pushViewController:success animated:YES];
} else {
[self.hud hide:YES];
// 支付失败或取消
NSLog(@"Error: code=%lu msg=%@", (unsigned long)error.code, [error getMsg]);
}
}];
} failureBlock:^(NSString *error) {
[self.hud hide:YES];
NSLog(@"%@", error);
UIAlertView *alertv = [[UIAlertView alloc] initWithTitle:nil message:@"请求超时" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction:) userInfo:alertv repeats:YES];
[alertv show];
}];
} else {
[self.hud hide:YES];
UIAlertView *alertv = [[UIAlertView alloc] initWithTitle:nil message:@"订单已过期" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction:) userInfo:alertv repeats:YES];
[alertv show];
}
} failureBlock:^(NSString *error) {
NSLog(@"%@", error);
[self.hud hide:YES];
UIAlertView *alertv = [[UIAlertView alloc] initWithTitle:nil message:@"请求超时" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction:) userInfo:alertv repeats:YES];
[alertv show];
}];
} else {
[self.hud hide:YES];
UIAlertView *alertv = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未安装微信" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction:) userInfo:alertv repeats:YES];
[alertv show];
}
}
(NSString *)getIPAddress:(BOOL)preferIPv4
{
NSArray *searchArray = preferIPv4 ?
@[ IOS_VPN @”/” IP_ADDR_IPv4, IOS_VPN @”/” IP_ADDR_IPv6, IOS_WIFI @”/” IP_ADDR_IPv4, IOS_WIFI @”/” IP_ADDR_IPv6, IOS_CELLULAR @”/” IP_ADDR_IPv4, IOS_CELLULAR @”/” IP_ADDR_IPv6 ] :
@[ IOS_VPN @”/” IP_ADDR_IPv6, IOS_VPN @”/” IP_ADDR_IPv4, IOS_WIFI @”/” IP_ADDR_IPv6, IOS_WIFI @”/” IP_ADDR_IPv4, IOS_CELLULAR @”/” IP_ADDR_IPv6, IOS_CELLULAR @”/” IP_ADDR_IPv4 ] ;NSDictionary *addresses = [self getIPAddresses];
NSLog(@”addresses: %@”, addresses);__block NSString *address;
[searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
{
address = addresses[key];
if(address) *stop = YES;
} ];
return address ? address : @”0.0.0.0”;
}(NSDictionary *)getIPAddresses
{
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];// retrieve the current interfaces - returns 0 on success
struct ifaddrs *interfaces;
if(!getifaddrs(&interfaces)) {
// Loop through linked list of interfaces
struct ifaddrs *interface;
for(interface=interfaces; interface; interface=interface->ifa_next) {
if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
continue; // deeply nested code harder to read
}
const struct sockaddr_in addr = (const struct sockaddr_in)interface->ifa_addr;
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
NSString *type;
if(addr->sin_family == AF_INET) {
if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
type = IP_ADDR_IPv4;
}
} else {
const struct sockaddr_in6 addr6 = (const struct sockaddr_in6)interface->ifa_addr;
if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
type = IP_ADDR_IPv6;
}
}
if(type) {
NSString *key = [NSString stringWithFormat:@”%@/%@”, name, type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];
}
}
}
// Free memory
freeifaddrs(interfaces);
}
return [addresses count] ? addresses : nil;
}