Exemplo n.º 1
0
        // # 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;
        }
    // # 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;
    }