iOS支付宝和微信支付

最近在做iOS端的支付,包括支付宝支付和微信支付,今天就来记录一下

一、准备

  • 关于申请的事交给后端吧,前段需要的是把下面的宏写在pch里面
宏.png
配置.png
image.png

二、微信注册

  • 在AppDelegate里的这个方法里注册
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  • 因为要加入友盟推送,所以是可以直接用友盟的方法
    这个方法注册了,可以调用友盟分享和微信支付

// 设置微信的appKey和appSecret
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WXAPP_ID appSecret:WXAPP_KEY redirectURL:nil];

  • 如果不需要友盟的情况
    [WXApi registerApp:WXAPP_ID];

三、拿到后台返回的数据

  • 后台有一个【预支付】过程会返回iOS端需要的东西
  • 除了上面给的宏定义,其他【所有数据】都由后台返回,这样保证前端的支付安全
1、微信需要返回的数据
@property (nonatomic, strong) NSString *totalprise;
@property (nonatomic, strong) NSString *order_id;
@property (nonatomic, strong) NSString *noncestr;
@property (nonatomic, strong) NSString *partnerid;
@property (nonatomic, strong) NSString *timestamp;
@property (nonatomic, strong) NSString *package;
@property (nonatomic, strong) NSString *prepayid;
@property (nonatomic, strong) NSString *sign;
@property (nonatomic, strong) NSString *agent;
@property (nonatomic, strong) NSString *appid;
2、支付宝需要返回的数据

@property (nonatomic, strong) NSString *ali_data;

【特别说明】【特别说明】【特别说明】

【后台】得到下面的这一坨要原封不动的给iOS端,支付宝的文档和demo都是取出来,然后后面一堆【没用】操作,虽有支付宝文档有提示,但是应该大字提醒呀!!!

“ali_data” = “alipay_sdk=alipay-sdk-java-3.3.1&app_id=2018110762076222&biz_content=%7B%22body%22%3A%22%E6%82%A8%E8%B4%AD%E4%B9%B0%EF%BC%9A18K%E8%9C%BB%E8%9C%93%EF%BC%8C%E6%94%AF%E4%BB%98%E9%87%91%E9%A2%9D%EF%BC%9A0.01%E5%85%83%22%2C%22goods_type%22%3A%221%22%2C%22out_trade_no%22%3A%221542256247438%22%2C%22passback_params%22%3A%22201811151230444328%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22seller_id%22%3A%222088331351165776%22%2C%22subject%22%3A%22%E6%94%AF%E4%BB%98%E8%AE%A2%E5%8D%95%22%2C%22timeout_express%22%3A%2229m%22%2C%22total_amount%22%3A%220.01%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay¬ify_url=http%3A%2F%2Fapp.qingwukeji.com%2Fpay%2Falipay%2Fcallback_url.do&sign=NTn4sYBGTkTEK5%2F7U11ZFnyKIMt3GmqHDmNIKp5nH1lXCIiBc4T3GllS659I0LLPC5LFidSVj6jU89aVn9%2B0fR4prUcht5mdw%2Bjn3JrnzbOty%2F9PE1rbk0PX3b2KoXymejQvuykefpUodWTlkHg5JiEncLCXNm7D9Ykacb3K%2B1KfMuOArPtUtwRifGeRu4QjHlqwcPFvwZlhfebffowu0ytubWre2%2FtX%2FNcB4OTOopKzxyko6M6T0NaHqsASnJFDTmtEFUU06WYoHXWhoPADAGlr80sxeSZKcUvgspseVN0XEU806bemhf4Qy09D0Pj3RWsDAXHk5kap8inYa9aPGA%3D%3D&sign_type=RSA2×tamp=2018-11-15+12%3A30%3A47&version=1.0”

四、支付

在要支付的界面
1、支付宝支付
 - (void)aliPay { 
     [[AlipaySDK defaultService] payOrder:ali_data fromScheme:AliAPP_ID callback:nil];
}
2、微信支付
- (void)weiChatPay {
    PayReq *request = [[PayReq alloc] init];
    request.openID = self.payModel.appid;
    request.partnerId = self.payModel.partnerid;
    request.nonceStr = self.payModel.noncestr;
    request.package = self.payModel.package;
    request.prepayId = self.payModel.prepayid;
    request.timeStamp = (UInt32)[self.payModel.timestamp intValue];
    request.sign = self.payModel.sign;
}

五、回调

支付成功之后需要返回自己的app
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)option {
    if ([[NSString stringWithFormat:@"%@",url] rangeOfString:[NSString stringWithFormat:@"%@://pay",WXAPP_ID]].location != NSNotFound) {
        // 从微信返回到自己的app执行这步
        return  [WXApi handleOpenURL:url delegate:“自己操作接收”];
    }else if ([url.host isEqualToString:@"safepay"]) {
        // 从支付宝返回到自己的app执行这步
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
           // 自己操作接收
        return YES;
    }else{
        return YES;
    }
}

六、废话

支付当中会遇到很多问题,这需要和后端慢慢的测,一起抽出一个固定的时间配合,因为有时真的不知道是哪一端哪个环节出了问题,就酱

你可能感兴趣的:(iOS支付宝和微信支付)