【问题标题】:Magento 2 - Programatically Add Credit Card into Vault (BrainTree)Magento 2 - 以编程方式将信用卡添加到 Vault (BrainTree)
【发布时间】:2018-03-19 05:07:32
【问题描述】:

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

基本上我想要的是在 LoginIn 之后将有一个单独的部分用于保存卡。在那个客户用于添加/编辑/删除他所有的信用卡详细信息。

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

 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);

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

以下是核心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 中执行相同的过程。

感谢您的帮助

【问题讨论】:

    标签: magento2


    【解决方案1】:

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

    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));
    

    然后你可以保存支付令牌:

    use Magento\Vault\Api\PaymentTokenRepositoryInterface;
    ...
    
    $this->paymentTokenRepository->save($paymentToken);
    

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

    为了检查支付令牌是否存在,您可以使用:

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

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

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 1970-01-01
      • 2014-11-14
      • 2021-02-05
      • 1970-01-01
      • 2016-12-06
      • 2016-09-27
      • 1970-01-01
      • 2016-12-30
      相关资源
      最近更新 更多