@Override public void processIpnMessage( String originalRequest, ProfilePayPalDTO profile, MailPropertiesDTO mailProps) throws PayPalException, PayPalIpnErrorException { log.info("IPN Original Request:" + originalRequest); String responseRequest = originalRequest + "&cmd=_notify-validate"; StringBuilder sb = new StringBuilder(); boolean errorFormalMessage = false; OrderFullDTO order = null; try { // resend message to PayPal for securiry protocol HashMap<String, String> responseMap = getWrapper(profile).getNotificationIPN(responseRequest); IpnDTO ipnDTO = valueOf(responseMap); log.info(String.format("prosessIpnMessage, ipnDTO: %s", ipnDTO.toString())); // check that txn_id has not been previously processed String txn_id = ipnDTO.getTxn_id(); Integer idOrder = Integer.valueOf(ipnDTO.getCustom()); TypeStateOrder state = TypeStateOrder.fromString(ipnDTO.getPayment_status()); if (!exitsIdTransaction(txn_id, ipnDTO.getPayment_status())) { // check that receiver_email is your Primary PayPal email String recEmailMerchant = profile.getReceiverEmail(); String receiver_email = ipnDTO.getReceiver_email(); if (!recEmailMerchant.trim().equalsIgnoreCase(receiver_email)) { sb.append( String.format( "messagio non elaborato: emailMerchant %s diversa dalla mail %s presente nel messaggio %s", recEmailMerchant, receiver_email, ipnDTO.toString())); errorFormalMessage = true; } if (!ipnDTO.getMc_currency().trim().equalsIgnoreCase("EUR")) { sb.append( String.format( "messagio non elaborato:divisa accettata %s diversa dalla divisa %s presente nel messaggio %s", "EUR", ipnDTO.getMc_currency(), ipnDTO.toString())); errorFormalMessage = true; } if (errorFormalMessage) { log.error( String.format( "Messagio %s non elaborato, ci sono errori formali: %s", ipnDTO.getTxn_id(), sb.toString())); return; } // inserisco il messaggio MessageIpnDTO ipnMessage = new MessageIpnDTO(); ipnMessage.setIdOrder(idOrder); ipnMessage.setFgElaborato(!errorFormalMessage); ipnMessage.setIdTransaction(txn_id); ipnMessage.setTxMessage(ipnDTO.toString()); ipnMessage.setTxStato(ipnDTO.getPayment_status()); ipnMessage.setTxNote(sb.toString()); insertMessage(ipnMessage); // verifica se richiesta di rimborso if (ipnDTO.getParent_txn_id() != null && ipnDTO.getReason_code().equalsIgnoreCase("refund")) { log.info("refund trovato txtId = " + ipnDTO.getParent_txn_id()); try { if (idOrder != null) { order = orderService.getOrders(idOrder, null, null, null).get(0); } updateStateRefundIPN(order, ipnDTO, responseMap, null); } catch (MailNotSendException e) { // TODO Auto-generated catch block log.error("errore nell'invio della mail per il conferma rimborso", e); } return; } // è presente un ordine, recupero i dettagli if (idOrder != null) { order = orderService.getOrders(idOrder, null, null, null).get(0); // check that payment_amount/payment_currency are correct BigDecimal mc_grossBD = new BigDecimal(ipnDTO.getMc_gross()); if (order.getImOrdine().compareTo(mc_grossBD.abs()) != 0) { sb.append( String.format( "messagio non elaborato: importo db %s diverso dalla importo %s presente nel messaggio %s", order.getImOrdine(), ipnDTO.getMc_gross(), ipnDTO.toString())); errorFormalMessage = true; } } // message is correct, process message if (isPaymentKO(ipnDTO.getPayment_status())) { // inviare una mail in cui si comunica che PayPal non ha // accettato il pagamento if (idOrder != null) { // siamo nel caso di express checkout orderService.increaseQtaArticle(order, state); try { sendMailRefusedPayment(order, mailProps); } catch (MailNotSendException e) { // TODO Auto-generated catch block log.error( String.format("errore nell'invio della mail di rifuto", order.getIdOrder()), e); } } } else if (isPaymentCompleted(ipnDTO.getPayment_status())) { // inviare una mail in cui si comunica che PayPal ha // accettato il pagamento if (idOrder != null) { int oldState = order.getIdStato(); log.info(String.format("Old state %d ", oldState)); orderService.updateStateOrder(idOrder, order, state); try { if (oldState != TypeStateOrder.COMPLETEDFUNDSHELD.getState()) { // questo stato è gia considerato completed, non // ha senso reinviare la mail; sendMailAcceptedPyamentAfterPending(order, mailProps, state); } } catch (MailNotSendException e) { // TODO Auto-generated catch block log.error( String.format( "errore nell'invio della mail di accettazione pagamento", order.getIdOrder()), e); } } } else { // nessuna elaborazione da fare log.error( String.format( "stato del messaggio %s, per il transactionId %s, non ammesso. Nessuna elaborazione da fare, messaggio %s", ipnDTO.getPayment_status(), ipnDTO.getTxn_id(), ipnDTO.toString())); } return; } else { // messaggio già elaborato log.error( String.format( "transactionId %s del messaggio %s già elaborato ", txn_id, originalRequest)); return; } } catch (PayPalIpnInvalidException e) { // TODO Auto-generated catch block log.error("invalid nella ricezione IPN da paypal, messaggion non corretto", e); } catch (PayPalIpnErrorException e) { // TODO Auto-generated catch block log.error("stringa non prevista nella ricezione IPN da paypal", e); throw e; } catch (PayPalException e) { // TODO Auto-generated catch block log.error("errore imprevisto sull'invio del messaggio IPN verso paypal", e); throw e; } }