1

I need to take information entered into a quote object, and authorize it on DirectPost programmatically.

The code, just like the REST API, will create an order, but with no payment data and no transaction. Nothing gets sent to Authorize.net.

This is what I have that creates an order from a quote with the correct payment method, but no transaction and no authorization:

    $cartId = $post['cart_id'];

    $quote = $this->_objectManager->create('\Magento\Quote\Model\Quote')->loadActive($cartId);
    $info = $quote->getPayment()->importData(
        [
            PaymentInterface::KEY_METHOD => 'authorizenet_directpost',
            'cc_type' => $post['cc_type'],
            'cc_owner' => $post['cc_owner'],
            'cc_last_4' => substr($post['cc_number'], -4),
            'cc_number' => $post['cc_number'],
            'cc_cid' => $post['cc_cid'],
            'cc_exp_month' => $post['cc_exp_month'],
            'cc_exp_year' => $post['cc_exp_year']
        ]
    );
    $quote->setCustomerId(null);
        $quote->setCustomerEmail($quote->getBillingAddress()->getEmail());
        $quote->setCustomerIsGuest(true);
        $quote->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID);

        $billingaddress=$quote->getBillingAddress();
        $quote->getShippingAddress()->setSameAsBilling(true);
        $quote->save();
    $payment = $quote->getPayment();
    $order = $this->_objectManager->create('\Magento\Quote\Model\QuoteManagement')->submit($quote);

How do I get it to authorize and add transaction data to the order it created?

1 Answer 1

0

Got it working. It's dirty, but it works.

$cartId = $post['cart_id'];
    if (!is_numeric($cartId)){
        $quoteIdMask = $this->_objectManager->create('Magento\Quote\Model\QuoteIdMaskFactory')->load($cartId, 'masked_id');
        $cartId = $quoteIdMask->getQuoteId();
    }


    $quote = $this->_objectManager->create('\Magento\Quote\Model\Quote')->loadActive($cartId);
    $info = $quote->getPayment()->importData(
        [
            PaymentInterface::KEY_METHOD => 'authorizenet_directpost',
            'cc_type' => $post['cc_type'],
            'cc_owner' => $post['cc_owner'],
            'cc_last_4' => substr($post['cc_number'], -4),
            'cc_number' => $post['cc_number'],
            'cc_cid' => $post['cc_cid'],
            'cc_exp_month' => $post['cc_exp_month'],
            'cc_exp_year' => $post['cc_exp_year']
        ]
    );
    $quote->setCustomerId(null);
        $quote->setCustomerEmail($quote->getBillingAddress()->getEmail());
        $quote->setCustomerIsGuest(true);
        $quote->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID);

        $billingaddress=$quote->getBillingAddress();

        $quote->getShippingAddress()->setSameAsBilling(true);
        $quote->save();
    $order = $this->_objectManager->create('\Magento\Quote\Model\QuoteManagement')->submit($quote);

        $direct_post = $this->_objectManager->create('Magento\Authorizenet\Model\Directpost\Request');
        $method = $this->_objectManager->create('Magento\Authorizenet\Model\Directpost');
        $method->generateRequestFromOrder($order);
        $direct_post->setConstantData($method);
        $direct_post->setDataFromOrder($order, $method);
        $direct_post->signRequestData();
        $direct_post->setData('x_card_num',$post['cc_number']);
        $direct_post->setData('x_cc_owner',$post['cc_owner']);
        $direct_post->setData('x_exp_date',($post['cc_exp_month'].'/'.$post['cc_exp_year']));
        $direct_post->setData('x_card_code',$post['cc_cid']);

        $method->authorize($info, $direct_post->getXAmount());
        $ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://secure.authorize.net/gateway/transact.dll");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
        http_build_query($direct_post->getData()));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

    echo json_encode($server_output);

I hope that ends up helping somebody else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.