/// <summary> /// Handle Preapproval API /// </summary> /// <param name="context"></param> private void Preapproval(HttpContext context) { NameValueCollection parameters = context.Request.Params; PreapprovalRequest req = new PreapprovalRequest(new RequestEnvelope("en_US"), parameters["cancelUrl"], parameters["currencyCode"], parameters["returnUrl"], parameters["startingDate"]); // set optional parameters if(parameters["dateOfMonth"] != "") { req.dateOfMonth = Int32.Parse(parameters["dateOfMonth"]); } if(parameters["dayOfWeek"] != "" && parameters["dayOfWeek"] != "") { req.dayOfWeek = (PayPal.AdaptivePayments.Model.DayOfWeek) Enum.Parse(typeof(PayPal.AdaptivePayments.Model.DayOfWeek), parameters["dayOfWeek"]); } if(parameters["dateOfMonth"] != "") { req.dateOfMonth = Int32.Parse(parameters["dateOfMonth"]); } if(parameters["endingDate"] != "") { req.endingDate = parameters["endingDate"]; } if(parameters["maxAmountPerPayment"] != "") { req.maxAmountPerPayment = Decimal.Parse(parameters["maxAmountPerPayment"]); } if(parameters["maxNumberOfPayments"] != "" ) { req.maxNumberOfPayments = Int32.Parse(parameters["maxNumberOfPayments"]); } if(parameters["maxNumberOfPaymentsPerPeriod"] != "") { req.maxNumberOfPaymentsPerPeriod = Int32.Parse(parameters["maxNumberOfPaymentsPerPeriod"]); } if(parameters["maxTotalAmountOfAllPayments"] != "") { req.maxTotalAmountOfAllPayments = Decimal.Parse(parameters["maxTotalAmountOfAllPayments"]); } if(parameters["paymentPeriod"] != "" && parameters["paymentPeriod"] != "") { req.paymentPeriod = parameters["paymentPeriod"]; } if(parameters["memo"] != "") { req.memo = parameters["memo"]; } if(parameters["ipnNotificationUrl"] != "") { req.ipnNotificationUrl = parameters["ipnNotificationUrl"]; } if(parameters["senderEmail"] != "") { req.senderEmail = parameters["senderEmail"]; } if(parameters["pinType"] != "" && parameters["pinType"] != "") { req.pinType = parameters["pinType"]; } if(parameters["feesPayer"] != "") { req.feesPayer = parameters["feesPayer"]; } if (parameters["displayMaxTotalAmount"] != "") { req.displayMaxTotalAmount = Boolean.Parse(parameters["displayMaxTotalAmount"]); } // All set. Fire the request AdaptivePaymentsService service = new AdaptivePaymentsService(); PreapprovalResponse resp = null; try { resp = service.Preapproval(req); } catch (System.Exception e) { context.Response.Write(e.Message); return; } // Display response values. Dictionary<string, string> keyResponseParams = new Dictionary<string, string>(); string redirectUrl = null; if (!(resp.responseEnvelope.ack == AckCode.FAILURE) && !(resp.responseEnvelope.ack == AckCode.FAILUREWITHWARNING)) { keyResponseParams.Add("Preapproval key", resp.preapprovalKey); } displayResponse(context, "Preapproval", keyResponseParams, service.getLastRequest(), service.getLastResponse(), resp.error, redirectUrl); }
// # Preapproval API Operation public PreapprovalResponse PreapprovalAPIOperation(decimal rate, string userId, DateTime subscriptionDate) { // Create the PreapprovalResponse object PreapprovalResponse responsePreapproval = new PreapprovalResponse(); try { // # PreapprovalRequest // The code for the language in which errors are returned RequestEnvelope envelopeRequest = new RequestEnvelope(); envelopeRequest.errorLanguage = "en_US"; PreapprovalRequest requestPreapproval = new PreapprovalRequest(); requestPreapproval.requestEnvelope = envelopeRequest; requestPreapproval.returnUrl = System.Configuration.ConfigurationManager.AppSettings["RETURN_URL"] + "/DoctorInformation/PayPalSubscription/?pstatus=success&userId=" + userId; requestPreapproval.cancelUrl = System.Configuration.ConfigurationManager.AppSettings["CANCEL_URL"] + "/DoctorInformation/PayPalSubscription/?pstatus=cancel&userid=" + userId; requestPreapproval.currencyCode = "USD"; requestPreapproval.startingDate = subscriptionDate.ToString("yyyy-MM-dd"); requestPreapproval.endingDate = subscriptionDate.AddYears(1).ToString("yyyy-MM-dd"); requestPreapproval.feesPayer = "EACHRECEIVER"; requestPreapproval.maxAmountPerPayment = rate; requestPreapproval.maxNumberOfPaymentsPerPeriod = 1; requestPreapproval.maxTotalAmountOfAllPayments = (12 * rate); requestPreapproval.maxNumberOfPayments = 12; //requestPreapproval.dateOfMonth = 27; requestPreapproval.pinType = "NOT_REQUIRED"; //if(!string.IsNullOrEmpty(paypalId)) // requestPreapproval.senderEmail = paypalId; requestPreapproval.paymentPeriod = "DAILY"; // 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 requestPreapproval.ipnNotificationUrl = System.Configuration.ConfigurationManager.AppSettings["IPN_URL"] + "/DoctorInformation/PayPalIPN/"; // Create the service wrapper object to make the API call AdaptivePaymentsService 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(); //logger.Info(acknowledgement + "\n"); //Console.WriteLine(acknowledgement + "\n"); // # Success values if (responsePreapproval.responseEnvelope.ack.ToString().Trim().ToUpper().Equals("SUCCESS")) { //logger.Info("Preapproval Key : " + responsePreapproval.preapprovalKey + "\n"); //Console.WriteLine("Preapproval Key : " + responsePreapproval.preapprovalKey + "\n"); // Once you get success response, user has to redirect to PayPal // to preapprove the payment. Construct redirectURL as follows, // `redirectURL=https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-preapproval&preapprovalkey=" // + responsePreapproval.preapprovalKey;` string url = String.Format("https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-preapproval&preapprovalkey={0}", responsePreapproval.preapprovalKey); if (HttpContext.Current != null) { HttpContext.Current.Response.Redirect(url); } } // # Error Values else { List <ErrorData> errorMessages = responsePreapproval.error; foreach (ErrorData error in errorMessages) { //logger.Debug("API Error Message : " + error.message); //Console.WriteLine("API Error Message : " + error.message + "\n"); } } } } // # Exception log catch (System.Exception ex) { // Log the exception message } return(responsePreapproval); }
// # 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 PreapprovalAPIOperation() { // Create the PreapprovalResponse object PreapprovalResponse responsePreapproval = new PreapprovalResponse(); try { // # PreapprovalRequest // The code for the language in which errors are returned RequestEnvelope envelopeRequest = new RequestEnvelope(); envelopeRequest.errorLanguage = "en_US"; // PreapprovalRequest takes mandatory params: // // * `RequestEnvelope` - Information common to each API operation, such // as the language in which an error message is returned. // * `Cancel URL` - URL to redirect the sender's browser to after // canceling the preapproval // * `Currency Code` - The code for the currency in which the payment is // made; you can specify only one currency, regardless of the number of // receivers // * `Return URL` - URL to redirect the sender's browser to after the // sender has logged into PayPal and confirmed the preapproval // * `Starting Date` - First date for which the preapproval is valid. It // cannot be before today's date or after the ending date. PreapprovalRequest requestPreapproval = new PreapprovalRequest(envelopeRequest, "http://localhost/cancel", "USD", "http://localhost/return", "2013-12-18"); // 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 requestPreapproval.ipnNotificationUrl = "http://IPNhost"; // Create the service wrapper object to make the API call AdaptivePaymentsService 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(); logger.Info(acknowledgement + "\n"); Console.WriteLine(acknowledgement + "\n"); // # Success values if (responsePreapproval.responseEnvelope.ack.ToString().Trim().ToUpper().Equals("SUCCESS")) { logger.Info("Preapproval Key : " + responsePreapproval.preapprovalKey + "\n"); Console.WriteLine("Preapproval Key : " + responsePreapproval.preapprovalKey + "\n"); // Once you get success response, user has to redirect to PayPal // to preapprove the payment. Construct redirectURL as follows, // `redirectURL=https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-preapproval&preapprovalkey=" // + responsePreapproval.preapprovalKey;` } // # Error Values else { List <ErrorData> errorMessages = responsePreapproval.error; foreach (ErrorData error in errorMessages) { logger.Debug("API Error Message : " + error.message); Console.WriteLine("API Error Message : " + error.message + "\n"); } } } } // # Exception log catch (System.Exception ex) { // Log the exception message logger.Debug("Error Message : " + ex.Message); Console.WriteLine("Error Message : " + ex.Message); } return(responsePreapproval); }