国际合作越来越多,如何国际化短视频源码(ios篇)

莫乐
2023-12-01

PayPal支付是一款常见的国际贸易支付工具,因此,接入paypal成为实现短视频app国际化的重要一步,之前写过安卓系统接入该支付功能的实现方法,今天讲讲IOS端短视频源码开发paypal支付功能的方式:

1.首先,短视频源码引入PayPal的SDK,我们这里用的是2.18.1的版本

#PayPal
pod 'PayPal-iOS-SDK','2.18.1'

2.在对应需要使用的.m文件中引用PayPal的头文件,初始化PayPal基本配置

//Paypal
#import <PayPalConfiguration.h>
#import <PayPalPayment.h>
#import <PayPalPaymentViewController.h>
#import <PayPalMobile.h>
@property (nonatomic, strong, readwrite) PayPalConfiguration *paypalConfiguration;
/*payPal支付*/
#pragma mark - PayPal

- (PayPalConfiguration *)paypalConfiguration {
    if (!_paypalConfiguration) {
        _paypalConfiguration = [[PayPalConfiguration alloc] init];
        _paypalConfiguration.merchantName = [PublicObj getAppName];
        _paypalConfiguration.acceptCreditCards = NO;//不支持信用卡
        _paypalConfiguration.payPalShippingAddressOption = PayPalShippingAddressOptionPayPal;
        _paypalConfiguration.languageOrLocale = lagType;
    }
    return _paypalConfiguration;
}

3.我们以ios端短视频源码购买钻石为例,当用户购买钻石选择PayPal支付后调用接口获取相应的支付信息

-(void)dopayPal{
    NSLog(@"paypal支付");
    [MBProgressHUD showMessage:@""];
    
    NSDictionary *subdic = @{
                             @"uid":[Config getOwnID],
                             @"changeid":[_seleDic valueForKey:@"id"],
                             @"coin":[_seleDic valueForKey:@"coin"],
                             @"money":[_seleDic valueForKey:@"money"]
                             };
    [YBToolClass postNetworkWithUrl:@"Charge.getPaypalOrder" andParameter:subdic success:^(int code, id  _Nonnull info, NSString * _Nonnull msg) {
        [MBProgressHUD hideHUD];
        if (code == 0) {
            shortDesc = [NSString stringWithFormat:@"%@%@",minstr([_seleDic valueForKey:@"coin"]),[common name_coin]];

            NSDictionary *dict = [info firstObject];
            //调起paypal支付
            [self selPaypalParameter:dict];
        }
        else{
            [MBProgressHUD showError:msg];
        }

    } fail:^{
        [MBProgressHUD hideHUD];

    }];
}

4.短视频源码调用PayPal的SDK,传入相应的信息,设置PayPal回调方法,判断是沙盒还是发布模式,传入相应的id信息,调用PayPalPayment进行支付

