<commit_before>package controllers.user;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.paypal.base.rest.PayPalRESTException;
import com.paypal.exception.ClientActionRequiredException;
import com.paypal.exception.HttpErrorException;
import com.paypal.exception.InvalidCredentialException;
import com.paypal.exception.InvalidResponseDataException;
import com.paypal.exception.MissingCredentialException;
import com.paypal.exception.SSLConfigurationException;
import com.paypal.sdk.exceptions.OAuthException;
import com.paypal.svcs.types.ap.PayResponse;

import controllers.AbstractController;
import domain.FeePayment;
import domain.PayPalObject;
import domain.ShipmentOffer;
import services.FeePaymentService;
import services.PayPalService;
import services.ShipmentOfferService;
import utilities.PayPalConfig;

@Controller
@RequestMapping("/user/payPal")
public class PayPalUserController extends AbstractController {
	
	// Services ---------------------------------------------------------------
	
	@Autowired
	private PayPalService payPalService;
	
	@Autowired
	private ShipmentOfferService shipmentOfferService;
	
	@Autowired
	private FeePaymentService feePaymentService;
	
	static Logger log = Logger.getLogger(PayPalUserController.class);

	
	// Constructors -----------------------------------------------------------
	
	public PayPalUserController() {
		super();
	}
		
	// Creation ------------------------------------------------------------------		

	@RequestMapping(value = "/pay", method = RequestMethod.GET)
	public ModelAndView adaptiveCreate(@RequestParam int type, @RequestParam int id,
			@RequestParam (required=false, defaultValue="0") Integer sizePriceId, @RequestParam (required=false, defaultValue = "0") Double amount,
			@RequestParam (required=false) String description, @RequestParam (required=false, defaultValue="0") int shipmentId) {
		ModelAndView result;

		PayResponse p = null;
		FeePayment feePayment;

		try {
			feePayment = feePaymentService.createAndSave(type, id, sizePriceId, amount, description, shipmentId);
			
			p = payPalService.authorizePay(feePayment.getId());
			result = new ModelAndView("redirect:" + PayPalConfig.getPayRedirectUrl()+ "?cmd=_ap-payment&paykey=" + p.getPayKey());

		} catch (Throwable e) {
			log.error(e.getStackTrace(),e);
			
			// Debera devolverte a la vista donde te pregunta como pagar y mostrar ah un error diciendo que actualmente hay un problema
			// 		con el metodo seleccionado. Que pruebe posteriormente o con tarjeta
			result = new ModelAndView("redirect:/?message=error");
		}
		return result;
	}
	
