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

      drupal_json_output($result); // how to still send data to client here?
      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()?

EDITED: for clarification, this is the function "create_payment_request":

function create_payment_request($number, $amount, $currency_code){
  // Send payment request
  $postData = '
    "payeePaymentReference": "0123456789",
    "callbackUrl": "https://www.examplesite.com/api_callback",
    "payerAlias": "'.$number.'",
    "payeeAlias": "1233206737",
    "amount": "'.$amount.'",
    "currency": "'.$currency_code.'",
    "message": "test payment"

  $ch = curl_init();
  $header = array(
    'Content-Type: application/json',
    'Accept: application/json',
    'Content-Length: ' . strlen($postData)
  curl_setopt($ch, CURLOPT_URL, "https://external_server_for_paymentrequests"); 
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_POST,1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  curl_setopt($ch, CURLOPT_SSLCERT, getcwd()."/sites/default/private/Test_Certificates/TestCertificate_123456789.pem");
  curl_setopt($ch, CURLOPT_SSLKEY, getcwd()."/sites/default/private/Test_Certificates/TestCertificate_123456789.key");
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt($ch, CURLOPT_CAINFO, getcwd()."/sites/default/private/Test_Certificates/TLS_RootCA.pem");
  curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'pem');
  curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'password');

  $result = curl_exec($ch);

  $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  if ($response_code == 200 or $response_code == 201) {
    return "Success"; 
  } else {
    return 'An error occurred'; 

  • after the call above drupal_json_output no longer sends data to the client side Something's happening in create_payment_request which is stopping it. What does that function do? – Clive Sep 17 at 10:06
  • Clive: it executes a cURL call, see my edited question. – TBJ Sep 17 at 11:40
  • And whoever downvoted me, please tell me why it's a bad question. – TBJ Sep 17 at 11:44
  • If you include the headers in the output, it's not JSON any more...I'd guess that's the problem – Clive Sep 17 at 11:47
  • Clive: OK, thanks for that suggestion. I changed the return value to a simple string "Success", but still nothing is sent to the client side – TBJ Sep 17 at 11:54

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.