一婆瓜、集成微信支付必要SDK和前期的設(shè)置工作(參考官方文檔)
微信支付官網(wǎng)文檔
不得不吐槽桦沉,這個(gè)官方文檔難道是實(shí)習(xí)生寫的定枷?發(fā)出來不review一下?
二沉颂、調(diào)起支付(關(guān)鍵性代碼)其實(shí)這些參數(shù)也可以后臺(tái)計(jì)算后返回給我們這樣andriod和iOS端都不用做
<pre><code>
PayReq *request = [[PayReq alloc] init];
request.partnerId =partnerId; //商戶號(hào)
request.prepayId=prepayId;//訂單id
request.package = @"Sign=WXPay";
NSArray *arr = @[@"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"];
// 獲取隨機(jī)數(shù)条摸,通過隨機(jī)0-25數(shù)字取數(shù)組中的字母,拼接成一個(gè)26位的隨機(jī)字符串
NSMutableString *strnon = [[NSMutableString alloc]init];
strnon = [[NSMutableString alloc]init];
for (int i=0; i<26; i++) {
int i = arc4random() % 26 ;
NSString *strnon1 = [arr objectAtIndex:i];
[strnon appendString:strnon1];
}
request.nonceStr= strnon;
//獲取系統(tǒng)當(dāng)前的時(shí)間間隔
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString *timeString = [NSString stringWithFormat:@"%0.f", a];
//10位整型
request.timeStamp= [timeString intValue];
// 拼接參數(shù)
NSString *str1 = [NSString stringWithFormat:@"appid=%@&noncestr=%@&package=%@&partnerid=%@&prepayid=%@×tamp=%@",appid ,strnon, @"Sign=WXPay",partnerId,prepayId,timeString];
// 拼接商戶密鑰key
NSString *str2 = [NSString stringWithFormat:@"%@&key=%@",str1,key];
NSString *sign1 = [self md5:str2];
// 小寫變大寫
NSString *sign2 = [sign1 uppercaseString];
// 賦值生成的參數(shù)MD5簽名
request.sign =sign2;
//調(diào)用微信支付請(qǐng)求
[WXApi sendReq:request];
</code></pre>
用到的MD5函數(shù)
<pre><code>
//MD5 32位小寫加密
- (NSString *)md5:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5( cStr, (int)strlen(cStr), result );
return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0],result[1],result[2],result[3],
result[4],result[5],result[6],result[7],
result[8],result[9],result[10],result[11],
result[12],result[13],result[14],result[15]];
}
</pre></code>
三铸屉、微信支付回調(diào)(注冊通知钉蒲,在接收到支付狀態(tài)后發(fā)送通知)
<pre><code>
/**
- 支持9.0以上
*/
- (BOOL)application:(UIApplication *)app openURL:(NSURL )url options:(NSDictionary<NSString, id> *)options
{
[WXApi handleOpenURL:url delegate:self];
return YES;
}
/**
- 只支持9.0以下
*/
- (BOOL)application:(UIApplication )application handleOpenURL:(NSURL )url
{
return [WXApi handleOpenURL:url delegate:self];
}
/
- 只支持9.0以下
*/
-
(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{[WXApi handleOpenURL:url delegate:self];
return YES;
}
/**
- 微信回調(diào)
/
-(void)onResp:(BaseResp)resp{
if ([resp isKindOfClass:[PayResp class]]){
PayResp *response=(PayResp *)resp;
switch(response.errCode){
case WXSuccess:
//服務(wù)器端查詢支付通知或查詢API返回的結(jié)果再提示成功
NSLog(@"支付成功");
[[NSNotificationCenter defaultCenter] postNotificationName:WxPaySucceedNoti object:nil];
break;
case WXErrCodeUserCancel:
NSLog(@"支付取消");
[[NSNotificationCenter defaultCenter] postNotificationName:WxPayCancelNoti object:nil];
break;
default:
NSLog(@"支付失敗,retcode=%d",resp.errCode);
[[NSNotificationCenter defaultCenter] postNotificationName:WxPayFailedNoti object:nil];
break;
}
}
}
</pre></code>
四抬探、遇到的問題
- 出現(xiàn)白色按鈕子巾,沒有跳到支付頁面
-> 應(yīng)該是前面簽名參數(shù)不對(duì), request.sign 與 微信服務(wù)器算出的不一致