以下是我的应用程序代码,首先我调用的是setExpressCheckout(),而不是getExpressCheckoutDetails()
比使用这段代码支付和生成配置文件收到令牌时
if(request.getParameter("token")!= null && request.getParameter("PayerID") != null){
if(token.contentEquals(request.getParameter("token"))/* && payerID.contentEquals(request.getParameter("PayerID"))*/){
this.payerID = request.getParameter("PayerID");
if(doExpressCheckout()){
if(createRecurringProfile()){
//some notification showing successful order
log.debug("Trasaction status : Successfull");
license.setProfileID(profileID);
license.setActive(true);
license.setLicenseType(LicenseBean.PAYMENT_RECURRING);
ApplicationInstance.getCurrent().fireEvent("SaveLicense", description, license,false);
ApplicationInstance.getCurrent().fireEvent("DisplayLicenseDetails", "Success", license,false);
}
以下是四种方法
public String setExpressCheckout() {
NVPEncoder encoder = new NVPEncoder();
NVPDecoder decoder = new NVPDecoder();
try
{
encoder.add("VERSION", "86.0");
encoder.add("METHOD","SetExpressCheckout");
encoder.add("L_BILLINGTYPE0","RecurringPayments");
encoder.add("L_BILLINGAGREEMENTDESCRIPTION0","UserPack");
// Add request-specific fields to the request string.
encoder.add("RETURNURL",returnURL);
encoder.add("CANCELURL",cancelURL);
encoder.add("AMT",cost);
encoder.add("PAYMENTACTION",paymentAction.getValue());
encoder.add("CURRENCYCODE",currencyCode.getValue());
// Execute the API operation and obtain the response.
String NVPRequest= encoder.encode();
String NVPResponse =caller.call(NVPRequest);
decoder.decode(NVPResponse);
}
catch(PayPalException pe){
log.error("Paypal Exception:", pe.getCause());
}
catch (Exception ex)
{
log.error(ex);
}
return decoder.get("TOKEN");
}
public boolean getExpressCheckoutDetails(String token)
{
NVPEncoder encoder = new NVPEncoder();
NVPDecoder decoder = new NVPDecoder();
try
{
encoder.add("VERSION", "86.0");
encoder.add("METHOD", "GetExpressCheckoutDetails");
// Add request-specific fields to the request string.
// Pass the token value returned in SetExpressCheckout.
encoder.add("TOKEN", token);
// Execute the API operation and obtain the response.
String NVPRequest = encoder.encode();
String NVPResponse = caller.call(NVPRequest);
decoder.decode(NVPResponse);
// payerID = decoder.get("PAYERID");
payerName = decoder.get("PAYERNAME");
}catch (Exception ex)
{
log.error(ex);
}
if(decoder.get("ACK").toLowerCase().contains("success")){
this.token = token;
return true;
}
else{
return false;
}
}
public boolean createRecurringProfile()
{
NVPEncoder encoder = new NVPEncoder();
NVPDecoder decoder = new NVPDecoder();
try
{
encoder.add("VERSION", "86.0");
encoder.add("METHOD","CreateRecurringPaymentsProfile");
encoder.add("DESC","UserPack");// #Profile description - same as billing agreement description
encoder.add("BILLINGPERIOD","Month");// #Period of time between billings
encoder.add("BILLINGFREQUENCY","1");// #Frequency of charges
// encoder.add("INITAMT","25.29");
// encoder.add("FAILEDINITAMTACTION","ContinueOnFailure");
// Add request-specific fields to the request string.
// Pass the token value by actual value returned in the SetExpressCheckout.
encoder.add("TOKEN",token);
encoder.add("PAYERID",payerID);
Date localTime = new Date();
//creating DateFormat for converting time from local timezone to GMT(as specified in paypal)
DateFormat converter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
encoder.add("PROFILESTARTDATE",converter.format(localTime));//SimpleDateFormat.getTimeInstance().format(new Date())
encoder.add("AMT",cost);
encoder.add("CURRENCYCODE","USD");// #The currency, e.g. US dollars
encoder.add("COUNTRYCODE","US");// #The country code, e.g. US
encoder.add("MAXFAILEDPAYMENTS","3");
// encoder.add("PAYMENTACTION",paymentAction.getValue());
encoder.add("CURRENCYCODE",currencyCode.getValue());
// Execute the API operation and obtain the response.
String NVPRequest = encoder.encode();
String NVPResponse =caller.call(NVPRequest);
decoder.decode(NVPResponse);
profileID = decoder.get("PROFILEID");
profileStatus = decoder.get("PROFILESTATUS");
//transactionID = decoder.get("TRANSACTIONID");
paymentDate = decoder.get("PAYMENTDATE");
log.debug("Paypal acknowledgement: "+decoder.get("ACK"));
//System.out.println("Paypal acknowledgement: "+decoder.get("ACK"));
}
catch(PayPalException pe){
log.error("Paypal Exception:", pe.getCause());
}
catch(Exception ex)
{
log.error("Error :",ex.getCause());
}
if(decoder.get("ACK").toLowerCase().contains("success")){
return true;
}
else{
//System.out.println("Paypal error: "+decoder.get("L_SHORTMESSAGE0"));
log.error("Paypal error: "+decoder.get("L_SHORTMESSAGE0"));
return false;
}
}
public boolean doExpressCheckout()
{
NVPEncoder encoder = new NVPEncoder();
NVPDecoder decoder = new NVPDecoder();
try
{
encoder.add("VERSION", "51.0");
encoder.add("METHOD","DoExpressCheckoutPayment");
// Add request-specific fields to the request string.
// Pass the token value by actual value returned in the SetExpressCheckout.
encoder.add("TOKEN",token);
encoder.add("PAYERID",payerID);
encoder.add("AMT",cost);
encoder.add("PAYMENTACTION",paymentAction.getValue());
encoder.add("CURRENCYCODE",currencyCode.getValue());
// Execute the API operation and obtain the response.
String NVPRequest = encoder.encode();
String NVPResponse =caller.call(NVPRequest);
decoder.decode(NVPResponse);
//transactionID = decoder.get("TRANSACTIONID");
paymentDate = decoder.get("PAYMENTDATE");
log.debug("Paypal acknowledgement: "+decoder.get("ACK"));
//System.out.println("Paypal acknowledgement: "+decoder.get("ACK"));
}
catch(PayPalException pe){
log.error("Paypal Exception:", pe.getCause());
}
catch(Exception ex)
{
log.error("Error :",ex.getCause());
}
if(decoder.get("ACK").toLowerCase().contains("success")){
return true;
}
else{
//System.out.println("Paypal error: "+decoder.get("L_SHORTMESSAGE0"));
log.error("Paypal error: "+decoder.get("L_SHORTMESSAGE0"));
return false;
}
}