我用JSF创建了这个简单的PayPal快速结账示例:
XHTML:
<h:form target="_blank">
<h:commandButton value="paypal" action="#{pricingCalculator.initPayPal}" image="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif"/>
</h:form>
Java代码:
public void initPayPal() throws SSLConfigurationException, InvalidCredentialException, IOException, HttpErrorException, InvalidResponseDataException, ClientActionRequiredException, MissingCredentialException, InterruptedException, OAuthException, ParserConfigurationException, com.paypal.exception.SSLConfigurationException, com.paypal.exception.InvalidCredentialException, com.paypal.exception.HttpErrorException, com.paypal.exception.InvalidResponseDataException, com.paypal.exception.ClientActionRequiredException, com.paypal.exception.MissingCredentialException, com.paypal.sdk.exceptions.OAuthException, org.xml.sax.SAXException
{
PaymentDetailsType paymentDetails = new PaymentDetailsType();
paymentDetails.setPaymentAction(PaymentActionCodeType.fromValue("Sale"));
PaymentDetailsItemType item = new PaymentDetailsItemType();
BasicAmountType amt = new BasicAmountType();
amt.setCurrencyID(CurrencyCodeType.fromValue("USD"));
double itemAmount = 1.00;
amt.setValue(String.valueOf(itemAmount));
int itemQuantity = 1;
item.setQuantity(itemQuantity);
item.setName("item");
item.setAmount(amt);
List<PaymentDetailsItemType> lineItems = new ArrayList<PaymentDetailsItemType>();
lineItems.add(item);
paymentDetails.setPaymentDetailsItem(lineItems);
BasicAmountType orderTotal = new BasicAmountType();
orderTotal.setCurrencyID(CurrencyCodeType.fromValue("USD"));
orderTotal.setValue(String.valueOf(itemAmount * itemQuantity));
paymentDetails.setOrderTotal(orderTotal);
List<PaymentDetailsType> paymentDetailsList = new ArrayList<PaymentDetailsType>();
paymentDetailsList.add(paymentDetails);
SetExpressCheckoutRequestDetailsType setExpressCheckoutRequestDetails = new SetExpressCheckoutRequestDetailsType();
setExpressCheckoutRequestDetails.setReturnURL("http://devtools-paypal.com/guide/expresscheckout/java?success=true");
setExpressCheckoutRequestDetails.setCancelURL("http://devtools-paypal.com/guide/expresscheckout/java?cancel=true");
setExpressCheckoutRequestDetails.setPaymentDetails(paymentDetailsList);
SetExpressCheckoutRequestType setExpressCheckoutRequest = new SetExpressCheckoutRequestType(setExpressCheckoutRequestDetails);
setExpressCheckoutRequest.setVersion("104.0");
SetExpressCheckoutReq setExpressCheckoutReq = new SetExpressCheckoutReq();
setExpressCheckoutReq.setSetExpressCheckoutRequest(setExpressCheckoutRequest);
Map<String, String> sdkConfig = new HashMap<String, String>();
sdkConfig.put("mode", "sandbox");
sdkConfig.put("acct1.UserName", "peter.penzov_api1.gmail.com");
sdkConfig.put("acct1.Password", "5LQB5QHQJVKP9QKN");
sdkConfig.put("acct1.Signature", "AFcWxV21C7fd0v3bYYYRCpSSRl31A6JqBNQNOE3.Jwxfs7nIV1jR7qcl");
PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService(sdkConfig);
SetExpressCheckoutResponseType setExpressCheckoutResponse = service.setExpressCheckout(setExpressCheckoutReq);
String token = setExpressCheckoutResponse.getToken();
String link = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" + token;
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.redirect(link);
}
我可以成功执行付款但是当我点击付款按钮时,我被重定向到http://devtools-paypal.com/guide/expresscheckout/java?success=true
我想打印Paypal发票?我需要添加哪些Java代码?
执行成功付款后,我希望将其重定向回JSF页面,但使用旧数据。我如何保存旧数据?也许我必须使用Session范围bean?
答案 0 :(得分:0)
在SetExpressCheckout中,只需将返回网址更改为您自己的返回网址,您就应该好了。