Magento 2-以编程方式将信用卡添加到保险柜(BrainTree) [英] Magento 2 - Programatically Add Credit Card into Vault (BrainTree)

查看:111
本文介绍了Magento 2-以编程方式将信用卡添加到保险柜(BrainTree)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Magento 2.1.5中以编程方式将信用卡详细信息添加到Vault(BrainTree)中

I need to Add Credit Card details in to Vault Programmatically (BrainTree) in Magento 2.1.5

基本上我想要的是登录后,将有一个单独的部分用于保存的卡.因为该客户用于添加/编辑/删除其所有信用卡详细信息.

Basically what i want is After LoginIn there will be a separate section for Saved Cards . In that Customer is used to Add/edit/delete All his Credit card details.

以下代码用于列出客户保存的所有信用卡

the Below Code is used to list all the Credit Card saved by the Customer

 use Magento\Vault\Api\PaymentTokenManagementInterface;
 use Magento\Customer\Model\Session;

 ... 

 // Get the customer id (currently logged in user)
 $customerId = $this->session->getCustomer()->getId();

 // Card list
 $cardList = $this->paymentTokenManagement->getListByCustomerId($customerId);

现在我想要的是如何将卡详细信息添加到保险柜?

Now what i want is how to Add the Card Details to the Vault ?

以下是在核心php中添加卡的代码

Below is the Code to Add card in core php

 $result = Braintree_Customer::create(array(
            'firstName' => 'first name',
            'lastName' => 'last name',
            'company' => 'company',
            'email' => 'xxxx@gmail.com',
            'phone' => '1234567890',
            'creditCard' => array(
                'cardholderName' => 'xxx xxx',
                'number' => '4000 0000 0000 0002 ',
                'expirationMonth' => '10',
                'expirationYear' => 2020,
                'cvv' => '123',
                'billingAddress' => array(
                    'firstName' => 'My First name',
                    'lastName' => 'My Last name'
                )
            )
        ));

但是我该如何在magento 2中执行相同的过程.

But how can i do this same process in magento 2.

感谢您的帮助

推荐答案

首先,您必须根据卡数据创建付款令牌:

First, you have to create a payment token from the card data:

use Magento\Vault\Model\CreditCardTokenFactory;
...

$paymentToken = $this->creditCardTokenFactory->create();
$paymentToken->setExpiresAt('Y-m-d 00:00:00');
$paymentToken->setGatewayToken('card_112371K7-28BB-4O3X-CCG9-1034JHK27D88');
$paymentToken->setTokenDetails([
  'type'              => 'Visa',
  'maskedCC'          => '1111',
  'expirationDate'    => '06/2019'
]);
$paymentToken->setIsActive(true);
$paymentToken->setIsVisible(true);
$paymentToken->setPaymentMethodCode('your_payment_method_code');
$paymentToken->setCustomerId($customerId);
$paymentToken->setPublicHash($this->generatePublicHash($paymentToken));

然后您可以保存付款令牌:

Then you can save the payment token:

use Magento\Vault\Api\PaymentTokenRepositoryInterface;
...

$this->paymentTokenRepository->save($paymentToken);

这只是您可以开始的一个示例.在现实世界中,您还需要检查令牌是否还不存在,还可以尝试在卡上进行支付授权,以确保其实际可用和有效.

This is just an example you can start with. In a real world situation, you would also want to check that the token doesn't already exist, an also try a payment authorisation on the card to make sure it's actually usable and valid.

为了检查付款令牌是否存在,您可以使用以下方法:

In order to check if a payment token exists or not, you can use this:

use Magento\Vault\Api\PaymentTokenManagementInterface;
...
$this->paymentTokenManagement->getByPublicHash( $paymentToken->getPublicHash(), $paymentToken->getCustomerId() );

您可以查看此处提到的Magento 2核心类,以了解有关可用于支付令牌处理的功能的更多信息. 祝你好运!

You can have a look at the core Magento 2 classes mentioned here to know more about the functions available for payment token handling. Good luck!

这篇关于Magento 2-以编程方式将信用卡添加到保险柜(BrainTree)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