不能對不起我的幾個粉絲~趟卸,我決定更新一篇文章啦~
點擊支付寶 進(jìn)入下載demo及創(chuàng)建應(yīng)用的的平臺啦~當(dāng)然還有其他完全寫好了的,這就給你們點我--demo
創(chuàng)建應(yīng)用,挨個填寫信息即可凉夯,這個讓老板自己創(chuàng)去吧,創(chuàng)好了找他要賬號
當(dāng)然采幌,這些東西看起來太復(fù)雜了劲够,我得給你一個實際的項目,跟 后臺做過交互的休傍,成功調(diào)起了征绎,不然那些參數(shù)傳過來傳過去的,看著太過繁瑣磨取,還不如實際一些人柿,程序猿,不就喜歡最直接的么忙厌,哪來那些彎彎扭扭~~
這幾個東西凫岖,你懂的,先放進(jìn)去逢净,那個Alipay文件夾哥放,demo里面都有歼指,先下載了,拖進(jìn)去
在appledegate 導(dǎo)入#import "AlipaySDK/AlipaySDK.h"
并導(dǎo)入下面的代碼
<pre><code>
-(BOOL)application:(UIApplication *)app openURL:(NSURL )url options:(NSDictionary)options<code>
{
if([url.host isEqualToString:@"safepay"])
{
//跳轉(zhuǎn)支付寶錢包進(jìn)行支付甥雕,處理支付結(jié)果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
}];
}
return YES;
}
</code></pre>
現(xiàn)在就去viewcontroller 調(diào)用
先寫一個方法踩身,然后在點擊方法內(nèi)調(diào)用傳入價格商品名即可,這個時候跟后臺之前商品數(shù)據(jù)進(jìn)行對接即可社露。
<pre><code>
//支付寶付款
-(void)userpayWithMoney:(NSString *)money Productname:(NSString *)productname
{
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
NSString * resultStr = [user objectForKey:@"ordernum"];
Order *order = [[Order alloc] init];
order.partner = alipartner;//支付寶PID例@"2066511942711331"這里進(jìn)行了宏定義
order.seller = aliseller;//支付寶商家賬戶這里進(jìn)行了宏定義就是收錢那個賬號
order.tradeNO = resultStr; //訂單ID也就是訂單號惰赋,可以自己隨機(jī)生成傳給后臺,也可以后臺生成傳給展示數(shù)據(jù)呵哨,我這里是自己生成的赁濒,下面可以給你生成的代碼(由商家自行制定)
//必須要商品標(biāo)題
order.productName = productname; //商品標(biāo)題
NSString * pricestr = [money stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"¥"]];
float p = [pricestr floatValue];
order.amount = [NSString stringWithFormat:@"%.2f",p]; //商品價格
order.notifyURL = payURL; //回調(diào)URL這里進(jìn)行了宏定義,這個url找后臺要
//以下參數(shù)是固定的
order.service = @"mobile.securitypay.pay";
order.paymentType = @"1";
order.inputCharset = @"utf-8";
order.itBPay = @"30m";
order.showUrl = @"m.alipay.com";
//應(yīng)用注冊scheme,在AlixPayDemo-Info.plist定義URL types
NSString *appScheme = @"lalalla";//這個就自己寫上你自己的了
//將商品信息拼接成字符串
NSString *orderSpec = [order description];
NSLog(@"orderSpec = %@",orderSpec);
//獲取私鑰并將商戶信息簽名,外部商戶可以根據(jù)情況存放私鑰和簽名,只需要遵循RSA簽名規(guī)范,并將簽名字符串base64編碼和UrlEncode
id<DataSigner> signer = CreateRSADataSigner(aliprivateKey);//支付寶私鑰 怎么獲取孟害,這里有詳解[私鑰獲取方法](http://blog.it985.com/12276.html)
NSString *signedString = [signer signString:orderSpec];
//將簽名成功字符串格式化為訂單字符串,請嚴(yán)格按照該格式
NSString *orderString = nil;
if (signedString != nil)
{
orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
dispatch_async(dispatch_get_main_queue(),^{
int success = [[resultDic objectForKey:@"resultStatus"] intValue] ;
if (success==9000)
{
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:@"支付成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * act = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
[self.navigationController pushViewController:[[OrderController alloc]init] animated:YES];
}];
[alertView addAction:act];
[self presentViewController:alertView animated:YES completion:nil];
}
else if (success==6001)
{
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:@"支付取消" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * act = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
[alertView addAction:act];
[self presentViewController:alertView animated:YES completion:nil];
}
else
{
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:@"支付失敗" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * act = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
[alertView addAction:act];
[self presentViewController:alertView animated:YES completion:nil];
}
});
}];
}
}
</code></pre>