	@RequestMapping(value = "/returnPayment", method = RequestMethod.GET)
	public ModelAndView adaptiveReturnCreate(@RequestParam String trackingId) {
		ModelAndView result;
		ShipmentOffer so;
		PayPalObject po;

		try {
			payPalService.refreshPaymentStatusFromPaypal(trackingId);
			
			po = payPalService.findByTrackingId(trackingId);
			
			if (po.getFeePayment().getShipmentOffer() != null){
				so = shipmentOfferService.accept(payPalService.findByTrackingId(trackingId).getFeePayment().getShipmentOffer().getId());
				result = new ModelAndView("redirect:/shipmentOffer/user/list.do?shipmentId=" + so.getShipment().getId());
			}else{
				result = new ModelAndView("redirect:/routeOffer/user/list.do?routeId=" + po.getFeePayment().getRouteOffer().getRoute().getId());
			}
			// Pagado y registrado correctamente

		} catch (SSLConfigurationException | InvalidCredentialException | HttpErrorException
				| InvalidResponseDataException | ClientActionRequiredException | MissingCredentialException
				| OAuthException | PayPalRESTException | IOException | InterruptedException e) {
			log.error(e.getStackTrace(),e);
			
			// Si algo ha ido mal decirle al cliente que pruebe a acceder ms tarde a user/payPal/returnPayment?trackingId= this.trackingid
			
			result = new ModelAndView("redirect:/?status=somethingWrongWithPayPal");<commit_msg>No marcar como aceptada un shipment cancelado desde paypal<commit_after>package controllers.user;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.paypal.base.rest.PayPalRESTException;
import com.paypal.exception.ClientActionRequiredException;
import com.paypal.exception.HttpErrorException;
import com.paypal.exception.InvalidCredentialException;
import com.paypal.exception.InvalidResponseDataException;
import com.paypal.exception.MissingCredentialException;
import com.paypal.exception.SSLConfigurationException;
import com.paypal.sdk.exceptions.OAuthException;
import com.paypal.svcs.types.ap.PayResponse;

import controllers.AbstractController;
import domain.FeePayment;
import domain.PayPalObject;
import domain.ShipmentOffer;
import services.FeePaymentService;
import services.PayPalService;
import services.ShipmentOfferService;
import utilities.PayPalConfig;

@Controller
@RequestMapping("/user/payPal")
public class PayPalUserController extends AbstractController {
	
	// Services ---------------------------------------------------------------
	
	@Autowired
	private PayPalService payPalService;
	
	@Autowired
	private ShipmentOfferService shipmentOfferService;
	
	@Autowired
	private FeePaymentService feePaymentService;
	
	static Logger log = Logger.getLogger(PayPalUserController.class);

	
	// Constructors -----------------------------------------------------------
	
	public PayPalUserController() {
		super();
	}
		
	// Creation ------------------------------------------------------------------		

	@RequestMapping(value = "/pay", method = RequestMethod.GET)
	public ModelAndView adaptiveCreate(@RequestParam int type, @RequestParam int id,
			@RequestParam (required=false, defaultValue="0") Integer sizePriceId, @RequestParam (required=false, defaultValue = "0") Double amount,
			@RequestParam (required=false) String description, @RequestParam (required=false, defaultValue="0") int shipmentId) {
		ModelAndView result;

		PayResponse p = null;
		FeePayment feePayment;

		try {
			feePayment = feePaymentService.createAndSave(type, id, sizePriceId, amount, description, shipmentId);
			
			p = payPalService.authorizePay(feePayment.getId());
			result = new ModelAndView("redirect:" + PayPalConfig.getPayRedirectUrl()+ "?cmd=_ap-payment&paykey=" + p.getPayKey());

		} catch (Throwable e) {
			log.error(e.getStackTrace(),e);
			
			// Debera devolverte a la vista donde te pregunta como pagar y mostrar ah un error diciendo que actualmente hay un problema
			// 		con el metodo seleccionado. Que pruebe posteriormente o con tarjeta
			result = new ModelAndView("redirect:/?message=error");
		}
		return result;
	}
	
	@RequestMapping(value = "/returnPayment", method = RequestMethod.GET)
	public ModelAndView adaptiveReturnCreate(@RequestParam String trackingId) {
		ModelAndView result;
		ShipmentOffer so;
		PayPalObject po;

		try {
			payPalService.refreshPaymentStatusFromPaypal(trackingId);
			
			po = payPalService.findByTrackingId(trackingId);
			
			if (po.getFeePayment().getShipmentOffer() != null){
				so = payPalService.findByTrackingId(trackingId).getFeePayment().getShipmentOffer();
				if (!po.getPayStatus().equals("CREATED"))
					so = shipmentOfferService.accept(so.getId());
				result = new ModelAndView("redirect:/shipmentOffer/user/list.do?shipmentId=" + so.getShipment().getId());
			}else{
				result = new ModelAndView("redirect:/routeOffer/user/list.do?routeId=" + po.getFeePayment().getRouteOffer().getRoute().getId());
			}
			// Pagado y registrado correctamente

		} catch (SSLConfigurationException | InvalidCredentialException | HttpErrorException
				| InvalidResponseDataException | ClientActionRequiredException | MissingCredentialException
				| OAuthException | PayPalRESTException | IOException | InterruptedException e) {
			log.error(e.getStackTrace(),e);
			
			// Si algo ha ido mal decirle al cliente que pruebe a acceder ms tarde a user/payPal/returnPayment?trackingId= this.trackingid
			
			result = new ModelAndView("redirect:/?status=somethingWrongWithPayPal");
{'fraction_non_alphanumeric': 0.13699415338759602, 'fraction_numerical': 0.0009171156712140319, 'mean_word_length': 4.39517625231911, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2557177', 'n_tokens_mistral': 2611, 'n_tokens_neox': 2372, 'n_words': 503}