Objective-C Yandex.Money SDK
概览
这个开源库允许您与 Yandex.Money API 一起工作。您可以在本页面上了解更多关于 Yandex.Money API 的信息: 页面(也有俄语版本 页面)。
安装
Objective-C Yandex.Money SDK 通过 CocoaPods 或 Carthage 提供。
Cocoapods
为了安装它,只需简单地将以下行添加到您的 Podfile 中。
pod "YandexMoneySDKObjc"
然后在终端中运行 pod install
命令。
Carthage
为了安装它,只需将以下行添加到您的 Cartfile
github "yandex-money/yandex-money-sdk-objc"
并在终端中运行 carthage bootstrap
命令
使用说明
应用注册
为了使用这个库,您需要首先注册您的应用程序并获取唯一的 client_id。请按照以下步骤在此页面(同时提供俄语版本)的描述进行操作。
Yandex.Money 钱包支付
授权
在进行第一次支付之前,应用程序必须通过 OAuth2 协议进行授权并接收访问令牌,这使授权既安全又方便。(更多信息请查看此 API 页面:[英文](http://api.yandex.com/money/doc/dg/concepts/money-oauth-intro.xml),[俄文](https://tech.yandex.ru/money/doc/dg/concepts/money-oauth-intro-docpage/))
首先,您应该使用 YMAAPISession 类创建授权请求。然后,您使用 UIWebView 或 OS 浏览器将此授权请求发送到 Yandex.Money 服务器(更多信息请查看此 API 页面:[英文](http://api.yandex.com/money/doc/dg/reference/request-access-token.xml),[俄文](https://tech.yandex.ru/money/doc/dg/reference/request-access-token-docpage/))
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];
对于授权请求,用户将被重定向到Yandex.Money授权页面。用户输入他的登录名和密码,查看请求的权限列表和支付限额,然后批准或拒绝应用的授权请求。授权结果显示为“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码。
支付
从Yandex.Money钱包中进行支付时,请使用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页面:英文,俄文),请使用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;
}
}
}];
无授权通过银行卡支付
注册应用程序实例
在进行首次支付之前,您需要在安装在设备上的Yandex.Money应用程序中注册一个副本,并获得应用程序实例的标识符— instance_id。要注册实例,请调用实例-id方法(详细信息请参阅此API页面:[English](http://api.yandex.com/money/doc/dg/reference/instance-id.xml),[Russian](http://api.yandex.ru/money/doc/dg/reference/instance-id.xml))。
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;
有关支付场景的更多信息,请参阅API页面:[English](http://api.yandex.com/money/doc/dg/reference/process-external-payments.xml),[Russian](https://tech.yandex.ru/money/doc/dg/reference/process-external-payments-docpage/)
请求外部支付
创建支付并检查其参数,请使用YMAExternalPaymentRequest类(详细信息请参阅此API页面:[English](http://api.yandex.com/money/doc/dg/reference/request-external-payment.xml),[Russian](http://api.yandex.ru/money/doc/dg/reference/request-external-payment.xml))。
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
}
}];
处理外部支付
进行支付。应用程序会调用该方法直到得知最终支付状态(状态=成功/拒绝)。推荐的重试模式由“next_retry”响应字段确定(默认为5秒)。(更多了解请参阅此API页面:英文,俄文)
使用 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] 联系方式
链接
- Yandex.Money API页面:英文,俄文
- 示例项目
- 电商平台集成详情联系方式
许可协议
Objective-c Yandex.Money SDK采用MIT许可。更多信息请参阅LICENSE文件。