https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.8bz1nF&treeId=193&articleId=105295&docType=1
1.在Build Phases選項(xiàng)卡的Link Binary With Libraries中俊扳,增加以下依賴:
2.拷貝文件到項(xiàng)目文件夾下,如
3.頭文件
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AlipaySDK/AlipaySDK.h>
#import "Order.h"
#import "APAuthV2Info.h"
#import "DataSigner.h"
4.組裝請求信息扒最。
//將商品信息賦予AlixPayOrder的成員變量
Order* order = [Order new];
// NOTE: app_id設(shè)置
order.app_id = appID;
// NOTE: 支付接口名稱
order.method = @"alipay.trade.app.pay";
// NOTE: 參數(shù)編碼格式
order.charset = @"utf-8";
// NOTE: 當(dāng)前時間點(diǎn)
NSDateFormatter* formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
order.timestamp = [formatter stringFromDate:[NSDate date]];
// NOTE: 支付版本
order.version = @"1.0";
// NOTE: sign_type設(shè)置
order.sign_type = @"RSA";
// NOTE: 商品數(shù)據(jù)
order.biz_content = [BizContent new];
order.biz_content.body = @"我是測試數(shù)據(jù)";
order.biz_content.subject = @"1";
order.biz_content.out_trade_no = [self generateTradeNO]; //訂單ID(由商家自行制定)
order.biz_content.timeout_express = @"30m"; //超時時間設(shè)置
order.biz_content.total_amount = [NSString stringWithFormat:@"%.2f", 0.01]; //商品價格
//將商品信息拼接成字符串
NSString *orderInfo = [order orderInfoEncoded:NO];
NSString *orderInfoEncoded = [order orderInfoEncoded:YES];
NSLog(@"orderSpec = %@",orderInfo);
// NOTE: 獲取私鑰并將商戶信息簽名,外部商戶的加簽過程請務(wù)必放在服務(wù)端誓军,防止公私鑰數(shù)據(jù)泄露史汗;
// 需要遵循RSA簽名規(guī)范,并將簽名字符串base64編碼和UrlEncode
id<DataSigner> signer = CreateRSADataSigner(privateKey);
NSString *signedString = [signer signString:orderInfo];
// NOTE: 如果加簽成功脑溢,則繼續(xù)執(zhí)行支付
if (signedString != nil) {
//應(yīng)用注冊scheme,在AliSDKDemo-Info.plist定義URL types
NSString *appScheme = @"alisdkdemo";
// NOTE: 將簽名成功字符串格式化為訂單字符串,請嚴(yán)格按照該格式
NSString *orderString = [NSString stringWithFormat:@"%@&sign=%@",
orderInfoEncoded, signedString];
// NOTE: 調(diào)用支付結(jié)果開始支付
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
}];
}
5.運(yùn)行出現(xiàn)錯誤喻奥,出現(xiàn)rsa.h file not found 席纽,在header search path路徑解決后, 然后出現(xiàn)openssl/asn1.h file not found, 解決
$(SRCROOT)/項(xiàng)目名
$(PROJECT_DIR)/項(xiàng)目名/Utils/Alipay //openssl文件路徑