YooMoneySDKObjc 2.4.0

YooMoneySDKObjc 2.4.0

Zalutskiy Alexander维护。



Objective-C YooMoney SDK

Version Platform Carthage compatible

概览

此开源库允许您与YooMoney API一起工作。您可以在此页面上了解更多关于YooMoney API的信息(也可在俄语中查看)。

安装

Objective-C YooMoney SDK可以通过CocoaPodsCarthage获得。

CocoaPods

要安装它,只需将以下行添加到您的Podfile中

pod "YooMoneySDKObjc"

然后在终端中运行pod install命令。

Carthage

要安装它,只需将以下行添加到您的 Cartfile

github "yoomoney/yoomoney-sdk-objc"

然后,在终端运行 carthage bootstrap 命令

使用方法

应用注册

为了能够使用库,您需要:首先,您需要注册您的应用程序并获取唯一的 client_id。请按照此页上的说明操作(也提供俄语版本:俄语)。

从 YooMoney 钱包的付款

授权

在执行首次付款之前,应用程序必须使用 OAuth2 协议进行授权,并接收访问令牌,这使得授权既安全又方便。(有关详细信息,请参阅此 API 页面:[英文](https://yoomoney.ru/docs/wallet/using-api/authorization/basics?lang=en),[俄文](https://yoomoney.ru/docs/wallet/using-api/authorization/basics?lang=ru))

首先,您应使用 YMAAPISession 类创建授权请求。然后,您使用 UIWebView 或 OS 浏览器将此授权请求发送到 YooMoney 服务器。(有关详细信息,请参阅此 API 页面:[英文](https://yoomoney.ru/docs/wallet/using-api/authorization/request-access-token?lang=en),[俄文](https://yoomoney.ru/docs/wallet/using-api/authorization/request-access-token?lang=ru))

NSDictionary *additionalParameters = @{
    YMAParameterResponseType    : YMAValueParameterResponseType, //Constant parameter  
    YMAParameterRedirectUri     : @"Your redirect_uri",          //URI that the OAuth server sends the authorization result to.
    YMAParameterScope           : @"payment-p2p"                 //A list of requested permissions.
}; 
// session - instance of YMAAPISession class 
// webView - instance of UIWebView class
NSURLRequest *authorizationRequest =  [session authorizationRequestWithClientId:@"Your client_id"
                                                           additionalParameters:additionalParameters];
[webView loadRequest:authorizationRequest];

对于授权请求,用户会被重定向到YooMoney授权页面。用户输入他的登录名和密码,查看被请求的权限列表和支付限额,然后批准或拒绝应用程序的授权请求。授权结果以“HTTP 302 重定向”的形式返回到您的 redirect_uri

您应该截获对您的 redirect_uri 的请求,取消请求并从请求查询字符串中提取验证码

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    BOOL shouldStartLoad = YES;
    NSMutableDictionary *authInfo = nil;
    NSError *error = nil;
    // session - instance of YMAAPISession class 
    if ([self.session isRequest:request 
                  toRedirectUrl:@"Your redirect_uri" 
              authorizationInfo:&authInfo 
                          error:&error]) {
        shouldStartLoad = NO;
        if (error == nil) {
            NSString *authCode = authInfo[@"code"]; 
            //Process temporary authorization code
        }
    }
    return shouldStartLoad;
}

如果授权成功完成,应用程序应立即用临时授权码交换访问令牌(了解详情请参阅本API页面:英文俄文

NSDictionary *additionalParameters = @{
    @"grant_type"           : @"authorization_code", // Constant parameter
    YMAParameterRedirectUri : @"Your redirect_uri"
};

// session  - instance of YMAAPISession class
// authCode - temporary authorization code
[session receiveTokenWithCode:authCode
                     clientId:@"Your client_id"
         additionalParameters:additionalParameters
                   completion:^(NSString *instanceId, NSError *error) {
                           if (error == nil && instanceId != nil && instanceId.length > 0) {
                               NSString *accessToken = instanceId; // Do NOT request access_token every time, when you need to call API method.
                               // Obtain it once and reuse it.
                               // Process access_token
                           }
                           else {
                               // Process error
                           }
                       }];

access_token 是对称授权密钥,因此应用程序开发者必须对其进行保护 - 令牌应当加密存储,仅当用户在应用程序内进行验证后才能访问。例如,可以使用3DES算法加密令牌,其中加密密钥是一个4位PIN码。

支付

要从YooMoney钱包进行支付,使用YMAAPISession类。在执行支付时,应调用两个API方法:request-payment和process-payment。

要执行请求(调用API方法),请使用YMAAPISession类的performRequest方法

/// Perform some request and obtaining response in block.
/// @param request  - request inherited from YMABaseRequest.
/// @param token    - access token
/// @param block    - completion of block is used to get the response.
- (void)performRequest:(YMABaseRequest *)request token:(NSString *)token completion:(YMARequestHandler)block;

有关支付场景的更多信息,请参阅API页面: 英文俄文

请求支付

要创建支付并检查其参数(了解详情请参阅本API页面:英文俄文)使用YMAPaymentRequest类

NSDictionary *paymentParameters = ... // depends on your implementation
NSString *patternId = ... // depends on your implementation
YMAPaymentRequest *request = [YMAPaymentRequest paymentWithPatternId:patternId paymentParameters:paymentParameters];

// session  - instance of YMAAPISession class 
// token    - access token
[session performRequest:request token:token completion:^(YMABaseRequest *request, YMABaseResponse *response, NSError *error) {

    YMAPaymentResponse *paymentResponse = (YMAPaymentResponse *)response;
    
    switch (paymentResponse.status) {
        case YMAResponseStatusSuccess: {
            // Process payment response
            break;
        }
        case YMAResponseStatusHoldForPickup: {
            // Process payment response
            break;
        }
        default: {
            // Process error
            break;
        }
    }
}];

处理支付

执行支付。应用程序调用该方法,直到最终支付状态已知(状态=success/refused)。推荐的重试模式由“next_retry”响应字段确定(默认为5秒)。(了解详情请参阅本API页面:英文俄文
要执行支付,请使用YMAPaymentRequest类

// paymentRequestId - requestId from instance of YMAPaymentInfoModel class
// moneySourceModel - instance of YMAMoneySourceModel class
// csc              - can be nil, if payment from wallet
// successUri       - can be nil, if payment from wallet
// failUri          - can be nil, if payment from wallet
 YMAProcessPaymentRequest *processPaymentRequest = [YMAProcessPaymentRequest   processPaymentRequestId:paymentRequestId 
                                        moneySource:moneySourceModel
                                                csc:csc
                                         successUri:successUri
                                            failUri:failUri];
    
// session - instance of YMAAPISession class 
// token   - access token
[session performRequest:processPaymentRequest 
                  token:token 
             completion:^(YMABaseRequest *request, YMABaseResponse *response, NSError *error) {
             
    YMAProcessPaymentResponse *processResponse = (YMAProcessPaymentResponse *)response;

    switch (processResponse.status) {
        case YMAResponseStatusSuccess: {
            // Process payment response
            break;
        }
        case YMAResponseStatusExtAuthRequired: {
            // Process payment response
            break;
        }
        default: {
            // Process error
            break;
        }
    }
}];

未经授权的银行卡支付

注册应用实例

在执行首次支付之前,您需要在 YooMoney 中注册设备上的应用副本,并获得应用实例的唯一标识符——实例_id。为了注册实例,请调用实例-id 方法

YMAExternalPaymentSession *session = [[YMAExternalPaymentSession alloc] init];

if (currentInstanceId == nil) {
    // token - can be nil
    [session instanceWithClientId:@"You client_id" 
                            token:token 
                       completion:^(NSString *instanceId, NSError *error)     {
        if (error != nil) {
            // Process error 
        } 
        else {
            currentInstanceId = instanceId; // Do NOT request instance id every time you, when you need to call API method. 
                             				// Obtain it once and reuse it.
            session.instanceId = currentInstanceId;
        }
    }];
} else {
    session.instanceId = currentInstanceId;
}

支付

对于未经授权的银行卡支付,请使用 YMAExternalPaymentSession 类。在进行支付时,应调用两个 API 方法:request-external-payment 和 process-external-payment。为了执行请求(调用 API 方法),使用 YMAExternalPaymentSession 类的 performRequest 方法

/// Perform some request and obtaining response in block.
/// @param request - request inherited from YMABaseRequest.
/// @param block - completion of block is used to get the response.
- (void)performRequest:(YMABaseRequest *)request token:(NSString *)token completion:(YMARequestHandler)block;

继续外部支付

为了创建支付并检查其参数,请使用 YMAExternalPaymentRequest 类

YMAExternalPaymentRequest *externalPaymentRequest = [YMAExternalPaymentRequest externalPaymentWithPatternId:patternId andPaymentParams:paymentParams];
    
// session  - instance of YMAExternalPaymentSession class 
// token    - can be nil.
[session performRequest:externalPaymentRequest token:token completion:^(YMABaseRequest *request, 
    YMABaseResponse *response, NSError *error) {
    if (error != nil) {
        // Process error
    } else {
        YMAExternalPaymentResponse *externalPaymentResponse = (YMAExternalPaymentResponse *) response;
        // Process external payment response
    }
}];

处理外部支付

支付执行。应用会调用该方法,直到最终支付状态已知(status=success/refused)。推荐的重试模式由 "next_retry" 响应字段确定(默认,5秒)。
为了执行支付,请使用 YMAExternalPaymentRequest 类

// paymentRequestId - requestId from instance of YMAExternalPaymentInfoModel class
// successUri       - address of the page to return to when the bank card was successfully authorized
// failUri          - address of the page to return to when the bank card was refused authorization
YMABaseRequest *processExternalPaymentRequest = [YMAProcessExternalPaymentRequest processExternalPaymentWithRequestId:paymentRequestId 
                         successUri:successUri 
                            failUri:failUri 
                       requestToken:NO];
    
// session  - instance of YMAExternalPaymentSession class 
// token    - can be nil.
[session performRequest:paymentRequest token:token completion:^(YMABaseRequest *request, YMABaseResponse *response,
    NSError *error) {
    
    if (error != nil) {
        // Process error
        return;
    }

    YMABaseProcessResponse *processResponse = (YMABaseProcessResponse *)response;
        
    if (processResponse.status == YMAResponseStatusInProgress) {
        // Process InProgress status 
    } 
    else if (processResponse.status == YMAResponseStatusSuccess) {
        // Process Success status
    } 
    else if (processResponse.status == YMAResponseStatusExtAuthRequired) {
        // Process AuthRequired status
    } 
}];

电商平台集成

每个电商平台都有特定的支付参数。如需了解更多详细信息,请随时联系我们:[email protected] 联系我们

链接

许可协议

Objective-c YooMoney SDK 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。