美文网首页
iOS-48-Stripe海外支付最详细流程和代码

iOS-48-Stripe海外支付最详细流程和代码

作者: 小东门儿 | 来源:发表于2020-04-01 10:22 被阅读0次

海外支付需求

我们需要使用海外支付进行银行卡的管理和支付功能。给我们的要求是使用Stripe,可以使用其提供的管理页面。可以使用银行卡列表页面、添加页面等。

第一步:在appdelegate中注册key

[Stripe setDefaultPublishableKey:stripeKey];

stripeKey为key,注意key可以设置测试环境和线上环境的key。

第二步:在需要的页面唤起银行卡列表页

STPPaymentConfiguration *config = [STPPaymentConfiguration sharedConfiguration];
config.additionalPaymentOptions = STPPaymentOptionTypeNone;
config.requiredBillingAddressFields = STPBillingAddressFieldsNone; config.publishableKey = stripeKey;
StripeAPIClient *manager = [StripeAPIClient sharedAPIClient];
STPCustomerContext *customerContext = [[STPCustomerContext alloc] initWithKeyProvider:manager];
STPPaymentOptionsViewController * vc = [[STPPaymentOptionsViewController alloc]initWithConfiguration:config theme:[STPTheme defaultTheme] customerContext:customerContext delegate:[NGPayManager sharedInstance]];
[[LYRouterManager getcurController].navigationController pushViewController:vc animated:YES];

StripeAPIClient为引用STPCustomerEphemeralKeyProvider的协议类,为了stripe去进行验证操作

StripeAPIClient.h

#import <Foundation/Foundation.h>
#import <Stripe/Stripe.h>


@interface StripeAPIClient : NSObject<STPCustomerEphemeralKeyProvider>

/**
 初始化Stripe用户信息
 */
+ (instancetype)sharedAPIClient;


/**
 STPEphemeralKeyProvider 代理方法,初始化后会自动调用

 @param apiVersion 当前API的版本
 @param completion 成功回调
 */
- (void)createCustomerKeyWithAPIVersion:(NSString *)apiVersion completion:(STPJSONResponseCompletionBlock)completion;


@end

StripeAPIClient.m

#import "StripeAPIClient.h"
#import <Stripe/Stripe.h>

static StripeAPIClient *_manager = nil;

@interface StripeAPIClient()

@end

@implementation StripeAPIClient


+ (instancetype)sharedAPIClient{
  if (!_manager) {
    _manager = [[StripeAPIClient alloc] init];
  }
  return _manager;
}

- (void)createCustomerKeyWithAPIVersion:(NSString *)apiVersion completion:(STPJSONResponseCompletionBlock)completion{
    
    [NGOrderSeverManager postAPICardEphemeralKeyWithdic:@{} Success:^(id  _Nonnull responseObject) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NGHideHud;
        });
        NSDictionary * tmpDic = (NSDictionary *)responseObject;
        completion(tmpDic,nil);
    } failure:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NGHideHud;
            NGShowMessage([error localizedDescription]);
        });
    }];
} 
@end
- (void)createCustomerKeyWithAPIVersion:(NSString *)apiVersion completion:(STPJSONResponseCompletionBlock)completion

stripe的验证是通过这个回调完成的

其中调用自己后台服务器的接口去绑定个人信息和银行卡信息的关系,返回给客户端相关数据提供给stripe的sdk。

{
    "associated_objects" =     (
                {
            id = "";
            type = customer;
        }
    );
    created = 1585708749;
    expires = 1585712349;
    id = "";
    livemode = 1;
    object = "ephemeral_key";
    secret = "";
}

后台的返回样式是这样。

选择银行卡的代理回调

#pragma mark delegate
- (void)paymentOptionsViewController:(STPPaymentOptionsViewController *)paymentOptionsViewController
              didFailToLoadWithError:(NSError *)error{
    
}

- (void)paymentOptionsViewControllerDidCancel:(STPPaymentOptionsViewController *)paymentOptionsViewController{
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [NGPayManager sharedInstance].bankCardSelectedBlock(dic);
    [paymentOptionsViewController.navigationController popViewControllerAnimated:YES];
}

- (void)paymentOptionsViewControllerDidFinish:(STPPaymentOptionsViewController *)paymentOptionsViewController{
    
}

