Magento 2 – Get Order PayPal Information Programmatically

magento2orderspaypal

I'm developing a custom module to pass the following data to an external system after payment.

  1. PayPal Express Checkout – Last Correlation ID
  2. PayPal Express Checkout – Last Transaction ID

I can get the Order object but I cannot find the correct method to get these info.

As suggested by Khoa, I checked the sales_order_payment table and here is the record

*************************** 35. row ***************************
                   entity_id: 35
                   parent_id: 35
      base_shipping_captured: 0.0000
           shipping_captured: 0.0000
             amount_refunded: NULL
            base_amount_paid: 32.0000
             amount_canceled: NULL
      base_amount_authorized: 32.0000
     base_amount_paid_online: 32.0000
 base_amount_refunded_online: NULL
        base_shipping_amount: 0.0000
             shipping_amount: 0.0000
                 amount_paid: 32.0000
           amount_authorized: 32.0000
         base_amount_ordered: 32.0000
      base_shipping_refunded: NULL
           shipping_refunded: NULL
        base_amount_refunded: NULL
              amount_ordered: 32.0000
        base_amount_canceled: NULL
            quote_payment_id: NULL
             additional_data: NULL
                cc_exp_month: NULL
            cc_ss_start_year: 0
            echeck_bank_name: NULL
                      method: paypal_express
       cc_debug_request_body: NULL
            cc_secure_verify: NULL
      protection_eligibility: NULL
                 cc_approval: NULL
                   cc_last_4: NULL
       cc_status_description: NULL
                 echeck_type: NULL
cc_debug_response_serialized: NULL
           cc_ss_start_month: 0
         echeck_account_type: NULL
               last_trans_id: 7B590901VY7471028
               cc_cid_status: NULL
                    cc_owner: NULL
                     cc_type: NULL
                   po_number: NULL
                 cc_exp_year: 0
                   cc_status: NULL
       echeck_routing_number: NULL
              account_status: NULL
           anet_trans_method: NULL
      cc_debug_response_body: NULL
                 cc_ss_issue: NULL
         echeck_account_name: NULL
               cc_avs_status: NULL
               cc_number_enc: NULL
                 cc_trans_id: NULL
              address_status: NULL
      additional_information: a:11:{s:15:"paypal_payer_id";s:13:"S3N79BW9ASCXQ";s:18:"paypal_payer_email";s:25:"developer-buyer@qooar.com";s:19:"paypal_payer_status";s:8:"verified";s:21:"paypal_correlation_id";s:13:"689b01d290375";s:32:"paypal_express_checkout_payer_id";s:13:"S3N79BW9ASCXQ";s:29:"paypal_express_checkout_token";s:20:"EC-7F776218LS594863N";s:12:"method_title";s:23:"PayPal Express Checkout";s:41:"paypal_express_checkout_redirect_required";N;s:29:"paypal_protection_eligibility";s:10:"Ineligible";s:21:"paypal_payment_status";s:9:"completed";s:21:"paypal_pending_reason";s:4:"None";}
35 rows in set (0.00 sec)

The data I want is in the paypal_correlation_id & paypal_express_checkout_token of the additional_information column. How can I retrieve it?

I have tried this but failed

    $payment = $order->getPayment();
    var_dump(serialize($payment->getTransactionAdditionalInfo()));

Best Answer

Should see sales_order and sales_order_payment tables.

The information is located at the additional_information of the sales_order_payment table

select additional_information from sales_order_payment
additional_information: a:11:{s:15:"paypal_payer_id";s:13:"S3N79BW9ASCXQ";s:18:"paypal_payer_email";s:25:"developer-buyer@qooar.com";s:19:"paypal_payer_status";s:8:"verified";s:21:"paypal_correlation_id";s:13:"689b01d290375";s:32:"paypal_express_checkout_payer_id";s:13:"S3N79BW9ASCXQ";s:29:"paypal_express_checkout_token";s:20:"EC-7F776218LS594863N";s:12:"method_title";s:23:"PayPal Express Checkout";s:41:"paypal_express_checkout_redirect_required";N;s:29:"paypal_protection_eligibility";s:10:"Ineligible";s:21:"paypal_payment_status";s:9:"completed";s:21:"paypal_pending_reason";s:4:"None";}

From your order object, we can get the payment:

$paymentAdditionalInfo = $order->getPayment()->getAdditionalInformation();

// get PayPal express checkout token
$token = $paymentAdditionalInfo['paypal_express_checkout_token'];

// get PayPal correlation ID
$correlationId = $paymentAdditionalInfo['paypal_correlation_id'];
Related Topic