今天對第三方支付作了一個小小的封裝,框架的導入與配置這裏就很少說了,能夠去看集成文檔。直接上封裝的demo微信
使用方式:[[SKAliPayTool sharedSKAliPayTool] goPayByPayType:payName
andOrderNum:self.orderNumber
andTitle:self.orderNumber
andDescribetion:@"測試"
andPrice:[self.goodsDictionary safeJsonObjForKey:@"FactPrice"]];
[SKAliPayTool sharedSKAliPayTool].paySuccess = ^{
};
[SKAliPayTool sharedSKAliPayTool].payFailed = ^{
};
app
首先.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];
}
}
atom
支付成功直接調用: [SKAliPayTool sharedSKAliPayTool].paySuccess();
debug
移動端熱門技術交流羣170229489code