使用支付寶進行一個完整的支付功能,大致有以下步驟:
1.向支付寶申請, 與支付寶簽約,獲得商戶ID(partner)和賬號ID(seller)和私鑰(privateKey)
下載支付寶SDK
2.生成訂單信息,簽名加密
3.調(diào)用支付寶客戶端,由支付寶客戶端跟支付寶安全服務(wù)器打交道
4.支付完畢后,支付寶客戶端會自動跳回到原來的應(yīng)用程序
5.在原來的應(yīng)用程序中顯示支付結(jié)果給用戶看

屏幕快照 2017-07-03 上午9.56.26.png
#import "ViewController.h"
#import "Product.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>
@interface ViewController ()
/** 商品 */
@property (nonatomic, strong) NSArray *products;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.products.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 1.取出模型
Product *product = self.products[indexPath.row];
// 2.給Cell設(shè)置數(shù)據(jù)
cell.textLabel.text = product.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"價格:%.2f", product.price];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.取出模型
Product *product = self.products[indexPath.row];
// 2.購買商品
[self buyProduct:product];
}
- (void)buyProduct:(Product *)product
{
// 1.商戶申請時,會獲取的內(nèi)容
NSString *partner = @"";
NSString *seller = @"";
NSString *privateKey = @"";
// 2.生成訂單
Order *order = [[Order alloc] init];
order.partner = partner;
order.seller = seller;
order.tradeNO = nil; // 訂單ID(由商家自行制定)
order.productName = product.name; //商品標(biāo)題
order.productDescription = product.desc; //商品描述
order.amount = [NSString stringWithFormat:@"%.2f",product.price]; //商品價格
// 填寫服務(wù)器的回調(diào)地址
order.notifyURL = @"http://www.xxx.com"; //回調(diào)URL
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 = @"alipay";
//將商品信息拼接成字符串
NSString *orderSpec = [order description];
//獲取私鑰并將商戶信息簽名,外部商戶可以根據(jù)情況存放私鑰和簽名,只需要遵循RSA簽名規(guī)范,并將簽名字符串base64編碼和UrlEncode
id<DataSigner> signer = CreateRSADataSigner(privateKey);
NSString *signedString = [signer signString:orderSpec];
//將簽名成功字符串格式化為訂單字符串,請嚴(yán)格按照該格式
NSString *orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];
// 調(diào)用對應(yīng)SDK,打開支付寶客戶端,開始支付(如果用戶手機安裝客戶端了,那么打開客戶端進行支付.如果用戶沒有安裝客戶端,打開網(wǎng)頁讓用戶支付,如果有支付結(jié)果,在這里回調(diào))
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
}];
}
#pragma mark - 懶加載代碼
- (NSArray *)products
{
if (_products == nil) {
Product *product = [Product productWithPrice:11500.0 name:@"iPhone 4" desc:@"Phone 4已經(jīng)絕版了,所有非常貴"];
Product *product1 = [Product productWithPrice:11500.0 name:@"iMac土豪金" desc:@"土豪金看起來比較帥"];
Product *product2 = [Product productWithPrice:499.0 name:@"1T硬盤" desc:@"1T硬盤不要998,不要98,只要499"];
Product *product3= [Product productWithPrice:123000 name:@"iWatch鍍金" desc:@"裝x必備商品"];
_products = @[product, product1, product2, product3];
}
return _products;
}
@end