1 public class PaypalProccessor{ 2 public string amount; 3 public String result {set;get;} 4 public string FirstName{get; set; } 5 public string LastName{get; set; } 6 public string Addy1{get; set; } 7 public string Addy2{get; set; } 8 public string Country{get; set; } 9 public string CardType{get; set; } 10 public string CardNo{get; set; } 11 public string expMo{get; set; } 12 public string expYe{get; set; } 13 public string CVV{get; set; } 14 public string city{get; set; } 15 public string state{get; set; } 16 public string zip{get; set; } 17 public string payer{ get; set; } 18 public string transid {get; set;} 19 public string message {get; set; } 20 public string err {get; set; } 21 public string rawResponse {get; set; } 22 23 public PaypalProccessor(){ 24 city = ''; 25 state = ''; 26 zip = ''; 27 CVV = ''; 28 expYe = ''; 29 expMo = ''; 30 CardNo = ''; 31 CardType = 'Visa'; 32 FirstName = ''; 33 LastName = ''; 34 Country = 'US'; 35 Addy1 = ''; 36 Addy2 = ''; 37 payer = ''; 38 err = ''; 39 message = ''; 40 } 41 42 43 public String doDirectPayment() 44 { 45 46 Http h = new Http(); 47 HttpRequest req = new HttpRequest(); 48 String url = 'https://api-3t.sandbox.paypal.com/2.0/'; 49 string un = 'sel2_1294222298_biz_api1.gmail.com'; 50 string pw = 'xxxxxxxxxx'; 51 string sig = 'AwG6Yp1inPr4tudjFvxxxxxxxtAVuo9M5cH1nAqpxK2Biv1RrZa4gX'; 52 53 54 String doDirectRequest; 55 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' + '\'' + '>'; 56 doDirectRequest += '<soap:Header><RequesterCredentials xmlns="urn:ebay:api:PayPalAPI"><Credentials xmlns="urn:ebay:apis:eBLBaseComponents">'; 57 doDirectRequest += '<Username>' + un + '</Username><ebl:Password xmlns:ebl="urn:ebay:apis:eBLBaseComponents">' + pw; 58 doDirectRequest += '</ebl:Password><Signature>' + sig + '</Signature>'; 59 doDirectRequest += '</Credentials></RequesterCredentials></soap:Header><soap:Body><DoDirectPaymentReq xmlns="urn:ebay:api:PayPalAPI">'; 60 doDirectRequest += '<DoDirectPaymentRequest><Version xmlns="urn:ebay:apis:eBLBaseComponents">1.00</Version>'; 61 doDirectRequest += '<DoDirectPaymentRequestDetails xmlns="urn:ebay:apis:eBLBaseComponents">'; 62 doDirectRequest += '<PaymentAction>Sale</PaymentAction><PaymentDetails><OrderTotal currencyID="USD">' + amount + '</OrderTotal>'; 63 doDirectRequest += '<ShipToAddress><Name>' + FirstName + ' ' + LastName + '</Name><Street1>' + Addy1 + '</Street1><Street2>' +Addy2 + '</Street2>'; 64 doDirectRequest += '<CityName>' + city + '</CityName><StateOrProvince>' + state + '</StateOrProvince><PostalCode>' + zip + '</PostalCode>'; 65 doDirectRequest += '<Country>' + country + '</Country></ShipToAddress>'; 66 doDirectRequest += '</PaymentDetails><CreditCard><CreditCardType>' + CardType + '</CreditCardType><CreditCardNumber>' + CardNo + '</CreditCardNumber>'; 67 doDirectRequest += '<ExpMonth>' + expMo + '</ExpMonth><ExpYear>' + expYe + '</ExpYear><CardOwner><PayerStatus>verified</PayerStatus>'; 68 doDirectRequest += '<PayerName><FirstName>' + FirstName+ '</FirstName><LastName>' + LastName + '</LastName></PayerName><PayerCountry>' + country + '</PayerCountry>'; 69 doDirectRequest += '<Address><Street1>' + Addy1 + '</Street1><Street2>' + Addy2 + '</Street2><CityName>' + city + '</CityName>'; 70 doDirectRequest += '<StateOrProvince>' + state + '</StateOrProvince><Country>' + country + '</Country><PostalCode>' + zip + '</PostalCode></Address>'; 71 doDirectRequest += '</CardOwner><CVV2>' + CVV + '</CVV2></CreditCard></DoDirectPaymentRequestDetails>'; 72 doDirectRequest += '</DoDirectPaymentRequest></DoDirectPaymentReq></soap:Body></soap:Envelope>'; 73 74 req.setBody(doDirectRequest); 75 76 req.setEndpoint(url); 77 req.setMethod('POST'); 78 req.setHeader('Content-length', '1753' ); 79 req.setHeader('Content-Type', 'text/xml;charset=UTF-8'); 80 req.setHeader('SOAPAction',''); 81 req.setHeader('Host','api-aa.sandbox.paypal.com'); 82 HttpResponse res = h.send(req); 83 String xml = res.getBody(); 84 rawResponse = xml; 85 system.debug('::' + rawResponse); 86 XmlStreamReader reader = res.getXmlStreamReader(); 87 result = readXMLResponse(reader,'Ack'); 88 reader = res.getXmlStreamReader(); 89 err = readXMLResponse(reader, 'LongMessage'); 90 91 if (result == 'Success') 92 { 93 reader = res.getXmlStreamReader(); 94 transid = readXMLResponse(reader, 'TransactionID'); 95 system.debug('::' + transid ); 96 } 97 else 98 { 99 result = err; 100 } 101 return result; 102 } 103 104 public String readXMLResponse(XmlStreamReader reader, String sxmltag) 105 { 106 string retValue; // Read through the XML 107 while(reader.hasNext()) 108 { 109 if (reader.getEventType() == XmlTag.START_ELEMENT) 110 { 111 if (reader.getLocalName() == sxmltag) { 112 reader.next(); 113 if (reader.getEventType() == XmlTag.characters) 114 { 115 retValue = reader.getText(); 116 } 117 } 118 } 119 reader.next(); 120 } 121 return retValue; 122 } 123 124 public String pay(){ 125 126 err = ''; 127 if (FirstName == '') 128 err = err + 'You must enter a First Name.\n'; 129 if (LastName == '') 130 err = err + 'You must enter a Last Name.\n'; 131 if (Addy1 == '') 132 err = err + 'You must enter an Address.\n'; 133 if (city == '') 134 err = err + 'You must enter a City.\n'; 135 if (state == '') 136 err = err + 'You must enter a State.\n'; 137 if (zip == '') 138 err = err + 'You must enter a Zip.\n'; 139 if (CardNo == '') 140 err = err + 'You must enter a Credit Card Number.\n'; 141 if (expMo.length() != 2) 142 err = err + 'Expiration month must be in the format MM.\n'; 143 if (expYe.length() != 4) 144 err = err + 'Expiration year must be in the format YYYY.\n'; 145 146 if (amount == '0') 147 { 148 err += 'Amount 0 can not process.\n'; 149 message = err; 150 } 151 message = err; 152 if (err == '') 153 { 154 message = doDirectPayment(); 155 } 156 157 if (message == 'Success') 158 { 159 160 } 161 else 162 { 163 //pr = null; 164 } 165 return message; 166 } 167 168 }