gpt4 book ai didi

java - 使用 Spring Controller 处理 Paypal IPN 请求?

转载 作者:行者123 更新时间:2023-11-30 08:03:21 26 4
gpt4 key购买 nike

我想扩展 Spring 应用程序的功能以包含一个 HTTP 端点来接收 Paypal 即时支付通知。

Paypal 像这样在 HTTP 正文中发送这些内容:

mc_gross=19.95&protection_eligibility=Eligible&address_status=confirmed&payer_id=LPLWNMTBWMFAY&tax=0.00&address_street=1+Main+St&payment_date=20%3A12%3A59+Jan+13%2C+2009+PST&payment_status=Completed&charset=windows-1252&address_zip=95131&first_name=Test&mc_fee=0.88&address_country_code=US&address_name=Test+User&notify_version=2.6&custom=&payer_status=verified&address_country=United+States&address_city=San+Jose&quantity=1&verify_sign=AtkOfCXbDm2hu0ZELryHFjY-Vb7PAUvS6nMXgysbElEn9v-1XcmSoGtf&payer_email=gpmac_1231902590_per%40paypal.com&txn_id=61E67681CH3238416&payment_type=instant&last_name=User&address_state=CA&receiver_email=gpmac_1231902686_biz%40paypal.com&payment_fee=0.88&receiver_id=S8XGHLYDW9T3S&txn_type=express_checkout&item_name=&mc_currency=USD&item_number=&residence_country=US&test_ipn=1&handling_amount=0.00&transaction_subject=&payment_gross=19.95&shipping=0.00

https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNIntro/

我是否需要定义一个具体类来定义请求主体,例如

请求类

    public class PaypalIPNRequest {

private static final long serialVersionUID = 1L;
private String mc_gross;
private String protection_eligibility;
private String address_street;
...


public PaypalIPNRequest() {
}

//getters setters
}

Controller

@Override
@Auditable
@RequestMapping(value = "/ipnRequest.do", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void ipnRequest(@RequestBody final PaypalIPNRequest request) {
}

如本 SO 答案所述:@RequestBody and @ResponseBody annotations in Spring

如果 Paypal 将来更改他们的 IPN 请求,会发生什么情况,这会中断吗?

有没有更好的方法在没有特定类的情况下传递请求体?

我可以使用 HttpServletRequest 吗?

最佳答案

我过去所做的是使用 com.paypal.base.ipn.IPNMessage 来验证和检索请求(就像你建议的那样)只是对我来说重要的字段而不是将整个请求主体映射到具体类,即:

private final static String PAYPAL_WEB_IPN_TXN_PARAM = "txn_id";
private final static String PAYPAL_WEB_IPN_AMOUNT_PARAM = "mc_gross";
private final static String PAYPAL_WEB_IPN_PAYMENT_STATUS_PARAM = "payment_status";
private final static String PAYPAL_WEB_IPN_PAYMENT_STATUS = "Completed";

@Resource(name = "payPalConfigurationMap")
private Map<String, String> configurationMap;

private OAuthTokenCredential oAuthTokenCredential;

@PostConstruct
public void init() {
Properties properties = new Properties();
properties.putAll(configurationMap);
PayPalResource.initConfig(properties);

oAuthTokenCredential = new OAuthTokenCredential(
configurationMap.get(Constants.CLIENT_ID),
configurationMap.get(Constants.CLIENT_SECRET),
configurationMap
);
}


public DonationDTO validateWebIPN(HttpServletRequest request) throws Exception {
IPNMessage ipnlistener = new IPNMessage(request, configurationMap);
boolean isIpnVerified = ipnlistener.validate();

String paymentStatus = ipnlistener.getIpnValue(PAYPAL_WEB_IPN_PAYMENT_STATUS_PARAM);

if (isIpnVerified && paymentStatus.equalsIgnoreCase(PAYPAL_WEB_IPN_PAYMENT_STATUS)) {
String amount = ipnlistener.getIpnValue(PAYPAL_WEB_IPN_AMOUNT_PARAM);
String tx = ipnlistener.getIpnValue(PAYPAL_WEB_IPN_TXN_PARAM);

// irrelevant code

return donationDTO;
}else{
String exceptionMessage = "Problem when requesting info from PayPal service";
logger.error(exceptionMessage);
throw new Exception(exceptionMessage);
}
}

这样,除非 Paypal 更改了字段的名称(这不应该发生),否则您应该没有任何问题。

关于java - 使用 Spring Controller 处理 Paypal IPN 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36334135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com