public function Echeck::createPayment in Commerce Authorize.Net 8
Creates a payment.
Parameters
\Drupal\commerce_payment\Entity\PaymentInterface $payment: The payment.
bool $capture: Whether the created payment should be captured (VS authorized only). Allowed to be FALSE only if the plugin supports authorizations.
Throws
\InvalidArgumentException If $capture is FALSE but the plugin does not support authorizations.
\Drupal\commerce_payment\Exception\PaymentGatewayException Thrown when the transaction fails for any reason.
Overrides SupportsStoredPaymentMethodsInterface::createPayment
File
- src/
Plugin/ Commerce/ PaymentGateway/ Echeck.php, line 38
Class
- Echeck
- Provides the Authorize.net echeck payment gateway.
Namespace
Drupal\commerce_authnet\Plugin\Commerce\PaymentGatewayCode
public function createPayment(PaymentInterface $payment, $capture = TRUE) {
$this
->assertPaymentState($payment, [
'new',
]);
$payment_method = $payment
->getPaymentMethod();
$this
->assertPaymentMethod($payment_method);
$order = $payment
->getOrder();
// Transaction request.
// eChecks have a pseudo "authorized" state, so just do AUTH_CAPTURE.
$transaction_request = new TransactionRequest([
'transactionType' => TransactionRequest::AUTH_CAPTURE,
'amount' => $payment
->getAmount()
->getNumber(),
]);
list($data_descriptor, $data_value) = explode('|', $payment_method
->getRemoteId());
$payment_data = [
'opaqueData' => [
'dataDescriptor' => $data_descriptor,
'dataValue' => $data_value,
],
];
$transaction_request
->addData('payment', $payment_data);
if ($billing_profile = $payment_method
->getBillingProfile()) {
/** @var \Drupal\address\AddressInterface $address */
$address = $billing_profile
->get('address')
->first();
$bill_to = [
// @todo how to allow customizing this.
'firstName' => $address
->getGivenName(),
'lastName' => $address
->getFamilyName(),
'company' => $address
->getOrganization(),
'address' => substr($address
->getAddressLine1() . ' ' . $address
->getAddressLine2(), 0, 60),
'country' => $address
->getCountryCode(),
'city' => $address
->getLocality(),
'state' => $address
->getAdministrativeArea(),
'zip' => $address
->getPostalCode(),
];
$transaction_request
->addDataType(new BillTo(array_filter($bill_to)));
}
$profiles = $order
->collectProfiles();
if (isset($profiles['shipping']) && !$profiles['shipping']
->get('address')
->isEmpty()) {
/** @var \Drupal\address\Plugin\Field\FieldType\AddressItem $shipping_address */
$shipping_address = $profiles['shipping']
->get('address')
->first();
$ship_data = [
// @todo how to allow customizing this.
'firstName' => $shipping_address
->getGivenName(),
'lastName' => $shipping_address
->getFamilyName(),
'address' => substr($shipping_address
->getAddressLine1() . ' ' . $shipping_address
->getAddressLine2(), 0, 60),
'country' => $shipping_address
->getCountryCode(),
'company' => $shipping_address
->getOrganization(),
'city' => $shipping_address
->getLocality(),
'state' => $shipping_address
->getAdministrativeArea(),
'zip' => $shipping_address
->getPostalCode(),
];
$transaction_request
->addDataType(new ShipTo(array_filter($ship_data)));
}
// Adding order information to the transaction.
$transaction_request
->addOrder(new OrderDataType([
'invoiceNumber' => $order
->getOrderNumber() ?: $order
->id(),
]));
$transaction_request
->addData('customerIP', $order
->getIpAddress());
// Adding line items.
$line_items = $this
->getLineItems($order);
foreach ($line_items as $line_item) {
$transaction_request
->addLineItem($line_item);
}
// Adding tax information to the transaction.
$transaction_request
->addData('tax', $this
->getTax($order)
->toArray());
$transaction_request
->addData('shipping', $this
->getShipping($order)
->toArray());
$request = new CreateTransactionRequest($this->authnetConfiguration, $this->httpClient);
$request
->setTransactionRequest($transaction_request);
$response = $request
->execute();
if ($response
->getResultCode() != 'Ok') {
$this
->logResponse($response);
$message = $response
->getMessages()[0];
switch ($message
->getCode()) {
case 'E00040':
$payment_method
->delete();
throw new PaymentGatewayException('The provided payment method is no longer valid');
default:
throw new PaymentGatewayException($message
->getText());
}
}
if (!empty($response
->getErrors())) {
$message = $response
->getErrors()[0];
throw new HardDeclineException($message
->getText());
}
// Mark the payment as pending as we await for transaction details from
// Authorize.net.
$payment
->setState('pending');
$payment
->setRemoteId($response->transactionResponse->transId);
$payment
->save();
}