public ActionResult PayPreapproved(Payment payment) { var userId = UserProfile.Current.UserId; _preApprovalService = DependencyResolver.Current.GetService<IPreApprovalService>(); //var userPreApproval = _preApprovalService.Find(x => x.Userd == userId && x.ContractEndDate >= DateTime.Now && x.ContractStartDate <= DateTime.Now).FirstOrDefault(); var payPal = new PayPalHelper.PayPalHelper(); //if (userPreApproval != null) //{ // var userActualPreApproval = payPal.PreapprovalDetailsAPIOperation(userPreApproval.PreApprovalKey); // userPreApproval.Approved = userActualPreApproval.approved != null && userActualPreApproval.approved.Value; // userPreApproval.ContractStartDate = DateTime.Parse(userActualPreApproval.startingDate); // userPreApproval.ContractEndDate = DateTime.Parse(userActualPreApproval.endingDate); // _preApprovalService.SaveOrUpdate(userPreApproval); // if(!userPreApproval.Approved) // { // return View("Error"); // } //} //else //{ // return View("Error"); //} var preApprovalKeys = new Dictionary<int, string>(); preApprovalKeys.Add(2, "PA-7RR891314Y878192Y"); preApprovalKeys.Add(3, ""); string preApprovalKey = ""; preApprovalKeys.TryGetValue(userId, out preApprovalKey); payment.PreapprovalKey = preApprovalKey; var responsePay = payPal.GeneratePreapprovalPayment(payment); if (responsePay != null) { // # Success values if (responsePay.responseEnvelope.ack.ToString().Trim().ToUpper().Equals("SUCCESS")) { payment.PayKey = responsePay.payKey; return View("Success"); } // # Error Values //List<ErrorData> errorMessages = responsePay.error; return View("Error"); } return View("Error"); }
// # Preapproval API Operation // Use the Preapproval API operation to set up an agreement between yourself and a sender for making payments on the sender’s behalf. You set up a preapprovals for a specific maximum amount over a specific period of time and, optionally, by any of the following constraints: the number of payments, a maximum per-payment amount, a specific day of the week or the month, and whether or not a PIN is required for each payment request. public PreapprovalResponse GeneratePreapproval(Preapproval preapproval) { // Create the PreapprovalResponse object var responsePreapproval = new PreapprovalResponse(); try { // # PreapprovalRequest // The code for the language in which errors are returned var envelopeRequest = new RequestEnvelope {errorLanguage = "en_US"}; const string pattern = "yyyy-MM-dd"; var requestPreapproval = new PreapprovalRequest(envelopeRequest, preapproval.CancelURL, "USD", preapproval.ReturnURL, "2013-07-15") {ipnNotificationUrl = preapproval.IPNHost}; // IPN URL // // * PayPal Instant Payment Notification is a call back system that is initiated when a transaction is completed // * The transaction related IPN variables will be received on the call back URL specified in the request // * The IPN variables have to be sent back to the PayPal system for validation, upon validation PayPal will send a response string "VERIFIED" or "INVALID" // * PayPal would continuously resend IPN if a wrong IPN is sent // Create the service wrapper object to make the API call var service = new AdaptivePaymentsService(); // # API call // Invoke the Preapproval method in service wrapper object responsePreapproval = service.Preapproval(requestPreapproval); if (responsePreapproval != null) { // Response envelope acknowledgement string acknowledgement = "Preapproval API Operation - "; acknowledgement += responsePreapproval.responseEnvelope.ack.ToString(); Console.WriteLine(acknowledgement + "\n"); // # Success values if (responsePreapproval.responseEnvelope != null && responsePreapproval.responseEnvelope.ack.ToString().Trim().ToUpper().Equals("SUCCESS")) { var preApprovalDetails = PreapprovalDetailsAPIOperation(responsePreapproval.preapprovalKey); _preApprovalService = DependencyResolver.Current.GetService<IPreApprovalService>(); _preApprovalService.SaveOrUpdate(new PreApproval { Approved = preApprovalDetails.approved != null && preApprovalDetails.approved.Value, ContractStartDate = DateTime.Parse(preApprovalDetails.startingDate), ContractEndDate = DateTime.Parse(preApprovalDetails.endingDate), PreApprovalKey = responsePreapproval.preapprovalKey, Userd = UserProfile.Current.UserId }); } // # Error Values else { List<ErrorData> errorMessages = responsePreapproval.error; foreach (ErrorData error in errorMessages) { Console.WriteLine("API Error Message : " + error.message + "\n"); } } } } // # Exception log catch (Exception ex) { // Log the exception message Console.WriteLine("Error Message : " + ex.Message); } return responsePreapproval; }