Ejemplo n.º 1
0
        private async Task TellPayPalToCancelSubscription(string payPalAgreement)
        {
            try
            {
                var client = _clientFactory.GetClient();

                var requestForPayPalAgreement = new AgreementGetRequest(payPalAgreement);
                var result = await client.Execute(requestForPayPalAgreement);

                var agreement = result.Result <Agreement>();

                var request = new AgreementCancelRequest(payPalAgreement).RequestBody(new AgreementStateDescriptor()
                {
                    Note = "Cancelled"
                });

                await client.Execute(request);
            }
            catch (Exception ex)
            {
                // save error in the database.
                PaypalErrors payPalReturnedError = new PaypalErrors()
                {
                    Exception = ex.Message,
                    DateTime  = DateTime.Now
                };

                this._dbContext.PaypalErrors.Add(payPalReturnedError);
                await this._dbContext.SaveChangesAsync();
            }
        }
        private void VerifyTask(IPNLocalContext ipnContext)
        {
            try
            {
                //var verificationRequest = System.Net.WebRequest.Create("https://ipnpb.sandbox.paypal.com/cgi-bin/webscr");

                var verificationRequest = System.Net.WebRequest.Create("https://ipnpb.paypal.com/cgi-bin/webscr");

                //Send response messages back to PayPal:

                //https://ipnpb.sandbox.paypal.com/cgi-bin/webscr (for Sandbox IPNs)
                //https://ipnpb.paypal.com/cgi-bin/webscr (for live IPNs)

                //Set values for the verification request
                verificationRequest.Method      = "POST";
                verificationRequest.ContentType = "application/x-www-form-urlencoded";

                //Add cmd=_notify-validate to the payload
                string strRequest = "cmd=_notify-validate&" + ipnContext.RequestBody;
                verificationRequest.ContentLength = strRequest.Length;

                //Attach payload to the verification request
                using (StreamWriter writer = new StreamWriter(verificationRequest.GetRequestStream(), Encoding.ASCII))
                {
                    writer.Write(strRequest);
                }

                //Send the request to PayPal and get the response
                using (StreamReader reader = new StreamReader(verificationRequest.GetResponse().GetResponseStream()))
                {
                    ipnContext.Verification = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                PaypalErrors error = new PaypalErrors
                {
                    Exception = ex.Message,
                    DateTime  = DateTime.Now
                };

                _dbcontext.PaypalErrors.Add(error);
                _dbcontext.SaveChanges();
            }

            ProcessVerificationResponse(ipnContext);
        }
        public async Task <IActionResult> Receive()
        {
            try
            {
                IPNLocalContext ipnContext = new IPNLocalContext()
                {
                    IPNRequest = Request
                };

                using (StreamReader reader = new StreamReader(ipnContext.IPNRequest.Body, Encoding.ASCII))
                {
                    ipnContext.RequestBody = reader.ReadToEnd();
                }

                //Store the IPN received from PayPal
                await LogAndEmailRequest(ipnContext);

                //React backend as per IPN Received
                await UpdateSubscription(ipnContext);

                //Fire and forget verification task
                VerifyTask(ipnContext);

                //Reply back a 200 code
                return(Ok());
            }
            catch (Exception)
            {
                await EmailSuperAdmin(Request.ToString(), "IPN Send back PayPal failed");

                PaypalErrors error = new PaypalErrors
                {
                    Exception = Request.ToString(),
                    DateTime  = DateTime.Now
                };

                _dbcontext.PaypalErrors.Add(error);
                _dbcontext.SaveChanges();

                return(Ok());
            }
        }
        private async Task TellPayPalToCancelSubscription(string payPalAgreement)
        {
            try
            {
                var client = _clientFactory.GetClient();

                var requestForPayPalAgreement = new AgreementGetRequest(payPalAgreement);
                var result = await client.Execute(requestForPayPalAgreement);

                var agreement = result.Result <Agreement>();

                var request = new AgreementCancelRequest(payPalAgreement).RequestBody(new AgreementStateDescriptor()
                {
                    Note = "Cancelled"
                });

                await client.Execute(request);

                var message = $"PayPal has been notified to cancel Subscription :{agreement.Id} for the package : {agreement.Description} under {agreement.Name}.";
                var subject = $"PayPal has been notified to Cancel Subscription :{agreement.Id}";
                await EmailAdmin(message, subject);

                //await EmailSuperAdmin("Notify PayPal to Cancel Subscription SUCCESS", "Notify PayPal to Cancel Subscription SUCCESS");
            }
            catch (Exception ex)
            {
                // save error in the database.
                PaypalErrors payPalReturnedError = new PaypalErrors()
                {
                    Exception = ex.Message,
                    DateTime  = DateTime.Now
                };

                _dbcontext.PaypalErrors.Add(payPalReturnedError);
                await _dbcontext.SaveChangesAsync();

                await EmailSuperAdmin($"Notify PayPal ({payPalAgreement}) to Cancel Subscription Failed", "Notify PayPal to Cancel Subscription Failed");
            }
        }