2

I am currently setting up a payment method in my Drupal 7 site and following the API documentation for the service I could use this cURL command for testing purposes:

$ curl -s -S -i --cert
./Swish_Merchant_TestCertificate_1231181189.p12:swish --cert-type p12 --
cacert ./Swish_TLS_RootCA.pem --tlsv1.1 --header "Content-Type:
application/json"
https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests --data
'{ "payeePaymentReference" : "0123456789", "callbackUrl" :
"https://myfakehost.se/swishcallback.cfm", "payerAlias" : "4671234768",
"payeeAlias" : "1231181189", "amount" : "100", "currency" : "SEK",
"message" : "Kingston USB Flash Drive 8 GB" }'

But since I am developing this function in Drupal 7 I want to use drupal_http_request instead. But I am confused about how to include the certificates used in the cURL command. Anyone?

EDITED: After the suggestion below I added a context parameter. I still get "connection refused by destination". Is there anything more I should do? This is how my function looks so far:

function create_payment_request(){
  // Send payment request to Swish API
  $headers =  array('Content-Type'=>'application/JSON' );
  $url = "https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests";

  $context = stream_context_create([ 'ssl' => [
  'local_cert'        => '/mypathtocertificates/TestCertificate_123456789.pem',
  'passphrase'        => 'pass' ]]);

  $json = '{
  "payeePaymentReference": "0123456789",
  "callbackUrl: "/swish_callback",
  "payerAlias": "4671234768",
  "payeeAlias: "1231181189",
  "amount": "100",
  "currency": "SEK",
  "message": "testo 925"
}
';

  $options=array(
      'headers'=>$headers,
      'context' => $context,
      'method'=>'POST',
      'data'=>$json,
    );

    $result=drupal_http_request($url, $options);
    dpm($result);
}
2

You need to supply a context parameter in the options for drupal_http_request. The value is a stream context created using the PHP function stream_context_create.

There are a lot of possible parameters so you may have to hunt through the parameter list to find the ones you want.

| improve this answer | |
  • Thanks a lot! I have edited my question. There is still something wrong though, the destination refuses to connect. – TBJ Jul 31 '19 at 15:03
  • Not an expert on this but might you need a cacert entry as well? – Alfred Armstrong Jul 31 '19 at 15:14
  • That took me one step further. Now the status is "connecting", but I still get an error: "Error initializing socket". But thanks a lot, I will contact this payment provider directly to figure out the rest. Thanks a lot for your input. Correct answer since the context parameter is just what was missing. – TBJ Aug 1 '19 at 6:38

Your Answer

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

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