0

I am able to use Magento2's REST API to programmatically create an invoice. When I create the invoice, the API also seems to change the associated order to state paid by default. However, my workflow needs to create invoices before payment is received. The code below creates the invoice, but I get the unintended consequence that my order changes from open to paid when I execute it. Is it possible to attach a flag to my create invoice REST call that will instruct Magento to leave the order as open instead?

/**
 * create an unpaid invoice
 * @param string $magento_host
 * @param string $bearer_token
 * @param int $magento_entity_id
 * @return mixed
 * */
function createInvoiceForPurchaseOrder( $magento_host, $bearer_token, $magento_entity_id ){
    $url = 'https://' . $magento_host . '/rest/V1/order/' . $magento_entity_id . '/invoice';
    $data = json_encode( [ 'capture' => false, 'notify'  => false ] );
    $headers = [ 'Content-Type: application/json', 'Authorization: Bearer ' . $bearer_token ];
    $curl = curl_init( $url );
    curl_setopt( $curl, CURLOPT_HTTPGET,        false        );
    curl_setopt( $curl, CURLOPT_POST,           true         );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $data        );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $headers     );
    curl_setopt( $curl, CURLOPT_HTTPAUTH,       CURLAUTH_ANY );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false        );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1            );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, 1            );
    $curl_response = curl_exec( $curl );
    $curl_errno = curl_errno( $curl );
    if( $curl_errno ){
        $curl_response = (object)[
            'response'   => $curl_response,
            'curl_errno' => $curl_errno,
            'error'      => curl_error( $curl ),
        ];
    }
    curl_close( $curl );
    return $curl_response;
}

Any help pointing me in the right direction will be much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.