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.2'
然后第一步是创建 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 方法,该方法通过提供您的 public key 返回相应国家/地区的有效身份证明类型。
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
}
请求的正文必须包含
{
"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
}
使用发卡行数据创建支付。
请求的正文必须包含
{
"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
}