#import <Foundation/Foundation.h> @class Order; typedef void (^ResultBlock)(Order *resultOrder); @interface AlipayService : NSObject +(instancetype)shareService; /** * 支付动作,跳转到支付客户端 * * @param sourceOrder 支付定单 * @param resultBlock 结果定单 */ -(void)payWithOrder:(Order *)sourceOrder resultBlock:(ResultBlock)resultBlock; /** * 处理结果的URL */ -(void)handleURL:(NSURL *)url; @end
#import "AlipayService.h" #import "Order.h" #import <UIKit/UIKit.h> @interface AlipayService(){ ResultBlock _resultBlock;//结果回调block } @end @implementation AlipayService +(instancetype)shareService{ return [[self alloc] init]; } +(instancetype)allocWithZone:(struct _NSZone *)zone{ static AlipayService *service = nil; static dispatch_once_t onceToken; #warning 线程安全 dispatch_once(&onceToken, ^{ service = [super allocWithZone:zone]; }); return service; } -(void)payWithOrder:(Order *)sourceOrder resultBlock:(ResultBlock)resultBlock{ //1.先存储结果block _resultBlock = resultBlock; //2.支付 //2.把定单属性拼接成orderDesc=124234&amount=1234 #warning 参数在网络的传输入过程都是字符串 NSString *params = [sourceOrder query]; NSLog(@"%@",params); NSString *alipayURLStr = [@"alipay2://payment?" stringByAppendingString:params]; #warning URL?号后面不能有中文,要进行URL Encode alipayURLStr = [alipayURLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //跳转到支付宝客户端 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:alipayURLStr]]; } -(void)handleURL:(NSURL *)url{ //作处理 NSLog(@"%s 处理支付结果",__func__); //url后面参数封装成一个结果Order //1.截取url的参数 NSString *query = [url query]; query = [query stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //转模型 Order *resultOrder = [Order orderWithQuery:query]; //回调block if (_resultBlock) { _resultBlock(resultOrder); } } @end
#import "AppDelegate.h" #import "AlipayService.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. return YES; } -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ NSLog(@"支付结果%@",url); [[AlipayService shareService] handleURL:url]; return YES; } @end
/** * * 定单模型 */ #import <Foundation/Foundation.h> @interface Order : NSObject /** * 定单的描述 (iWatch) */ @property (nonatomic,copy)NSString *orderDesc ; /** * 支付金额 */ @property (nonatomic,copy)NSString *amount ; /** * 定单编号 (客户端自己定义) 20150426130323 */ @property (nonatomic,copy)NSString *orderNum ; /** * 定单状态 1:代表支付成功 0: 支付失败 */ @property (nonatomic,copy)NSString *status ; /** * 支付类型; 1.支付 2.退款 */ @property (nonatomic,copy)NSString *paymentType ; /** * 回调到本应用的URL */ @property (nonatomic,copy)NSString *appCallbackURL ; /** * query 是URL 问号?后面参数 * 把定单属性拼接成orderDesc=124234&amount=1234 */ -(NSString *)query; /** * 通过query返回一个Order模型 * */ +(Order *)orderWithQuery:(NSString *)query; @end
// // Order.m // B01_电商APP // // Created by apple on 15/4/26. // Copyright (c) 2015年 itcast. All rights reserved. // #import "Order.h" @implementation Order -(NSString *)query{ /* •orderDesc : 定单的描述 (iWatch) •amount : 支付金额 •orderNum : 定单编号 (客户端自己定义) 20150426130323 •status : 定单状态 1:代表支付成功 0:支付失败 •paymentType : 支付类型: 1.支付 2.退款*/ NSMutableString *strM = [NSMutableString string]; [strM appendFormat:@"orderDesc=%@&",self.orderDesc]; [strM appendFormat:@"amount=%@&",self.amount]; [strM appendFormat:@"orderNum=%@&",self.orderNum]; [strM appendFormat:@"status=%@&",self.status]; [strM appendFormat:@"appCallbackURL=%@&",self.appCallbackURL]; [strM appendFormat:@"paymentType=%@",self.paymentType]; return [strM copy]; } +(Order *)orderWithQuery:(NSString *)query{ Order *order = [[Order alloc] init]; //orderDesc=iWatch智能手表&amount=7000&orderNum=201523412342134&status=0&paymentType=1 NSArray *arr = [query componentsSeparatedByString:@"&"]; NSLog(@"%@",arr); for (NSString *item in arr) { //"orderDesc=iWatch\U667a\U80fd\U624b\U8868", NSArray *kv = [item componentsSeparatedByString:@"="]; [order setValue:kv[1] forKey:kv[0]]; } return order; } @end
@interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *productNameLabel; @property (weak, nonatomic) IBOutlet UILabel *priceLabel; /** * 支付结果 */ @property (weak, nonatomic) IBOutlet UILabel *resultLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (IBAction)payBtnClick { //支付定单 //数据传输 /* •orderDesc : 定单的描述 (iWatch) •amount : 支付金额 •orderNum : 定单编号 (客户端自己定义) 20150426130323 •status : 定单状态 1:代表支付成功 0:支付失败 •paymentType : 支付类型: 1.支付 2.退款 */ //1.创建定单 Order *order = [[Order alloc] init]; order.orderDesc = self.productNameLabel.text; order.amount = self.priceLabel.text; order.orderNum = @"201523412342134"; order.paymentType = @"1"; order.status = @"0"; //设置回调app的URL order.appCallbackURL = @"dsapp://result"; //支付,并通过block返回结果 [[AlipayService shareService] payWithOrder:order resultBlock:^(Order *resultOrder) { if ([resultOrder.status isEqualToString:@"1"]) { NSLog(@"支付成功"); self.resultLabel.text = @"支付成功"; }else{ NSLog(@"失败"); self.resultLabel.text = @"支付失败"; } }]; } #pragma mark 第一种支付方法 -(void)pay1{ //支付定单 //数据传输 /* •orderDesc : 定单的描述 (iWatch) •amount : 支付金额 •orderNum : 定单编号 (客户端自己定义) 20150426130323 •status : 定单状态 1:代表支付成功 0:支付失败 •paymentType : 支付类型: 1.支付 2.退款 */ //1.创建定单 Order *order = [[Order alloc] init]; order.orderDesc = self.productNameLabel.text; order.amount = self.priceLabel.text; order.orderNum = @"201523412342134"; order.paymentType = @"1"; order.status = @"0"; //设置回调app的URL order.appCallbackURL = @"dsapp://result"; //2.把定单属性拼接成orderDesc=124234&amount=1234 #warning 参数在网络的传输入过程都是字符串 NSString *params = [order query]; NSLog(@"%@",params); NSString *alipayURLStr = [@"alipay2://payment?" stringByAppendingString:params]; #warning URL?号后面不能有中文,要进行URL Encode alipayURLStr = [alipayURLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //跳转到支付宝客户端 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:alipayURLStr]]; } @end
时间: 2024-10-21 02:27:57