Mercado Pago Services SDK - iOS (Swift 4.0)
此库使得从您的应用程序中消费 Mercado Pago API 变得非常容易。通过创建令牌,Mercado Pago 通过防止敏感的卡数据到达您的服务器来处理大部分 PCI 合规性。
安装
只需将以下行添加到您的 Podfile 中
use_frameworks!
platform :ios, '8.0'
pod 'MercadoPagoServices', '1.0.15'
第一步是创建 MercadoPagoServices 类的实例
let mercadoPagoServices = MercadoPagoServices(merchantPublicKey: "publicKey")
加载卡片
获取加载卡片所需的数据。
为了加载用户的卡片,您需要了解将要使用的卡的信息。
您需要找到客户信用卡的类型和品牌,以便正确验证详细信息,并且正如我们稍后将要看到的,以知道是否需要请求额外的信息或定义可用的分期付款数量。
使用 MercadoPagoServices 类的 getPaymentMethods 函数显示可用的支付方式
mercadoPagoServices.getPaymentMethods(callback: { (pxPaymentMethods) in
//Show paymentMethods for selection
}) { (error) in
//TODO: Manage API Failure
}
收集信用卡信息
现在您必须以安全的方式获取客户的信用卡数据。
要求信用卡信息直接从您的客户的设备发送到MercadoPago,而不是您的服务器,这一点非常重要。这样做的话,您就无需担心遵守PCI标准。我们为您提供SDK,以便您可以简单安全地实现这一点。
为您的客户创建一个输入信用卡信息的表单,例如
根据您请求支付的国家的不同,您可能需要向您的客户询问其证件类型和号码。为此,您可以从《MercadoPagoServices》类的getIdentificationTypes方法进行咨询,该方法通过提供您的公钥可以返回相关国家的有效证件类型。
mercadoPagoServices.getIdentificationTypes(callback: { (pxIdentificationTypes) in
// Done, show the identification types to your users.
}) { (error) in
//TODO: Manage API Failure
}
验证信息
在为信用卡信息创建令牌之前,建议您验证用户输入的信息。这些验证中许多非常简单,包括验证输入数据的类型是否正确,或者必填字段是否已填写。
在信用卡号码和安全性代码的情况下,根据选择的支付方式执行额外的验证。例如,如果是VISA,它会检查以4开头,安全性代码是三位数字。不用担心,MercadoPago的SDK可以帮助您处理这些细节。
示例
private func validateCardToken(cardToken: PXCardToken , paymentMethod: PXPaymentMethod) -> Bool {
/* Set errors within this function
* Request focus on the first wrong filled field recommended */
var result = true;
/* Validate card number and security code
* according the payment method’s particularities */
if !cardToken.validateCardNumber(paymentMethod){
result = false
}
if !cardToken.validateSecurityCode(paymentMethod){
result = false
}
if !cardToken.validateExpiryDate(12, year: 23) {
result = false
}
if !cardToken.validateCardholderName() {
result = false
}
/* If an identification type was selected,
* validate it’s configurations */
if getIdentificationType() != nil &&
!cardToken.validateIdentificationNumber(getIdentificationType()) {
result = false
}
return result
}
创建一次性令牌
以下代码展示了您如何直接从MercadoPago交换信用卡详细信息以获取安全令牌。
mercadoPagoServices.createToken(pxCardToken, callback: { (pxToken) in
//DONE!
}) { (error) in
//TODO: Manage API Failure
}
完成!现在您可以在服务器上创建支付了!
除了您刚刚创建的令牌,您还需要发送用户选择的支付方法、分期付款数和选择的银行。您还需要识别所购买的商品或服务以及使用您应用程序的客户。
mercadoPagoServices.createPayment(YOUR_BASE_URL, YOUR_PAYMENTS_URI, body, queryParams, callback: {(pxPayment) in
//DONE!
}) { (error) in
//TODO: Manage API Failure
}
请将请求的BODY包含以下内容
{
"transaction_amount": 100,
"token": "ff8080814c11e237014c1ff593b57b4d",
"description": "Title of what you are paying for",
"installments": 1,
"payment_method_id": "visa",
"payer": {
"email": "[email protected]"
}
}
分期付款
显示分期付款计划
使用 MercadoPagoServices 类,您可以通过提供信用卡(bin)的前六位数字、交易金额、如果需要的话,银行的标识符以及支付方式来获取可用的分期付款计划。金额是可选的,但它允许您获取我们推荐的分期付款描述,这可以通过编写很少的代码来非常方便地显示可用的分期付款计划。
我们稍后会发现,当bin不足以识别发行银行时,我们需要明确指出它以获取适当的成本。
mercadoPagoServices.getInstallments(bin, amount, issuerId, paymentMethodId, callback: {(pxInstallments) in
let payerCosts = pxInstallments[0].payerCosts
// Show payerCosts list for selection
}) { (error) in
//TODO: Manage API Failure
}
让用户在需要时选择银行
在某些情况下,MercadoPago需要知道发卡银行,以便找到其提供的促销活动或了解如何处理付款。
要了解是否需要从用户请求此信息,您可以使用来自 PaymentMethod 类的 isIssuerRequired() 方法。
mercadoPagoServices.getIssuers(paymentMethodId, bin, callback: { (pxIssuers) in
//Show issuers for selection
}) { (error) in
//TODO: Manage API Failure
}
使用发行商数据创建付款。
请将请求的BODY包含以下内容
{
"transaction_amount": 100,
"token": "ff8080814c11e237014c1ff593b57b4d",
"description": "Title of what you are paying for",
"payer": {
"email": "[email protected]"
},
"installments": 3,
"payment_method_id": "master",
"issuer_id": 338
}