/** * Delete Braintree customer when Magento customer is deleted * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { if (!$this->config->isActive()) { return $this; } $customer = $observer->getEvent()->getCustomer(); $customerId = $this->helper->generateCustomerId($customer->getId(), $customer->getEmail()); if ($this->vault->exists($customerId)) { $this->vault->deleteCustomer($customerId); } return $this; }
public function testExistsException() { $exception = new \Braintree_Exception(); $customerId = 1; $this->braintreeCustomerMock->expects($this->once())->method('find')->with($customerId)->willThrowException($exception); $this->loggerMock->expects($this->once())->method('critical')->with($exception); $result = $this->model->exists($customerId); $this->assertEquals(false, $result); }
/** * @param InfoInterface $payment * @param string|null $token * @return array * @throws LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function populateAuthorizeRequest(InfoInterface $payment, $token) { /** @var \Magento\Sales\Api\Data\OrderInterface $order */ $order = $payment->getOrder(); $orderId = $order->getIncrementId(); $billing = $order->getBillingAddress(); $shipping = $order->getShippingAddress(); $transactionParams = ['channel' => $this->getChannel(), 'orderId' => $orderId, 'customer' => ['firstName' => $billing->getFirstname(), 'lastName' => $billing->getLastname(), 'company' => $billing->getCompany(), 'phone' => $billing->getTelephone(), 'fax' => $billing->getFax(), 'email' => $order->getCustomerEmail()]]; $customerId = $this->braintreeHelper->generateCustomerId($order->getCustomerId(), $order->getCustomerEmail()); $merchantAccountId = $this->config->getMerchantAccountId(); if ($merchantAccountId) { $transactionParams['merchantAccountId'] = $merchantAccountId; } if (!$this->isTokenAllowed()) { $token = null; } elseif (!$token) { $token = $this->getInfoInstance()->getAdditionalInformation('cc_token'); } if ($token) { $transactionParams['paymentMethodToken'] = $token; $transactionParams['customerId'] = $customerId; } elseif ($this->getInfoInstance()->getAdditionalInformation('payment_method_nonce')) { $transactionParams['paymentMethodNonce'] = $this->getInfoInstance()->getAdditionalInformation('payment_method_nonce'); if ($this->isPaymentMethodNonceForCc()) { if ($order->getCustomerId() && $this->config->useVault()) { if ($this->getInfoInstance()->getAdditionalInformation('store_in_vault')) { $last4 = $this->getInfoInstance()->getAdditionalInformation('cc_last4'); if ($this->shouldSaveCard($last4)) { $transactionParams['options']['storeInVaultOnSuccess'] = true; } } else { $transactionParams['options']['storeInVault'] = false; } if ($this->vault->exists($customerId)) { $transactionParams['customerId'] = $customerId; //TODO: How can we update customer information? unset($transactionParams['customer']); } else { $transactionParams['customer']['id'] = $customerId; } } $transactionParams['creditCard'] = ['cardholderName' => $billing->getFirstname() . ' ' . $billing->getLastname()]; } $transactionParams['billing'] = $this->toBraintreeAddress($billing); $transactionParams['shipping'] = $this->toBraintreeAddress($shipping); $transactionParams['options']['addBillingAddressToPaymentMethod'] = true; } else { throw new LocalizedException(__('Incomplete payment information.')); } if ($this->verify3dSecure()) { $transactionParams['options']['three_d_secure'] = ['required' => true]; if ($token && $this->getInfoInstance()->getAdditionalInformation('payment_method_nonce')) { $transactionParams['paymentMethodNonce'] = $this->getInfoInstance()->getAdditionalInformation('payment_method_nonce'); unset($transactionParams['paymentMethodToken']); } } if ($this->config->isFraudProtectionEnabled() && strlen($this->getInfoInstance()->getAdditionalInformation('device_data')) > 0) { $transactionParams['deviceData'] = $this->getInfoInstance()->getAdditionalInformation('device_data'); } return $transactionParams; }