public ActionResult ConfigurePreappoval(Payment payment) { var payPal = new PayPalHelper.PayPalHelper(); var preapproval = new Preapproval { CancelURL = payment.ReturnCancelURL, ReturnURL = payment.ReturnURL, IPNHost = "http://localhost:26927/PayPal/IPN" }; var responsePreapproval = payPal.GeneratePreapproval(preapproval); if (responsePreapproval != null) { // # Success values if (responsePreapproval.responseEnvelope != null && responsePreapproval.responseEnvelope.ack.ToString().Trim().ToUpper().Equals("SUCCESS")) { preapproval.PreapprovalKey = responsePreapproval.preapprovalKey; return View(preapproval); } // # Error Values //List<ErrorData> errorMessages = responsePreapproval.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; }