美文网首页
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海外支付最详细流程和代码

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