-(void)selPaypalParameter:(NSDictionary *)parameter{
    NSString *sandBox = minstr([parameter valueForKey:@"paypal_sandbox"]);//0-沙盒  1正式
    NSString *client_id = minstr([parameter valueForKey:@"product_clientid"]);
    NSString *paypal_sandbox_id = minstr([parameter valueForKey:@"sandbox_clientid"]);
    
    [PayPalMobile initializeWithClientIdsForEnvironments:@{PayPalEnvironmentProduction:client_id,PayPalEnvironmentSandbox:paypal_sandbox_id}];
    if ([sandBox isEqual:@"1"]) {
        [PayPalMobile preconnectWithEnvironment:PayPalEnvironmentProduction];
    }else{
        [PayPalMobile preconnectWithEnvironment:PayPalEnvironmentSandbox];
    }
    
    NSString *orderStr = minstr([parameter valueForKey:@"orderid"]);
    PayPalPayment *payment = [[PayPalPayment alloc] init];
    payment.amount = [[NSDecimalNumber alloc] initWithString:minstr([_seleDic valueForKey:@"money"])];
    payment.currencyCode = @"USD";
    payment.invoiceNumber = orderStr;
    payment.shortDescription = shortDesc;//minstr([parameter valueForKey:@"shortDesc"]);
    payment.custom = @"coin_charge";//钻石充值
    payment.items = nil;  // if not including multiple items, then leave payment.items as nil
    payment.paymentDetails = nil; // if not including payment details, then leave payment.paymentDetails as nil
    payment.intent = PayPalPaymentIntentSale;
    if (!payment.processable) {
        NSLog(@"-------------");
    }
    PayPalPaymentViewController *paymentViewController = [[PayPalPaymentViewController alloc] initWithPayment:payment configuration:self.paypalConfiguration delegate:self];
    [[MXBADelegate sharedAppDelegate].topViewController presentViewController:paymentViewController animated:YES completion:nil];

}
#pragma mark - PayPalPaymentDelegate methods
- (void)payPalPaymentViewController:(PayPalPaymentViewController *)paymentViewController didCompletePayment:(PayPalPayment *)completedPayment {
    [self verifyCompletedPayment:completedPayment];
    [[MXBADelegate sharedAppDelegate].topViewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)payPalPaymentDidCancel:(PayPalPaymentViewController *)paymentViewController {
    NSLog(@"支付有错误 稍后重试");
    [[MXBADelegate sharedAppDelegate].topViewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)verifyCompletedPayment:(PayPalPayment *)completedPayment {
    // Send the entire confirmation dictionary
    NSData *confirmation = [NSJSONSerialization dataWithJSONObject:completedPayment.confirmation options:0 error:nil];
    NSLog(@"=================%@",completedPayment.confirmation);
    NSLog(@"---------------------------------");
    NSLog(@"==================%@",confirmation);
    [self requestData];
}

5.支付成功,请求服务器刷新页面数据:至此,短视频源码调用PayPal完成支付的全流程结束

- (void)requestData{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSNumber *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];//本地 build
    //NSString *appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];version
    //NSLog(@"当前应用软件版本:%@",appCurVersion);
    NSString *build = [NSString stringWithFormat:@"%@",app_build];

    [YBToolClass postNetworkWithUrl:@"User.getBalance" andParameter:@{@"type":@"1",@"version_ios":build} success:^(int code, id  _Nonnull info, NSString * _Nonnull msg) {
        [backScroll.mj_header endRefreshing];
        if (code == 0) {
            NSDictionary *infoDic = [info firstObject];
            coinL.text = minstr([infoDic valueForKey:@"coin"]);
            jifenL.text = minstr([infoDic valueForKey:@"score"]);
            LiveUser *user = [Config myProfile];
            user.coin = minstr([infoDic valueForKey:@"coin"]);
            [Config saveProfile:user];
            if ([[PublicObj getAppBuild] isEqual:[common ios_shelves]]) {
                tipsTitleLabel.text = @"";
                tipsContentLabel.text = @"";
            }else{
                tipsTitleLabel.text = minstr([infoDic valueForKey:@"tip_t"]);
                tipsContentLabel.text = minstr([infoDic valueForKey:@"tip_d"]);
            }
//            tipsTitleLabel.text = minstr([infoDic valueForKey:@"tip_t"]);
//            tipsContentLabel.text = minstr([infoDic valueForKey:@"tip_d"]);
            if (self.block) {
                self.block(minstr([infoDic valueForKey:@"coin"]));
            }
            if (allArray.count == 0) {
                _aliapp_key_ios = [infoDic valueForKey:@"aliapp_key_ios"];
                _aliapp_partner = [infoDic valueForKey:@"aliapp_partner"];
                _aliapp_seller_id = [infoDic valueForKey:@"aliapp_seller_id"];
                //微信的信息
                _wx_appid = [infoDic valueForKey:@"wx_appid"];
                [WXApi registerApp:_wx_appid universalLink:WechatUniversalLink];
                
                //            NSMutableArray *a1 = [NSMutableArray array];
                //            [a1 addObjectsFromArray:[infoDic valueForKey:@"paylist"]];
                //            [a1 addObjectsFromArray:[infoDic valueForKey:@"paylist"]];
                //
                //            NSMutableArray *a2 = [NSMutableArray array];
                //            [a2 addObjectsFromArray:[infoDic valueForKey:@"rules"]];
                //            [a2 addObjectsFromArray:[infoDic valueForKey:@"rules"]];
                //            allArray = @[a1,a2];
                
                NSArray *ssssss = [infoDic valueForKey:@"paylist"];
                if (ssssss.count > 0) {
                    allArray = @[[infoDic valueForKey:@"paylist"],[infoDic valueForKey:@"rules"]];
                    if (!isCreatUI) {
                        [self creatUI];
                    }
                }
            }
            
        }
    } fail:^{
        [backScroll.mj_header endRefreshing];
    }];
}

本篇教程不仅适用于短视频源码,也适用于直播源码、视频社交源码等产品,随着国内源码越来越“物美价廉”很多国外开发团队愿意来中国购买成品短视频源码,再自行开发运营,而做好语言包和国际支付,是短视频源码走向国际化的重要一步。

 类似资料: