Salesforce calls WebService

Source: Internet
Author: User

Call external web service from salesforce apex

 

Some times, you may need to call an extenal web service which might have written on a serverside language like. net, PHP or Java.
Once you made your web service on the serverside or you can use a third party Web Service API.

I will explain you this using authorize. netpayment gateway.
There are several types of payment criterias available for authorize.net. For an example I will go with CIM (Customer Information Manager)
You will find all the details in authoirze. netdeveloper documentation.

To do payments on authorize using CIM we need to create customer profile first.

Here is the apex code that Callan external web service.

// Before any apex
Callout can call an external site, that site must be registered in the remote site settings page, or the call will fail. The platform, by default, prevents callto Unauthorized network addresses.
// You can do this like this: Got setup-> Administration setup-> security controls-> remote site settings-> New and add your endpoint URL there.
// In our case the Ende point is https://api.authorize.net/soap/v1/Service.asmx.

View plainprint?
  1. Public class callexternalws
  2. {
  3. Public void invokeexternalws ()
  4. {
  5. Httprequest Req = new httprequest ();
  6. // Set httprequest Method
  7. Req. setmethod ('post ');
  8. Req. setendpoint ('https: // api.authorize.net/soap/v1/service.asmx ');
  9. Req. setmethod ('post ');
  10. Req. setheader ('content-type', 'text/XML; charset = UTF-8 ');
  11. Req. setheader ('soapaction', 'https: // api.authorize.net/soap/v1/createcustomerprofile ');//
  12. String B = '<? XML version = "1.0" encoding = "UTF-8"?> <Soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema"> '+
  13. '<Soap: Body> <createcustomerprofile xmlns = "https://api.authorize.net/soap/v1/">' +
  14. '<Merchantauthentication> <Name> merchant name here </Name>' +
  15. '<Transactionkey> transaction key here </transactionkey> </merchantauthentication>' +
  16. '<Profile> <description> description </description>' +
  17. '<Email> sforce2009@gmail.com </Email>' +
  18. '<Paymentprofiles>' +
  19. '<Mermerpaymentprofiletype> <customertype> individual </customertype>' +
  20. '<Payment> <creditcard> <cardNumber> 6011000000000012 </cardNumber>' +
  21. '<Expirationdate> 2009-12 </expirationdate> </creditcard>' +
  22. '</Payment> </customerpaymentprofiletype> </paymentprofiles> </profile>' +
  23. '</Createcustomerprofile> </soap: Body> </soap: envelope> ';
  24. Req. setbody (B );
  25. HTTP = new HTTP ();
  26. Try {
  27. // Execute web service call here
  28. Httpresponse res = http. Send (req );
  29. // Helpful debug messages
  30. System. debug (res. tostring ());
  31. System. debug ('status: '+ res. getstatus ());
  32. System. debug ('status _ code: '+ res. getstatuscode ());
  33. // You can always parse the response XML using xmlstreamreader class
  34. } Catch (system. calloutexception e ){
  35. // Exception Handling goes here ....
  36. }
  37. }
  38. }


Hope this is useful

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.