- (void)paymentOptionsViewController:(STPPaymentOptionsViewController *)paymentOptionsViewController didSelectPaymentOption:(id<STPPaymentOption>)paymentOption{
    STPPaymentMethod * paymentMethod = (STPPaymentMethod *)paymentOption;
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setObject:paymentMethod.stripeId forKey:@"payCardId"];
    NSString * brand = @"";
    switch (paymentMethod.card.brand) {
        case STPCardBrandVisa:
            brand = @"Visa";
            break;
        case STPCardBrandJCB:
            brand = @"JCB";
            break;
        case STPCardBrandAmex:
            brand = @"Amex";
            break;
        case STPCardBrandUnknown:
            brand = @"Unknown";
            break;
        case STPCardBrandDiscover:
            brand = @"Discover";
            break;
        case STPCardBrandUnionPay:
            brand = @"UnionPay";
            break;
        case STPCardBrandDinersClub:
            brand = @"DinersClub";
            break;
        case STPCardBrandMasterCard:
            brand = @"MasterCard";
            break;
        default:
            break;
    }
    [dic setObject:brand forKey:@"brand"];
    [dic setObject:paymentMethod.card.last4 forKey:@"last4"];
    [dic setObject:[NSNumber numberWithInteger:paymentMethod.card.expMonth] forKey:@"expMonth"];
    [dic setObject:[NSNumber numberWithInteger:paymentMethod.card.expYear] forKey:@"expYear"];
    [NGPayManager sharedInstance].bankCardSelectedBlock(dic);
    [paymentOptionsViewController.navigationController popViewControllerAnimated:YES];
    
}

选择完银行卡后进行支付操作

将选择的银行卡信息提交给后台,其中需要的是paymentMethod.stripeId参数

后台将stripe需要的支付信息返回给客户端,客户端拉起支付操作。

+ (void)bankpayWithOrderDic:(NSDictionary *)orderDic callback:(NGPayCompletionBlock)completionBlock{
    // Collect card details
    STPPaymentIntentParams *paymentIntentParams = [[STPPaymentIntentParams alloc] initWithClientSecret:[CommonTools getStringWithDic:orderDic key:@"clientSecret"]];
    paymentIntentParams.paymentMethodId = [CommonTools getStringWithDic:orderDic key:@"paymentMethodId"];
    // Submit the payment
    STPPaymentHandler *paymentHandler = [STPPaymentHandler sharedHandler];
    [paymentHandler confirmPayment:paymentIntentParams withAuthenticationContext:[NGPayManager sharedInstance] completion:^(STPPaymentHandlerActionStatus status, STPPaymentIntent *paymentIntent, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            switch (status) {
                case STPPaymentHandlerActionStatusFailed: {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        NGHideHud;
                        NGShowMessage([error localizedDescription]);
                        completionBlock(@{@"success":@"0"});
                    });
                    break;
                }
                case STPPaymentHandlerActionStatusCanceled: {
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        NGHideHud;
                        NGShowMessage([error localizedDescription]);
                        completionBlock(@{@"success":@"1"});
                    });
                    break;
                }
                case STPPaymentHandlerActionStatusSucceeded: {
                    
                    completionBlock(@{@"success":@"2"});
                    break;
                }
                default:
                    break;
            }
        });
    }];
}

选择控制器

- (UIViewController *)authenticationPresentingViewController {
    return [LYRouterManager getcurController];
} 

支付完成

进行订单查询即可

相关文章

  • iOS-48-Stripe海外支付最详细流程和代码

    海外支付需求 我们需要使用海外支付进行银行卡的管理和支付功能。给我们的要求是使用Stripe,可以使用其提供的管理...

  • 支付中心之支付篇

    支付是支付中心最核心也是最负责的功能呢,今天从用户支付的角度和大家一起探讨支付流程的设计。 文章目录 如何理解业务...

  • M1芯片 xcode13.3 shell自动化iOS打包

    代码地址和详细流程:https://gitee.com/yuanlang068/app-shell-demo.gi...

  • 支付宝接入支付集成总结

    支付宝接入支付集成总结 我会详细的记录每一步,以便自己以后查看复习,包括:非代码部分、代码部分 配置 支付宝集成文...

  • 开源项目收集

    Springboot开源项目收集 支付服务 简介:支付服务:支付宝、微信、银联详细 代码案例,目前已经1800+S...

  • Spring MVC+Spring+Mybatis实现支付宝支付

    前言 本教程详细介绍了如何使用ssm框架实现支付宝支付功能。本文章分为两大部分,分别是「支付宝测试环境代码测试」和...

  • 基于nodejs后端微信支付接口

    本文不谈框架,不谈代码组织结构,只谈怎么实现, 1,微信移动端支付流程如下: 这个流程可以微信app支付文档找到,...

  • 微信支付开发IOS图文教程案例

    前言:下面介绍微信支付的开发流程的细节,图文并茂,你可以按照我的随笔流程过一遍代码。包你也学会了微信支付。而且支付...

  • 网站集成微信支付-签名生成

    微信支付相比于支付宝来说要复杂的多,说多了都是泪。 直接贴代码,代码注释很详细。包括accsii排序,以及MD5的...

  • 微信APP支付和退款(JAVA)

    微信APP支付和退款 微信支付流程说明 Java demo实例 退款 转账 1、微信支付 1 微信支付流程说明 本...

网友评论

      本文标题:iOS-48-Stripe海外支付最详细流程和代码

      本文链接:https://www.haomeiwen.com/subject/nfrouhtx.html