Esempio n. 1
0
        public static string GetProductHandle()
        {
            ChargifyAccountRetrieverSection config      = ConfigurationManager.GetSection("chargify") as ChargifyAccountRetrieverSection;
            ChargifyAccountElement          accountInfo = config.GetDefaultOrFirst();

            return(accountInfo.ProductHandle);
        }
Esempio n. 2
0
        public static ChargifyConnect CreateChargify()
        {
            ChargifyAccountRetrieverSection config = ConfigurationManager.GetSection("chargify") as ChargifyAccountRetrieverSection;

            if (config == null)
            {
                return(null);
            }

            // new instance
            ChargifyConnect _chargify = new ChargifyConnect();

            ChargifyAccountElement accountInfo = config.GetDefaultOrFirst();

            _chargify.apiKey    = accountInfo.ApiKey;
            _chargify.Password  = accountInfo.ApiPassword;
            _chargify.URL       = accountInfo.Site;
            _chargify.SharedKey = accountInfo.SharedKey;
            _chargify.UseJSON   = config.UseJSON;

            return(_chargify);
        }
Esempio n. 3
0
        public ActionResult ChargifyWebHook()
        {
            //ViewBag.Message = "Your contact page.";

            //var responseText=string.Empty;
            //ChargifyAccountRetrieverSection config = ConfigurationManager.GetSection("chargify") as ChargifyAccountRetrieverSection;
            //ChargifyAccountElement accountInfo = config.GetDefaultOrFirst();

            //HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(accountInfo.Site + "webhooks.format");
            //httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            ////2.Pass the apikey and apipassword of chargify
            //httpWebRequest.Headers.Add("username:"******"password:"******"GET";


            ////https://domainname.chargify.com/

            //return View();

            WebhookEventData webHookdata = new WebhookEventData();

            webHookdata.@event = "signup_success";

            ChargifyAccountRetrieverSection config      = ConfigurationManager.GetSection("chargify") as ChargifyAccountRetrieverSection;
            ChargifyAccountElement          accountInfo = config.GetDefaultOrFirst();
            var sharedKey = accountInfo.SharedKey;

            var signatureHeaderHandle = "X-Chargify-Webhook-Signature-Hmac-Sha-256";
            //string signature = !string.IsNullOrEmpty(signature_hmac_sha_256) ? signature_hmac_sha_256 : this.Request.Headers[signatureHeaderHandle]; // Try and get the signature passed in the request header

            //ChargifyAccountRetrieverSection config = ConfigurationManager.GetSection("chargify") as ChargifyAccountRetrieverSection;
            //ChargifyAccountElement accountInfo = config.GetDefaultOrFirst();

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(accountInfo.Site + "webhooks.json");

            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            //2.Pass the apikey and apipassword of chargify
            httpWebRequest.Headers.Add("username:"******"password:"******"GET";


            // var isRequestValid = this.Request.InputStream.IsWebhookRequestValid(sharedKey, signature);

            //if (!isRequestValid) { return new HttpStatusCodeResult(HttpStatusCode.NotAcceptable, "Signature mismatch"); }

            switch (webHookdata.@event)
            {
            case "signup_success":
                break;

            case "signup_failure":
                break;

            case "renewal_success":
                break;

            case "renewal_failure":
                break;

            case "payment_success":
                break;

            case "payment_failure":
                break;

            case "billing_date_change":
                break;

            case "subscription_state_change":
                break;

            case "subscription_product_change":
                break;

            case "subscription_card_update":
                break;

            case "expiring_card":
                break;

            case "customer_update":
                break;

            case "component_allocation_change":
                break;

            case "metered_usage":
                break;

            case "upgrade_downgrade_sucess":
                break;

            case "upgrade_downgrade_failure":
                break;

            case "refund_success":
                break;

            case "refund_failure":
                break;

            case "upcoming_renewal_notice":
                break;

            case "end_of_trial_notice":
                break;

            case "statement_closed":
                break;

            case "statement_settled":
                break;

            case "expiration_date_change":
                break;

            default:
                break;
            }

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
        public ActionResult Webhook(WebhookEventData model, string signature_hmac_sha_256)
        {
            ChargifyAccountRetrieverSection config      = ConfigurationManager.GetSection("chargify") as ChargifyAccountRetrieverSection;
            ChargifyAccountElement          accountInfo = config.GetDefaultOrFirst();
            var sharedKey = accountInfo.SharedKey;

            var    signatureHeaderHandle = "X-Chargify-Webhook-Signature-Hmac-Sha-256";
            string signature             = !string.IsNullOrEmpty(signature_hmac_sha_256) ? signature_hmac_sha_256 : this.Request.Headers[signatureHeaderHandle]; // Try and get the signature passed in the request header
            var    isRequestValid        = this.Request.InputStream.IsWebhookRequestValid(sharedKey, signature);

            if (!isRequestValid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotAcceptable, "Signature mismatch"));
            }

            switch (model.@event)
            {
            case "signup_success":
                break;

            case "signup_failure":
                break;

            case "renewal_success":
                break;

            case "renewal_failure":
                break;

            case "payment_success":
                break;

            case "payment_failure":
                break;

            case "billing_date_change":
                break;

            case "subscription_state_change":
                break;

            case "subscription_product_change":
                break;

            case "subscription_card_update":
                break;

            case "expiring_card":
                break;

            case "customer_update":
                break;

            case "component_allocation_change":
                break;

            case "metered_usage":
                break;

            case "upgrade_downgrade_sucess":
                break;

            case "upgrade_downgrade_failure":
                break;

            case "refund_success":
                break;

            case "refund_failure":
                break;

            case "upcoming_renewal_notice":
                break;

            case "end_of_trial_notice":
                break;

            case "statement_closed":
                break;

            case "statement_settled":
                break;

            case "expiration_date_change":
                break;

            default:
                break;
            }

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
Esempio n. 5
0
        public override void OnChargifyUpdate(int webhookID, string signature, string data)
        {
            ChargifyAccountRetrieverSection config =
                ConfigurationManager.GetSection("chargify") as ChargifyAccountRetrieverSection;

            if (config == null)
            {
                throw new HttpException(500, "Chargify section is not defined in a web.config file");
            }
            if (!data.IsChargifyWebhookContentValid(signature, config.GetSharedKeyForDefaultOrFirstSite()))
            {
                throw new HttpException(500, "Chargify Webhook Exception. Data was INVALID through self-validation. WebhookID: " + webhookID.ToString() + " Data: " + data);
            }

            NameValueCollection query = new NameValueCollection();

            string[] _arr = data.Split(new string[] { "&" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string _arrElem in _arr)
            {
                string[] _a = _arrElem.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                if (_a[0].Length > 0)
                {
                    query.Add(_a[0], _a.Length > 1 ? Uri.UnescapeDataString(_a[1]) : string.Empty);
                }
            }

            Guid   _orgId  = Guid.Empty;
            Guid   _instId = Guid.Empty;
            string _event  = query["event"];

            if (_event == "payment_success" || _event == "payment_failure")
            {
                if (string.IsNullOrEmpty(query["payload[subscription][customer][reference]"]))
                {
                    throw new HttpException(500, "Customer Refererence Organization and Instance Guids is not defined. Chargify Webhook Event:\"" + _event + "\" Id:" + webhookID.ToString() + " Data:" + data);
                }
                string   custRef     = query["payload[subscription][customer][reference]"];
                string   errDescr    = "Chargify Webhook Event:\"" + _event + "\" Id:" + webhookID.ToString() + " Reference:\"" + custRef + "\" Data:" + data;
                string[] _arrCustRef = custRef.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
                if (_arrCustRef.Length < 2)
                {
                    throw new HttpException(500, "Instance Refererence Guid is not defined. " + errDescr);
                }
                if (!Guid.TryParse(_arrCustRef[0], out _orgId))
                {
                    throw new HttpException(500, "Customer Organization Refererence Guid is invalid. " + errDescr);
                }
                if (!Guid.TryParse(_arrCustRef[1], out _instId))
                {
                    throw new HttpException(500, "Customer Instance Refererence Guid is invalid. " + errDescr);
                }
                Organization _org = null;
                try
                {
                    _org = Providers.OrganizationProvider.GetOrganization(_orgId);
                }
                catch (Exception ex)
                {
                    throw new HttpException(500, "Can't execute Providers.OrganizationProvider.GetOrganization() method. " + errDescr, ex);
                }
                if (_org == null)
                {
                    throw new HttpException(500, "Can't find Organization. " + errDescr);
                }
                Instance _inst = null;
                try
                {
                    _inst = Providers.InstanceProvider.GetInstance(_instId, _orgId);
                }
                catch (Exception ex)
                {
                    throw new HttpException(500, "Can't execute Providers.InstanceProvider.GetInstance() method. " + errDescr, ex);
                }
                if (_inst == null)
                {
                    throw new HttpException(500, "Can't find Instance. " + errDescr);
                }
                if (_event == "payment_failure" && _inst.CreditCardStatus == CreditCardStatus.Registered)
                {
                    try
                    {
                        Providers.InstanceProvider.UpdateInstance(_inst, CreditCardStatus.Declined);
                    }
                    catch (Exception ex)
                    {
                        throw new HttpException(500, "Can't Update Instance CreditCardStatus to Declined. " + errDescr, ex);
                    }
                    string _uEmail     = query["payload[subscription][customer][email]"];
                    string _uFirstName = query["payload[subscription][customer][first_name]"];
                    string _uLastName  = query["payload[subscription][customer][last_name]"];
                    string _appName    = FrameworkConfiguration.Current.WebApplication.Name;
                    string _salesEmail = FrameworkConfiguration.Current.WebApplication.Email.SalesTeam;
                    string _subject    = _org.Name + " " + _inst.Name + " Chargify payment attempt failded.";
                    string _body       = _appName + " Chargify service can't process payment operation for \"" + _org.Name + " " + _inst.Name + "\" organization instance.\r\n Operation status is " + _event + "\r\n\r\nContact User Email: " + _uEmail + " " + _uFirstName + " " + _uLastName;
                    if (!string.IsNullOrEmpty(_salesEmail))
                    {
                        try
                        {
                            Support.SendEmail(FrameworkConfiguration.Current.WebApplication.Support.Email, FrameworkConfiguration.Current.WebApplication.Support.Email, _salesEmail, string.Empty, _subject, _body, false, false, EmailSendingReason.Undefined);
                        }
                        catch (Exception ex)
                        {
                            throw new HttpException(500, "Can't send failure event support email. " + errDescr, ex);
                        }
                    }
                }
                else if (_event == "payment_success" && _inst.CreditCardStatus != CreditCardStatus.Registered)
                {
                    try
                    {
                        Providers.InstanceProvider.UpdateInstance(_inst, CreditCardStatus.Registered);
                    }
                    catch (Exception ex)
                    {
                        throw new HttpException(500, "Can't Update Instance CreditCardStatus to Registered. " + errDescr, ex);
                    }
                }
            }
            else
            {
                throw new HttpException(500, "Unknown Chargify Webhook. Event:\"" + _event + "\" Id:" + webhookID.ToString() + " Data:" + data);
            }
        }