1. Need to set up developer account in paypal
http://developer.paypal.com
nd create one account there. After creating, login there only. In the left menu you will find "Test Accounts" link. Click on that.
From the right side screen, you can create multiple test accounts.
For testing purpose generally you need two type of accounts. One is "Seller" and one is "Buyer". Its always better to create preconfigured accounts.
Page:
<apex:page controller="PaypalProcessor" />
<apex:form>
<apex:inputText value="{!firstName}"/>
<apex:inputText value="{!lastName}" />
<apex:inputText value="{!Addy1}" />
<apex:inputText value="{!Addy2}" />
<apex:inputText value="{!CardType}" />
<apex:inputText value="{!CardNo}" />
<apex:inputText value="{!expMo}" />
<apex:inputText value="{!expYe}" />
<apex:commandbutton value="submit" action="{!doDirectPayment}" />
</apex:form>
</apex:page>
Controller:
global class PaypalProcessor{
public string amount {set;get;}
public String result {set;get;}
public string FirstName{get; set; }
public string LastName{get; set; }
public string Addy1{get; set; }
public string Addy2{get; set; }
public string Country{get; set; }
public string CardType{get; set; }
public string CardNo{get; set; }
public string expMo{get; set; }
public string expYe{get; set; }
public string CVV{get; set; }
public string city{get; set; }
public string state{get; set; }
public string zip{get; set; }
public string payer{ get; set; }
public string transid {get; set;}
public string message {get; set; }
public string err {get; set; }
public string rawResponse {get; set; }
public XmlStreamReader reader;
public PaypalProcessor(){
city = '';
state = '';
zip = '';
CVV = '';
expYe = '2019';//2019
expMo = '04';//04
CardNo = '4256541603152055'; //4256541603152055
CardType = 'Visa'; //Visa
FirstName = '';
LastName = '';
Country = 'US'; //US
Addy1 = '';
Addy2 = '';
payer = '';
err = '';
message = '';
}
public String doDirectPayment()
{
//Use the username password credentials of your Paypal account
// this below credentials will now work
String url =https://api-3t.sandbox.paypal.com/2.0/;
string un =ssanebina-ftator_api1.gmail.com ;
string pw =13968266;
string sig =An5nso7MWUdW4QKJi4-ALQdI5p2aNE1c5OKgKgV6zf5tPo3 ;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Host','api-aa.sandbox.paypal.com');}
this host is for paypal production
// req.setHeader('Host','api-aa.paypal.com');
}
}
if(CardType == 'AMEX'){
CardType='Amex';
}
String doDirectRequest;
doDirectRequest = '<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' + '\'' + '>';
doDirectRequest += '<soap:Header><RequesterCredentials xmlns="urn:ebay:api:PayPalAPI"><Credentials xmlns="urn:ebay:apis:eBLBaseComponents">';
doDirectRequest += '<Username>' + un + '</Username><ebl:Password xmlns:ebl="urn:ebay:apis:eBLBaseComponents">' + pw;
doDirectRequest += '</ebl:Password><Signature>' + sig + '</Signature>';
doDirectRequest += '</Credentials></RequesterCredentials></soap:Header><soap:Body><DoDirectPaymentReq xmlns="urn:ebay:api:PayPalAPI">';
doDirectRequest += '<DoDirectPaymentRequest><Version xmlns="urn:ebay:apis:eBLBaseComponents">1.00</Version>';
doDirectRequest += '<DoDirectPaymentRequestDetails xmlns="urn:ebay:apis:eBLBaseComponents">';
doDirectRequest += '<PaymentAction>Authorization</PaymentAction><PaymentDetails><OrderTotal currencyID="USD">' + amount + '</OrderTotal>';
doDirectRequest += '<ShipToAddress><Name>' + FirstName + ' ' + LastName + '</Name><Street1>' + Addy1 + '</Street1><Street2>' +Addy2 + '</Street2>';
doDirectRequest += '<CityName>' + city + '</CityName><StateOrProvince>' + state + '</StateOrProvince><PostalCode>' + zip + '</PostalCode>';
doDirectRequest += '<Country>' + country + '</Country></ShipToAddress>';
doDirectRequest += '</PaymentDetails><CreditCard><CreditCardType>' + CardType + '</CreditCardType><CreditCardNumber>' + CardNo + '</CreditCardNumber>';
doDirectRequest += '<ExpMonth>' + expMo + '</ExpMonth><ExpYear>' + expYe + '</ExpYear><CardOwner><PayerStatus>verified</PayerStatus>';
doDirectRequest +=
'<PayerName><FirstName>' + FirstName+ '</FirstName><LastName>' + LastName + '</LastName></PayerName><PayerCountry>' + country + '</PayerCountry>';
doDirectRequest += '<Address><Street1>' + Addy1 + '</Street1><Street2>' + Addy2 + '</Street2><CityName>' + city + '</CityName>';
doDirectRequest += '<StateOrProvince>' + state + '</StateOrProvince><Country>' + country + '</Country><PostalCode>' + zip + '</PostalCode></Address>';
doDirectRequest += '</CardOwner><CVV2>' + CVV + '</CVV2></CreditCard></DoDirectPaymentRequestDetails>';
doDirectRequest += '</DoDirectPaymentRequest></DoDirectPaymentReq></soap:Body></soap:Envelope>';
System.debug('Request Value:'+doDirectRequest);
req.setEndpoint(url);
req.setMethod('POST');
req.setHeader('Content-length', '1753' );
req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
req.setHeader('SOAPAction','');
req.setTimeout(120000);
HttpResponse res = h.send(req);
String xml = res.getBody();
rawResponse = xml;
system.debug('::' + rawResponse);
reader = res.getXmlStreamReader();
result = readXMLResponse(reader,'Ack');
reader = res.getXmlStreamReader();
err = readXMLResponse(reader, 'LongMessage');
if (result == 'Success')
{
reader = res.getXmlStreamReader();
transid = readXMLResponse(reader, 'TransactionID');
if(transid == null){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,err));
}
}
else
{
result = err;
}
return result;
}
public String readXMLResponse(XmlStreamReader reader, String sxmltag)
{
string retValue; // Read through the XML
while(reader.hasNext())
{
if (reader.getEventType() == XmlTag.START_ELEMENT)
{
if (reader.getLocalName() == sxmltag) {
reader.next();
if (reader.getEventType() == XmlTag.characters)
{
retValue = reader.getText();
}
}
}
reader.next();
}
return retValue;
}
/*public String processPayment(){
message = doDirectPayment();
if (message != 'Success'){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,message));
}
return message;
}*/
webservice static String doVoidPayment(String objtransaction)
{
//Use the username password credentials of your Paypal account
// this below credentials will now work
String url =https://api-3t.sandbox.paypal.com/2.0/;
string un =ssanebina-ftator_api1.gmail.com ;
string pw =13968266;
string sig =An5nso7MWUdW4QKJi4-ALQdI5p2aNE1c5OKgKgV6zf5tPo3 ;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Host','api-aa.sandbox.paypal.com');
//This Host is for production
// req.setHeader('Host','api-aa.paypal.com');
String doVoidRequest='';
doVoidRequest += '<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' + '\'' + '>';
doVoidRequest += '<soap:Header><RequesterCredentials xmlns="urn:ebay:api:PayPalAPI"><Credentials xmlns="urn:ebay:apis:eBLBaseComponents">';
doVoidRequest += '<Username>'+un+'</Username><ebl:Password xmlns:ebl="urn:ebay:apis:eBLBaseComponents">'+pw;
doVoidRequest += '</ebl:Password><Signature>'+sig+'</Signature>';
doVoidRequest += '</Credentials></RequesterCredentials></soap:Header><soap:Body><DoVoidReq xmlns="urn:ebay:api:PayPalAPI"><DoVoidRequest xmlns="urn:ebay:api:PayPalAPI"><Version xmlns="urn:ebay:apis:eBLBaseComponents">86.0</Version>';
doVoidRequest += '<AuthorizationID>'+objtransaction+'</AuthorizationID></DoVoidRequest></DoVoidReq></soap:Body></soap:Envelope>';
req.setBody(doVoidRequest);
System.debug('Request Value:'+doVoidRequest);
req.setEndpoint(url);
req.setMethod('POST');
req.setHeader('Content-length', '1753' );
req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
req.setHeader('SOAPAction','');
req.setTimeout(120000);
HttpResponse res = h.send(req);
String xml = res.getBody();
System.XmlStreamReader reader = res.getXmlStreamReader();
PaypalProcessor proc=new PaypalProcessor();
String result = proc.readXMLResponse(reader,'Ack');
system.debug(xml);
return result;
}
webservice static String doRefundPayment(String objtransaction)
{
//Use the username password credentials of your Paypal account
// this below credentials will now work
String url =https://api-3t.sandbox.paypal.com/2.0/;
string un =ssanebina-ftator_api1.gmail.com ;
string pw =13968266;
string sig =An5nso7MWUdW4QKJi4-ALQdI5p2aNE1c5OKgKgV6zf5tPo3 ;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Host','api-aa.sandbox.paypal.com');}
// This host is for production
// req.setHeader('Host','api-aa.paypal.com');
String doReFundRequest;
doReFundRequest = '<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' + '\'' + '>';
doReFundRequest += '<soap:Header><RequesterCredentials xmlns="urn:ebay:api:PayPalAPI"><Credentials xmlns="urn:ebay:apis:eBLBaseComponents">';
doReFundRequest += '<Username>'+un+'</Username><ebl:Password xmlns:ebl="urn:ebay:apis:eBLBaseComponents">'+pw;
doReFundRequest += '</ebl:Password><Signature>'+sig+'</Signature>';
doReFundRequest += '</Credentials></RequesterCredentials></soap:Header><soap:Body><RefundTransactionReq xmlns="urn:ebay:api:PayPalAPI"><RefundTransactionRequest><Version xmlns="urn:ebay:apis:eBLBaseComponents">86.0</Version>';
doReFundRequest += '<TransactionID>'+objtransaction+'</TransactionID><RefundType>Full</RefundType><Memo>customer requested refund.</Memo></RefundTransactionRequest></RefundTransactionReq></soap:Body></soap:Envelope>';
req.setBody(doReFundRequest);
System.debug('Request Value:'+doReFundRequest);
req.setEndpoint(url);
req.setMethod('POST');
req.setHeader('Content-length', '1753' );
req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
req.setHeader('SOAPAction','');
req.setTimeout(120000);
HttpResponse res = h.send(req);
String xml = res.getBody();
System.XmlStreamReader reader = res.getXmlStreamReader();
PaypalProcessor proc=new PaypalProcessor();
String result = proc.readXMLResponse(reader,'Ack');
system.debug(xml);
return result;
}
}
http://developer.paypal.com
nd create one account there. After creating, login there only. In the left menu you will find "Test Accounts" link. Click on that.
From the right side screen, you can create multiple test accounts.
For testing purpose generally you need two type of accounts. One is "Seller" and one is "Buyer". Its always better to create preconfigured accounts.
Page:
<apex:page controller="PaypalProcessor" />
<apex:form>
<apex:inputText value="{!firstName}"/>
<apex:inputText value="{!lastName}" />
<apex:inputText value="{!Addy1}" />
<apex:inputText value="{!Addy2}" />
<apex:inputText value="{!CardType}" />
<apex:inputText value="{!CardNo}" />
<apex:inputText value="{!expMo}" />
<apex:inputText value="{!expYe}" />
<apex:commandbutton value="submit" action="{!doDirectPayment}" />
</apex:form>
</apex:page>
Controller:
global class PaypalProcessor{
public string amount {set;get;}
public String result {set;get;}
public string FirstName{get; set; }
public string LastName{get; set; }
public string Addy1{get; set; }
public string Addy2{get; set; }
public string Country{get; set; }
public string CardType{get; set; }
public string CardNo{get; set; }
public string expMo{get; set; }
public string expYe{get; set; }
public string CVV{get; set; }
public string city{get; set; }
public string state{get; set; }
public string zip{get; set; }
public string payer{ get; set; }
public string transid {get; set;}
public string message {get; set; }
public string err {get; set; }
public string rawResponse {get; set; }
public XmlStreamReader reader;
public PaypalProcessor(){
city = '';
state = '';
zip = '';
CVV = '';
expYe = '2019';//2019
expMo = '04';//04
CardNo = '4256541603152055'; //4256541603152055
CardType = 'Visa'; //Visa
FirstName = '';
LastName = '';
Country = 'US'; //US
Addy1 = '';
Addy2 = '';
payer = '';
err = '';
message = '';
}
public String doDirectPayment()
{
//Use the username password credentials of your Paypal account
// this below credentials will now work
String url =https://api-3t.sandbox.paypal.com/2.0/;
string un =ssanebina-ftator_api1.gmail.com ;
string pw =13968266;
string sig =An5nso7MWUdW4QKJi4-ALQdI5p2aNE1c5OKgKgV6zf5tPo3 ;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Host','api-aa.sandbox.paypal.com');}
this host is for paypal production
// req.setHeader('Host','api-aa.paypal.com');
}
}
if(CardType == 'AMEX'){
CardType='Amex';
}
String doDirectRequest;
doDirectRequest = '<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' + '\'' + '>';
doDirectRequest += '<soap:Header><RequesterCredentials xmlns="urn:ebay:api:PayPalAPI"><Credentials xmlns="urn:ebay:apis:eBLBaseComponents">';
doDirectRequest += '<Username>' + un + '</Username><ebl:Password xmlns:ebl="urn:ebay:apis:eBLBaseComponents">' + pw;
doDirectRequest += '</ebl:Password><Signature>' + sig + '</Signature>';
doDirectRequest += '</Credentials></RequesterCredentials></soap:Header><soap:Body><DoDirectPaymentReq xmlns="urn:ebay:api:PayPalAPI">';
doDirectRequest += '<DoDirectPaymentRequest><Version xmlns="urn:ebay:apis:eBLBaseComponents">1.00</Version>';
doDirectRequest += '<DoDirectPaymentRequestDetails xmlns="urn:ebay:apis:eBLBaseComponents">';
doDirectRequest += '<PaymentAction>Authorization</PaymentAction><PaymentDetails><OrderTotal currencyID="USD">' + amount + '</OrderTotal>';
doDirectRequest += '<ShipToAddress><Name>' + FirstName + ' ' + LastName + '</Name><Street1>' + Addy1 + '</Street1><Street2>' +Addy2 + '</Street2>';
doDirectRequest += '<CityName>' + city + '</CityName><StateOrProvince>' + state + '</StateOrProvince><PostalCode>' + zip + '</PostalCode>';
doDirectRequest += '<Country>' + country + '</Country></ShipToAddress>';
doDirectRequest += '</PaymentDetails><CreditCard><CreditCardType>' + CardType + '</CreditCardType><CreditCardNumber>' + CardNo + '</CreditCardNumber>';
doDirectRequest += '<ExpMonth>' + expMo + '</ExpMonth><ExpYear>' + expYe + '</ExpYear><CardOwner><PayerStatus>verified</PayerStatus>';
doDirectRequest +=
'<PayerName><FirstName>' + FirstName+ '</FirstName><LastName>' + LastName + '</LastName></PayerName><PayerCountry>' + country + '</PayerCountry>';
doDirectRequest += '<Address><Street1>' + Addy1 + '</Street1><Street2>' + Addy2 + '</Street2><CityName>' + city + '</CityName>';
doDirectRequest += '<StateOrProvince>' + state + '</StateOrProvince><Country>' + country + '</Country><PostalCode>' + zip + '</PostalCode></Address>';
doDirectRequest += '</CardOwner><CVV2>' + CVV + '</CVV2></CreditCard></DoDirectPaymentRequestDetails>';
doDirectRequest += '</DoDirectPaymentRequest></DoDirectPaymentReq></soap:Body></soap:Envelope>';
System.debug('Request Value:'+doDirectRequest);
req.setEndpoint(url);
req.setMethod('POST');
req.setHeader('Content-length', '1753' );
req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
req.setHeader('SOAPAction','');
req.setTimeout(120000);
HttpResponse res = h.send(req);
String xml = res.getBody();
rawResponse = xml;
system.debug('::' + rawResponse);
reader = res.getXmlStreamReader();
result = readXMLResponse(reader,'Ack');
reader = res.getXmlStreamReader();
err = readXMLResponse(reader, 'LongMessage');
if (result == 'Success')
{
reader = res.getXmlStreamReader();
transid = readXMLResponse(reader, 'TransactionID');
if(transid == null){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,err));
}
}
else
{
result = err;
}
return result;
}
public String readXMLResponse(XmlStreamReader reader, String sxmltag)
{
string retValue; // Read through the XML
while(reader.hasNext())
{
if (reader.getEventType() == XmlTag.START_ELEMENT)
{
if (reader.getLocalName() == sxmltag) {
reader.next();
if (reader.getEventType() == XmlTag.characters)
{
retValue = reader.getText();
}
}
}
reader.next();
}
return retValue;
}
/*public String processPayment(){
message = doDirectPayment();
if (message != 'Success'){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,message));
}
return message;
}*/
webservice static String doVoidPayment(String objtransaction)
{
//Use the username password credentials of your Paypal account
// this below credentials will now work
String url =https://api-3t.sandbox.paypal.com/2.0/;
string un =ssanebina-ftator_api1.gmail.com ;
string pw =13968266;
string sig =An5nso7MWUdW4QKJi4-ALQdI5p2aNE1c5OKgKgV6zf5tPo3 ;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Host','api-aa.sandbox.paypal.com');
//This Host is for production
// req.setHeader('Host','api-aa.paypal.com');
String doVoidRequest='';
doVoidRequest += '<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' + '\'' + '>';
doVoidRequest += '<soap:Header><RequesterCredentials xmlns="urn:ebay:api:PayPalAPI"><Credentials xmlns="urn:ebay:apis:eBLBaseComponents">';
doVoidRequest += '<Username>'+un+'</Username><ebl:Password xmlns:ebl="urn:ebay:apis:eBLBaseComponents">'+pw;
doVoidRequest += '</ebl:Password><Signature>'+sig+'</Signature>';
doVoidRequest += '</Credentials></RequesterCredentials></soap:Header><soap:Body><DoVoidReq xmlns="urn:ebay:api:PayPalAPI"><DoVoidRequest xmlns="urn:ebay:api:PayPalAPI"><Version xmlns="urn:ebay:apis:eBLBaseComponents">86.0</Version>';
doVoidRequest += '<AuthorizationID>'+objtransaction+'</AuthorizationID></DoVoidRequest></DoVoidReq></soap:Body></soap:Envelope>';
req.setBody(doVoidRequest);
System.debug('Request Value:'+doVoidRequest);
req.setEndpoint(url);
req.setMethod('POST');
req.setHeader('Content-length', '1753' );
req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
req.setHeader('SOAPAction','');
req.setTimeout(120000);
HttpResponse res = h.send(req);
String xml = res.getBody();
System.XmlStreamReader reader = res.getXmlStreamReader();
PaypalProcessor proc=new PaypalProcessor();
String result = proc.readXMLResponse(reader,'Ack');
system.debug(xml);
return result;
}
webservice static String doRefundPayment(String objtransaction)
{
//Use the username password credentials of your Paypal account
// this below credentials will now work
String url =https://api-3t.sandbox.paypal.com/2.0/;
string un =ssanebina-ftator_api1.gmail.com ;
string pw =13968266;
string sig =An5nso7MWUdW4QKJi4-ALQdI5p2aNE1c5OKgKgV6zf5tPo3 ;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Host','api-aa.sandbox.paypal.com');}
// This host is for production
// req.setHeader('Host','api-aa.paypal.com');
String doReFundRequest;
doReFundRequest = '<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' + '\'' + '>';
doReFundRequest += '<soap:Header><RequesterCredentials xmlns="urn:ebay:api:PayPalAPI"><Credentials xmlns="urn:ebay:apis:eBLBaseComponents">';
doReFundRequest += '<Username>'+un+'</Username><ebl:Password xmlns:ebl="urn:ebay:apis:eBLBaseComponents">'+pw;
doReFundRequest += '</ebl:Password><Signature>'+sig+'</Signature>';
doReFundRequest += '</Credentials></RequesterCredentials></soap:Header><soap:Body><RefundTransactionReq xmlns="urn:ebay:api:PayPalAPI"><RefundTransactionRequest><Version xmlns="urn:ebay:apis:eBLBaseComponents">86.0</Version>';
doReFundRequest += '<TransactionID>'+objtransaction+'</TransactionID><RefundType>Full</RefundType><Memo>customer requested refund.</Memo></RefundTransactionRequest></RefundTransactionReq></soap:Body></soap:Envelope>';
req.setBody(doReFundRequest);
System.debug('Request Value:'+doReFundRequest);
req.setEndpoint(url);
req.setMethod('POST');
req.setHeader('Content-length', '1753' );
req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
req.setHeader('SOAPAction','');
req.setTimeout(120000);
HttpResponse res = h.send(req);
String xml = res.getBody();
System.XmlStreamReader reader = res.getXmlStreamReader();
PaypalProcessor proc=new PaypalProcessor();
String result = proc.readXMLResponse(reader,'Ack');
system.debug(xml);
return result;
}
}