Can't send response to client after ajax call, because of cURL call on server

by TBJ   Last Updated September 17, 2019 10:07 AM

Please bare with me if this question is stupid... I have spent days googling for a solution without finding one.

The background is that I am trying to implement an onsite payment method in my Drupal 7 site.

On the server side I have this callback function for an Ajax call from the client side (which works fine, a data string is sent from the client and is captured in the function below):

function _ajax_payment_request() {

  $number = json_decode(file_get_contents("php://input"), $assoc = true);

  if(!empty($number)) {
    global $user;
    $order = commerce_cart_order_load($user->uid);
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $amount = $wrapper->commerce_order_total->amount->value();
    $currency_code = $wrapper->commerce_order_total->currency_code->value();

    // the function called below executes a cURL call to an external server
    $result = create_payment_request($number, $amount, $currency_code);
    // after the call above drupal_json_output no longer sends data to the client side

    drupal_set_message(t('result = '.$result)); // by reloading the page I can see the http response from the cURL call

    if(!empty($result)){
      drupal_json_output($result); // how to still send data to client here?
    }else{
      drupal_json_output("Inget svar från Swish"); // ... same thing here.
    }
  }
}

My problem is that after calling the create_payment_request() function, in which a cURL call is made to an external server, the function drupal_json_output() above can no longer send data to the client side.

By reloading the page the message set with drupal_set_message() becomes visible, so I can confirm that the returned cURL result was successful.

So, as soon as the cURL call is made and control is returned to the function above, the http communication with the client side script no longer works. So how can I restore the communication and send data to the client with drupal_json_output()?



Related Questions


Updated January 30, 2019 08:07 AM

Updated May 09, 2019 19:07 PM

Updated September 16, 2019 11:07 AM

Updated May 25, 2017 19:07 PM

Updated March 01, 2017 18:07 PM