普通支付宝: 海淘支付宝
集成SDK
- 官网下载SDK和Demo(http://open.alipay.com/platform/document.htm#down)
- 往自己项目中添加支付宝相关文件:AlipaySDK.bundle,AlipaySDK.framework,openssl,Util
- 添加支付宝依赖类库:libcrypto.a,libssl.a,SystemConfiguration.framework,QuartzCore.framework,libz.dylib,CoreText.framework,CoreTelephony.framework,Foundation.framework,CoreGraphics.framework,UIKit.framework
普通支付宝
- order.h
@interface Order : NSObject
@property(nonatomic, copy) NSString * partner;
@property(nonatomic, copy) NSString * seller;
@property(nonatomic, copy) NSString * tradeNO;
@property(nonatomic, copy) NSString * productName;
@property(nonatomic, copy) NSString * productDescription;
@property(nonatomic, copy) NSString * amount;
@property(nonatomic, copy) NSString * notifyURL;
@property(nonatomic, copy) NSString * service;
@property(nonatomic, copy) NSString * paymentType;
@property(nonatomic, copy) NSString * inputCharset;
@property(nonatomic, copy) NSString * itBPay;
@property(nonatomic, copy) NSString * showUrl;
@property(nonatomic, copy) NSString * rsaDate;//可选
@property(nonatomic, copy) NSString * appID;//可选
@property(nonatomic, readonly) NSMutableDictionary * extraParams;
@end
- order.m 只有一个方法,description,把所有的参数,按照顺序,拼接起来
@implementation Order
- (NSString *)description {
NSMutableString * discription = [NSMutableString string];
if (self.partner) {
[discription appendFormat:@"partner=\"%@\"", self.partner];
}
if (self.seller) {
[discription appendFormat:@"&seller_id=\"%@\"", self.seller];
}
if (self.tradeNO) {
[discription appendFormat:@"&out_trade_no=\"%@\"", self.tradeNO];
}
if (self.productName) {
[discription appendFormat:@"&subject=\"%@\"", self.productName];
}
if (self.productDescription) {
[discription appendFormat:@"&body=\"%@\"", self.productDescription];
}
if (self.amount) {
[discription appendFormat:@"&total_fee=\"%@\"", self.amount];
}
if (self.notifyURL) {
[discription appendFormat:@"¬ify_url=\"%@\"", self.notifyURL];
}
if (self.service) {
[discription appendFormat:@"&service=\"%@\"",self.service];//mobile.securitypay.pay
}
if (self.paymentType) {
[discription appendFormat:@"&payment_type=\"%@\"",self.paymentType];//1
}
if (self.inputCharset) {
[discription appendFormat:@"&_input_charset=\"%@\"",self.inputCharset];//utf-8
}
if (self.itBPay) {
[discription appendFormat:@"&it_b_pay=\"%@\"",self.itBPay];//30m
}
if (self.showUrl) {
[discription appendFormat:@"&show_url=\"%@\"",self.showUrl];//m.alipay.com
}
if (self.rsaDate) {
[discription appendFormat:@"&sign_date=\"%@\"",self.rsaDate];
}
if (self.appID) {
[discription appendFormat:@"&app_id=\"%@\"",self.appID];
}
for (NSString * key in [self.extraParams allKeys]) {
[discription appendFormat:@"&%@=\"%@\"", key, [self.extraParams objectForKey:key]];
}
return discription;
}
@end
海淘支付宝
- 和普通支付宝区别的参数:参数名相同,值不同:partner,seller,notifyURL;参数名不相同:(普通对应amount)rmb_fee,forex_biz,currency,customs_place
- order.h
@interface Order : NSObject
@property(nonatomic, copy) NSString * partner;
@property(nonatomic, copy) NSString * seller;
@property(nonatomic, copy) NSString * tradeNO;
@property(nonatomic, copy) NSString * productName;
@property(nonatomic, copy) NSString * productDescription;
@property(nonatomic, copy) NSString * notifyURL;
@property(nonatomic, copy) NSString * service;
@property(nonatomic, copy) NSString * paymentType;
@property(nonatomic, copy) NSString * inputCharset;
@property(nonatomic, copy) NSString * itBPay;
@property(nonatomic, copy) NSString * showUrl;
@property(nonatomic, copy) NSString * rsaDate;//可选
@property(nonatomic, copy) NSString * appID;//可选
//海淘
@property(nonatomic, copy) NSString * rmb_fee;
@property(nonatomic, copy) NSString * currency;//币种
@property(nonatomic, copy) NSString * forex_biz;
@property(nonatomic, copy) NSString * customs_place;
@property(nonatomic, readonly) NSMutableDictionary * extraParams;
@end
- order.m
@implementation Order
- (NSString *)description {
NSMutableString * discription = [NSMutableString string];
if (self.partner) {
[discription appendFormat:@"partner=\"%@\"", self.partner];
}
if (self.seller) {
[discription appendFormat:@"&seller_id=\"%@\"", self.seller];
}
if (self.tradeNO) {
[discription appendFormat:@"&out_trade_no=\"%@\"", self.tradeNO];
}
if (self.productName) {
[discription appendFormat:@"&subject=\"%@\"", self.productName];
}
if (self.productDescription) {
[discription appendFormat:@"&body=\"%@\"", self.productDescription];
}
if (self.amount) {
[discription appendFormat:@"&total_fee=\"%@\"", self.amount];
}
if (self.notifyURL) {
[discription appendFormat:@"¬ify_url=\"%@\"", self.notifyURL];
}
if (self.service) {
[discription appendFormat:@"&service=\"%@\"",self.service];//mobile.securitypay.pay
}
if (self.paymentType) {
[discription appendFormat:@"&payment_type=\"%@\"",self.paymentType];//1
}
if (self.customs_place) {
[discription appendFormat:@"&forex_param=\"%@\"",self.customs_place];
}
if (self.inputCharset) {
[discription appendFormat:@"&_input_charset=\"%@\"",self.inputCharset];//utf-8
}
if (self.itBPay) {
[discription appendFormat:@"&it_b_pay=\"%@\"",self.itBPay];//30m
}
if (self.showUrl) {
[discription appendFormat:@"&show_url=\"%@\"",self.showUrl];//m.alipay.com
}
if (self.rsaDate) {
[discription appendFormat:@"&sign_date=\"%@\"",self.rsaDate];
}
if (self.appID) {
[discription appendFormat:@"&app_id=\"%@\"",self.appID];
}
if (self.rmb_fee) {
[discription appendFormat:@"&rmb_fee=\"%@\"",self.rmb_fee];
}
if (self.currency) {
[discription appendFormat:@"¤cy=\"%@\"",self.currency];
}
if (self.forex_biz) {
[discription appendFormat:@"&forex_biz=\"%@\"",self.forex_biz];
}
for (NSString * key in [self.extraParams allKeys]) {
[discription appendFormat:@"&%@=\"%@\"", key, [self.extraParams objectForKey:key]];
}
return discription;
}
@end
支付宝&&海淘支付宝
- order.h
@interface Order : NSObject
@property(nonatomic, copy) NSString * partner;
@property(nonatomic, copy) NSString * seller;
@property(nonatomic, copy) NSString * tradeNO;
@property(nonatomic, copy) NSString * productName;
@property(nonatomic, copy) NSString * productDescription;
@property(nonatomic, copy) NSString * amount;
@property(nonatomic, copy) NSString * notifyURL;
@property(nonatomic, copy) NSString * service;
@property(nonatomic, copy) NSString * paymentType;
@property(nonatomic, copy) NSString * inputCharset;
@property(nonatomic, copy) NSString * itBPay;
@property(nonatomic, copy) NSString * showUrl;
@property(nonatomic, copy) NSString * rsaDate;//可选
@property(nonatomic, copy) NSString * appID;//可选
//海淘
@property(nonatomic, copy) NSString * rmb_fee;
@property(nonatomic, copy) NSString * currency;//币种
@property(nonatomic, copy) NSString * forex_biz;
@property(nonatomic, copy) NSString * customs_place;
@property(nonatomic, readonly) NSMutableDictionary * extraParams;
@end
- order.m
@implementation Order
- (NSString *)description {
NSMutableString * discription = [NSMutableString string];
if (self.partner) {
[discription appendFormat:@"partner=\"%@\"", self.partner];
}
if (self.seller) {
[discription appendFormat:@"&seller_id=\"%@\"", self.seller];
}
if (self.tradeNO) {
[discription appendFormat:@"&out_trade_no=\"%@\"", self.tradeNO];
}
if (self.productName) {
[discription appendFormat:@"&subject=\"%@\"", self.productName];
}
if (self.productDescription) {
[discription appendFormat:@"&body=\"%@\"", self.productDescription];
}
if (self.amount) {
[discription appendFormat:@"&total_fee=\"%@\"", self.amount];
}
if (self.notifyURL) {
[discription appendFormat:@"¬ify_url=\"%@\"", self.notifyURL];
}
if (self.service) {
[discription appendFormat:@"&service=\"%@\"",self.service];//mobile.securitypay.pay
}
if (self.paymentType) {
[discription appendFormat:@"&payment_type=\"%@\"",self.paymentType];//1
}
if (self.customs_place) {
[discription appendFormat:@"&forex_param=\"%@\"",self.customs_place];
}
if (self.inputCharset) {
[discription appendFormat:@"&_input_charset=\"%@\"",self.inputCharset];//utf-8
}
if (self.itBPay) {
[discription appendFormat:@"&it_b_pay=\"%@\"",self.itBPay];//30m
}
if (self.showUrl) {
[discription appendFormat:@"&show_url=\"%@\"",self.showUrl];//m.alipay.com
}
if (self.rsaDate) {
[discription appendFormat:@"&sign_date=\"%@\"",self.rsaDate];
}
if (self.appID) {
[discription appendFormat:@"&app_id=\"%@\"",self.appID];
}
if (self.rmb_fee) {
[discription appendFormat:@"&rmb_fee=\"%@\"",self.rmb_fee];
}
if (self.currency) {
[discription appendFormat:@"¤cy=\"%@\"",self.currency];
}
if (self.fp) {
[discription appendFormat:@"&forex_biz=\"%@\"",self.forex_biz];
}
for (NSString * key in [self.extraParams allKeys]) {
[discription appendFormat:@"&%@=\"%@\"", key, [self.extraParams objectForKey:key]];
}
return discription;
}
@end
调启支付宝
- 生成订单信息
//将商品信息拼接成字符串
NSString *orderSpec = [order description];
NSLog(@"orderSpec = %@",orderSpec);
- 对订单信息签名
//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
id<DataSigner> signer = CreateRSADataSigner(privateKey);
NSString *signedString = [signer signString:orderSpec];
- 拼出支付宝所需订单字符串
NSString *orderString = nil;
orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];
- 设置appscheme
//商户程序注册的URL protocol,供支付完成后回调商户程序使用。一般为app的bundleid
NSString *appScheme = @"com.text.xxx";
- 调启支付宝
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic)
{}];
处理回调
手机上没有支付宝
- 支付并通过回调返回结果
- resultDic里面有两个键值对{resultStatus:6001,memo:@”“}由于momo对应的是空串,支付宝支付结果字符串,只能通过状态吗,手动添加上
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic)
{
strmessage = [resultDic objectForKey:@"memo"]; //为空串@“”
statusCode = [resultDic objectForKey:@"resultStatus"];
if (!strmessage.length) {
switch (statusCode.intValue) {
case 6001:
strmessage = @"用户中途取消";
break;
case 6002:
strmessage = @"网络连接出错";
break;
case 4000:
strmessage = @"订单支付失败";
break;
case 8000:
strmessage = @"正在处理中";
break;
case 9000:
strmessage = @"支付成功";
break;
default:
break;
}
}
手机上有支付宝
- 回调方法走这里:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
- 状态码和操作提示字符串
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([url.host isEqualToString:@"safepay"])
{
//url 支付宝客户端回传的url
// 处理客户端返回url
[[AlipaySDK defaultService]
processOrderWithPaymentResult:url
standbyCallback:^(NSDictionary *resultDic)//当支付宝客户端在操作时,商户app进程在后台被结束,只能通过这个block输出支付结果。
{
NSString *codeDesc= [resultDic objectForKey:@"memo"];
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示"
message:codeDesc
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alertView show];
}];
}