I am building a offsite payment module for drupal commerce and the offsite payment provider wants the data in JSON format.
In short, I need to send the transaction form data in JSON format, receive the response in JSON format and then mark the process complete or failed depending upon the response.
So this is the last step which is not being completed.
$ data = [ 'merchant_id' => $ merchant_id, 'login_id' => $ login_id, 'password' => $ password, 'api_key' => $ api_key, 'merchant_transaction_id' => $ order_id, 'transaction_amount' => $ amount, 'url_response' => $ url_response, 'direct_to_url_response' => 'True', 'transmission_date_time' => date( 'YmdHis' ), ];
return $ this->buildRedirectForm($ form, $ form_state, $ redirect_url, $ data, $ redirect_method);
I need to send this data in JSON format to the server and get the response in JSON format. From there I need to parse this data and mark the transaction complete or whatever error I receive.
I have tried using json_encode($ data) but that doesn’t seem to work with this and throws a WSOD.
Is there any other way to send this data in JSON format within buildRedirectForm.
When I try to debug this via print, it shows the data in JSON format
$ json_data = json_encode( $ data );
echo json_encode($ json_data);
"; print_r($ json_data); echo "
"; print_r($ data); echo "
This shows the data is in JSON format but I am not able to send it to the server.
Tried this too..
$ client = \Drupal::httpClient(); $ request = $ client->post(‘https://websitename/api/simulator/gateway/rest/v1/url’, [ ‘json’ => [ ‘merchant_id’ => $ merchant_id, ‘login_id’ => $ login_id, ‘password’ => $ password, ‘api_key’ => $ api_key, ‘merchant_transaction_id’ => $ order_id, ‘transaction_amount’ => $ amount, ‘url_response’ => $ url_response, ‘direct_to_url_response’ => ‘True’, ‘transmission_date_time’ => date( ‘YmdHis’ ),
], ‘headers’ => [ ‘Accept’ => ‘application/json’, ‘Content-Type’ => ‘application/json’ ], ]);
$ response = json_decode($ request->getBody());
Have also tried CURL but not successful so far.
$ url = ‘https://https://websitename/api/simulator/gateway/rest/v1/url’ //Initiate cURL. $ ch = curl_init($ url);
//The JSON data. $ jsonData = array( ‘merchant_id’ => $ merchant_id, ‘login_id’ => $ login_id, ‘password’ => $ password, ‘api_key’ => $ api_key, ‘merchant_transaction_id’ => $ order_id, ‘transaction_amount’ => $ amount, ‘url_response’ => $ url_response, ‘direct_to_url_response’ => ‘True’, ‘transmission_date_time’ => date( ‘YmdHis’ ),
//Encode the array into JSON. $ jsonDataEncoded = json_encode($ jsonData);
//Tell cURL that we want to send a POST request. curl_setopt($ ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields. curl_setopt($ ch, CURLOPT_POSTFIELDS, $ jsonDataEncoded);
//Set the content type to application/json curl_setopt($ ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’));
//Execute the request $ result = curl_exec($ ch);
I am able to send the post data successfully but I need to send the transaction form data in JSON format, receive the response in JSON format and then mark the process complete or failed depending upon the response.
Any pointers or help with the code would be helpful and will be highly appreciated.
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
99,9% servers uptime
No usage restrictions
Perfect for SEO
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!