salesforce 调用webservice

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 calls an 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 calls to 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.

  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.                       '<CustomerPaymentProfileType><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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值