今天对第三方支付做了一个小小的封装,框架的导入与配置这里就不多说了,可以去看集成文档。直接上封装的demo
使用方式:[[SKAliPayTool sharedSKAliPayTool] goPayByPayType:payName
andOrderNum:self.orderNumber
andTitle:self.orderNumber
andDescribetion:@"测试"
andPrice:[self.goodsDictionary safeJsonObjForKey:@"FactPrice"]];
[SKAliPayTool sharedSKAliPayTool].paySuccess = ^{
};
[SKAliPayTool sharedSKAliPayTool].payFailed = ^{
};
首先.h文件
#import <Foundation/Foundation.h>
#import "SKSingle.h"
/**支付宝*/
#import <AlipaySDK/AlipaySDK.h>
#import "Order.h"
#import "DataSigner.h"
/**微信支付*/
#import "WXApi.h"
#import "payRequsestHandler.h"
/**y银联支付*/
#import "UPPaymentControl.h"
typedef void(^alipaySuccessBlock)();
typedef void(^alipayFailedBlock)();
@interface SKAliPayTool : NSObject
SKSingleH(SKAliPayTool)
/**支付宝支付*/
- (void)gopayForAli;
/**微信支付*/
- (void)gopayForWeChat;
/**综合方法*/
- (void)goPayByPayType:(NSString *)payType andOrderNum:(NSString *)orderNum andTitle:(NSString *)title andDescribetion:(NSString *)describe andPrice:(NSString *)price;
/**订单号*/
@property (nonatomic,copy) NSString *orderNum;
/**标题*/
@property (nonatomic,copy) NSString *title;
/**商品描述*/
@property (nonatomic,copy) NSString *describe;
/**付款价格*/
@property (nonatomic,copy) NSString *totalPrice;
/**支付成功*/
@property (nonatomic,copy) alipaySuccessBlock paySuccess;
@property (nonatomic,copy) alipayFailedBlock payFailed;
@end
=======================================================================================================
.m文件
#import "SKAliPayTool.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>
@implementation SKAliPayTool
SKSingleM(SKAliPayTool)
#pragma mark - 支付宝支付
- (void)gopayForAli
{
[SKRequest isBackCache:NO];
[SKRequest SKGETRequest:@"GetWebAlipayConfig" requestUrl:SHOPPINGCART_URL WithReturnValeuBlock:^(SKBaseModel *model) {
[self didGotConfig:model];
} WithFailureBlock:^(NSInteger errorCode) {
[SKToast showMessage:@"获取配置信息失败"];
}];
}
- (void)didGotConfig:(SKBaseModel *)model
{
if ([model.Code intValue] != 0) {
[SKToast showMessage:model.Msg];
return;
}
/*
*商户的唯一的parnter和seller。
*签约后,支付宝会为每个商户分配一个唯一的 parnter 和 seller。
*需要填写商户app申请的
*/
NSString *partner = [[model.Info safeObjectAtIndex:0] stringObjectForKey:@"PartnerID"];
NSString *seller = [[model.Info safeObjectAtIndex:0] stringObjectForKey:@"SellerNo"];
NSString *privateKey = [[model.Info safeObjectAtIndex:0] stringObjectForKey:@"PrivateKey"];
//partner和seller获取失败,提示
if ([partner length] == 0 ||
[seller length] == 0 ||
[privateKey length] == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"缺少partner或者seller或者私钥。"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
return;
}
/*
*生成订单信息及签名
*/
//将商品信息赋予AlixPayOrder的成员变量
Order *order = [[Order alloc] init];
order.partner = partner;
order.sellerID = seller;
order.outTradeNO = self.orderNum; //订单ID(由商家自行制定)
order.subject = self.title; //商品标题
order.body = self.describe; //商品描述
// order.totalFee = [NSString stringWithFormat:@"%.2f",[self.totalPrice doubleValue]]; //商品价格
#warning 修改过。。。。
order.totalFee = @"0.01"; //商品价格
order.notifyURL = [NSString stringWithFormat:@"%@",[[model.Info safeObjectAtIndex:0] stringObjectForKey:@"NotifyUrl"]]; //回调URL
order.service = @"mobile.securitypay.pay";
order.paymentType = @"1";
order.inputCharset = @"utf-8";
order.itBPay = @"30m";
order.showURL = @"m.alipay.com";
//应用注册scheme,在AlixPayDemo-Info.plist定义URL types
NSString *appScheme = @"alisdkdemo";
//将商品信息拼接成字符串
NSString *orderSpec = [order description];
NSLog(@"orderSpec = %@",orderSpec);
//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
id<DataSigner> signer = CreateRSADataSigner(privateKey);
NSString *signedString = [signer signString:orderSpec];
//将签名成功字符串格式化为订单字符串,请严格按照该格式
NSString *orderString = nil;
if (signedString != nil) {
orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
DLog(@"reslut = %@",resultDic);
if ([resultDic[@"resultStatus"] isEqualToString:@"9000"]) {
self.paySuccess();
} else {
self.payFailed();
}
}];
}
}
#pragma mark - 微信支付
- (void)gopayForWeChat
{
//本地IP
NSString *UserIP =[NSString getPhoneIP];
//单号
NSString *orderNumber = self.orderNum;
//价格
NSString *price =[NSString stringWithFormat:@"%.2f",[self.totalPrice doubleValue]]; //商品价格
NSMutableDictionary *mdic= [NSMutableDictionary dictionaryWithCapacity:0];
[mdic safeSetObject:orderNumber forKey:@"OrderNum"];
[mdic safeSetObject:price forKey:@"Amount"];
[mdic safeSetObject:UserIP forKey:@"CreateIp"];
[SKRequest SKPOSTRequest:mdic methodName:@"GetWeiXinPrePayNum" requestUrl:SHOPPINGCART_URL ragumentName:@"StrTxt" WithReturnValeuBlock:^(SKBaseModel *model) {
[self weChatPaySuccess:model];
} WithFailureBlock:^(NSInteger errorCode) {
}];
}
- (void)weChatPaySuccess:(SKBaseModel *)model
{
if ([model.Code intValue] == 0) {
NSString *prePayOrderNum = [[model.Info safeObjectAtIndex:0]safeJsonObjForKey:@"WeiXinPrePayOrderNum"];
if (prePayOrderNum.length !=0 && [WXApi isWXAppInstalled])
{
//创建支付签名对象
payRequsestHandler *req = [[payRequsestHandler alloc]init];
//初始化支付签名对象
[req init:APP_ID mch_id:MCH_ID];
//设置密钥
[req setKey:PARTNER_ID];
NSString *phoneIP = [NSString getPhoneIP];//手机IP
NSString *TradeName = self.title;//商品名
NSString *PayMoney = [NSString stringWithFormat:@"%.2f",[self.totalPrice doubleValue]];//价格
NSString *prepayId = prePayOrderNum;//获取prepayId(预支付交易会话标识)
//获取到实际调起微信支付的参数后,在app端调起支付
NSMutableDictionary *dict = [req sendPay:TradeName price:PayMoney PhoneIP:phoneIP WeixinprepayId:prepayId];
if(dict == nil){
//错误提示
NSString *debug = [req getDebugifo];
NSLog(@"%@\n\n",debug);
}else{
NSLog(@"%@\n\n",[req getDebugifo]);
//[self alert:@"确认" msg:@"下单成功,点击OK后调起支付!"];
NSMutableString *stamp = [dict objectForKey:@"timestamp"];
//调起微信支付
PayReq* req = [[PayReq alloc] init];
req.openID = [dict objectForKey:@"appid"];
req.partnerId = [dict objectForKey:@"partnerid"];
req.prepayId = [dict objectForKey:@"prepayid"];
req.nonceStr = [dict objectForKey:@"noncestr"];
req.timeStamp = stamp.intValue;
req.package = [dict objectForKey:@"package"];
req.sign = [dict objectForKey:@"sign"];
[WXApi sendReq:req];
}
}
}
}
#pragma mark - 银联支付
- (void)gopayForYinLian
{
//单号
NSString *OrderNum = self.orderNum;
//价格
NSString *price = self.totalPrice; //商品价格
if (OrderNum.length !=0) {
NSMutableDictionary *mdic= [NSMutableDictionary dictionaryWithCapacity:0];
[mdic safeSetObject:OrderNum forKey:@"OrderNum"];
[mdic safeSetObject:price forKey:@"Amount"];
[SKRequest SKPOSTRequest:mdic methodName:@"GetYinLianPrePayNum" requestUrl:SHOPPINGCART_URL ragumentName:@"StrTxt" WithReturnValeuBlock:^(SKBaseModel *model) {
[self gopayYinlianSuccess:model];
} WithFailureBlock:^(NSInteger errorCode) {
}];
}
}
- (void)gopayYinlianSuccess:(SKBaseModel *)model
{
if ([model.Code intValue] == 0) {
NSString *tradeNo =[[model.Info objectAtIndex:0]objectForKey:@"YinLianPrePayOrderNum"];
if (tradeNo.length > 0)
{
// 银联支付需要传入一个viewController,这里先写nil ,待需要添加银联功能时需要测试,如果测试传入nil不可行,就在综合方法上添加一个参数,即viewController
[[UPPaymentControl defaultControl] startPay:tradeNo fromScheme:@"UPPayDemo" mode:YinLianCeShi viewController:nil]; // mode 支付环境
}
}
}
/**综合方法*/
- (void)goPayByPayType:(NSString *)payType andOrderNum:(NSString *)orderNum andTitle:(NSString *)title andDescribetion:(NSString *)describe andPrice:(NSString *)price
{
if ([payType isEqualToString:@"支付宝"]) {
self.orderNum = orderNum;
self.title = title;
self.describe = describe;
self.totalPrice = price;
[self gopayForAli];
} else if ([payType isEqualToString:@"微信支付"]){
self.orderNum = orderNum;
self.title = title;
self.describe = describe;
self.totalPrice = price;
[self gopayForWeChat];
} else if ([payType isEqualToString:@"银联支付"]){
self.orderNum = orderNum;
self.title = title;
self.describe = describe;
self.totalPrice = price;
[self gopayForYinLian];
}
}
支付成功直接调用: [SKAliPayTool sharedSKAliPayTool].paySuccess();