Params
<pre>
1.应用APPID
2.微信支付商户号
3.微信支付商户密钥
</pre>
CS_XML
字符:
<pre>
<xml>
<appid>应用APPID</appid>
<body>公司-描述</body>
<mch_id>微信支付商户号</mch_id>
<nonce_str>VVA6TKJMECEJDDCSI0M04SI73S9IDKO8</nonce_str>
<notify_url>http://www.weixin.qq.com/wxpay/pay.php</notify_url>
<out_trade_no>1499224427384612</out_trade_no>
<spbill_create_ip>10.50.45.40</spbill_create_ip>
<total_fee>1</total_fee>
<trade_type>APP</trade_type>
<sign>E458129168DADC4E0CC5EDB738CF89C9</sign>
</xml>
</pre>
请求
<pre>
//预付单与查询订单请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL: [NSURL URLWithString:requestType==wxPay?@"https://api.mch.weixin.qq.com/pay/unifiedorder":@"https://api.mch.weixin.qq.com/pay/orderquery"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:12];
[request setHTTPMethod:@"POST"];
[request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"UTF-8" forHTTPHeaderField:@"charset"];
[request setHTTPBody:[xml dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession]
dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//亲,你的代码
}];
[dataTask resume];
</pre>
解析
<pre>
//H文件
import <Foundation/Foundation.h>
@interface HDXmlParser : NSObject<NSXMLParserDelegate>
{
NSXMLParser* xmlParser;
NSMutableString* valBuffer;
NSMutableDictionary* dictionary;
NSError* lastError;
}
/** xml解析 */
- (NSDictionary)parseData:(NSData)data;
@end
##########华丽分割线#########
//M文件
import "HDXmlParser.h"
@implementation HDXmlParser
- (NSDictionary)parseData:(NSData)data
{
HDXmlParser* parser = [[HDXmlParser alloc] init];
return [parser parseData:data];
}
(NSDictionary)parseData:(NSData)data
{
lastError = nil;
dictionary = [NSMutableDictionary new];
valBuffer = [NSMutableString string];
xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:self];
[xmlParser parse];
if(lastError)
return nil;
return [dictionary copy];
}(void)parser:(NSXMLParser)parser foundCharacters:(NSString)string{
[valBuffer setString:string];
}-
(void)parser:(NSXMLParser)parser
didEndElement:(NSString)elementName
namespaceURI:(NSString)namespaceURI
qualifiedName:(NSString)qName{if([valBuffer isEqualToString:@"\n"]==NO &&
[elementName isEqualToString:@"root"]==NO)
{
[dictionary setObject:[valBuffer copy] forKey:elementName];
}
} (void)parser:(NSXMLParser)parser parseErrorOccurred:(NSError)parseError
{
lastError = parseError;
}(void)parser:(NSXMLParser)parser validationErrorOccurred:(NSError)validationError
{
lastError = validationError;
}
@end
</pre>
TOOL
作为NSString扩展 依赖于:
<CommonCrypto/CommonDigest.h>
<sys/socket.h>
<sys/sockio.h>
<sys/ioctl.h>
<net/if.h>
<arpa/inet.h>
md5-方法1
<pre>
-
(NSString *)md5String:(NSString *)str {
const char *myPasswd = [str UTF8String ];
unsigned char mdc[ 16 ];
CC_MD5 (myPasswd, ( CC_LONG ) strlen (myPasswd), mdc);
NSMutableString *md5String = [ NSMutableString string ];
for ( int i = 0 ; i< 16 ; i++) {
[md5String appendFormat : @"%02x" ,mdc[i]];
}
NSString *md5Str = [md5String uppercaseString];
return md5Str;
}
</pre>
md5-方法2
<pre>
-
( NSString *)md5StringNB:( NSString *)str {
const char *myPasswd = [str UTF8String ];
unsigned char mdc[ 16 ];
CC_MD5 (myPasswd, ( CC_LONG ) strlen (myPasswd), mdc);
NSMutableString *md5String = [ NSMutableString string ];
[md5String appendFormat : @"%02x" ,mdc[ 0 ]];
for ( int i = 1 ; i< 16 ; i++) {
[md5String appendFormat : @"%02x" ,mdc[i]^mdc[ 0 ]];
}
NSString *md5Str = [md5String uppercaseString];
return md5Str;
}
</pre>
获取IP地址
<pre>
-
(NSString *)getIPAddress {
int sockfd =socket(AF_INET,SOCK_DGRAM, 0);
// if (sockfd <</span> 0) return nil;
NSMutableArray *ips = [NSMutableArray array];
int BUFFERSIZE =4096;
struct ifconf ifc;
char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
struct ifreq *ifr, ifrcopy;
ifc.ifc_len = BUFFERSIZE;
ifc.ifc_buf = buffer;
if (ioctl(sockfd,SIOCGIFCONF, &ifc) >= 0){
for (ptr = buffer; ptr < buffer + ifc.ifc_len; ){ ifr = (struct ifreq *)ptr; int len =sizeof(struct sockaddr); if (ifr->ifr_addr.sa_len > len) { len = ifr->ifr_addr.sa_len; } ptr += sizeof(ifr->ifr_name) + len; if (ifr->ifr_addr.sa_family !=AF_INET) continue; if ((cptr = (char *)strchr(ifr->ifr_name,':')) != NULL) *cptr =0; if (strncmp(lastname, ifr->ifr_name,IFNAMSIZ) == 0)continue; memcpy(lastname, ifr->ifr_name,IFNAMSIZ); ifrcopy = *ifr; ioctl(sockfd,SIOCGIFFLAGS, &ifrcopy); if ((ifrcopy.ifr_flags &IFF_UP) == 0)continue; NSString *ip = [NSString stringWithFormat:@"%s",inet_ntoa(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr)]; [ips addObject:ip]; }
}
close(sockfd);
NSString *deviceIP =@"";
for (int i=0; i < ips.count; i++)
{
if (ips.count >0) { deviceIP = [NSString stringWithFormat:@"%@",ips.lastObject]; }
}
return deviceIP;
}
</pre>
获取随机数
<pre>
-
(NSString *)getNonce_str {
NSArray *sourceStr = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",
@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",
@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",
@"U",@"V",@"W",@"X",@"Y",@"Z"];NSString *resultStr = [[NSMutableString alloc] init];
for (int i = 0; i < 32; i ++) {
int value = arc4random() % 32; resultStr = [resultStr stringByAppendingString:[NSString stringWithFormat:@"%@",sourceStr[value]]];
}
return [NSString stringWithString:resultStr];
}
</pre>
获取时间戳-(10位)
<pre>
time_t now;
time(&now);
timeStamp = (UInt32)[[NSString stringWithFormat:@"%ld", now] integerValue];
</pre>
WXDOC
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5