Rock.Store.StoreService.Purchase C# (CSharp) Method

Purchase() public method

Purchases the specified username.
public Purchase ( string username, string password, int packageId, string &errorResponse ) : PurchaseResponse
username string The username.
password string The password.
packageId int The package identifier.
errorResponse string The error response.
return PurchaseResponse
        public PurchaseResponse Purchase( string username, string password, int packageId, out string errorResponse )
        {
            errorResponse = string.Empty;

            // get organization key
            string organizationKey = GetOrganizationKey();

            // setup REST call
            var client = new RestClient( _rockStoreUrl );
            client.Timeout = _clientTimeout;
            string encodedUserName = HttpUtility.UrlEncode( Convert.ToBase64String( Encoding.UTF8.GetBytes( username ) ) );
            string encodedPassword = HttpUtility.UrlEncode( Convert.ToBase64String( Encoding.UTF8.GetBytes( password ) ) );
            string requestUrl = string.Format( "api/Store/Purchase/{0}/{1}/{2}/{3}", encodedUserName, encodedPassword, organizationKey, packageId.ToString() );
            var request = new RestRequest( requestUrl, Method.GET );

            // deserialize to list of packages
            var response = client.Execute<PurchaseResponse>( request );

            if ( response.ResponseStatus == ResponseStatus.Completed )
            {
                return response.Data;
            }
            else
            {
                errorResponse = response.ErrorMessage;
                return null;
            }
        }

Usage Example

        protected void btnInstall_Click( object sender, EventArgs e )
        {
            StoreService storeService = new StoreService();

            string errorResponse = string.Empty;
            var installResponse = storeService.Purchase( txtUsername.Text, txtPassword.Text, packageId, out errorResponse );
            if ( installResponse != null )
            {
                switch ( installResponse.PurchaseResult )
                {
                    case PurchaseResult.AuthenticationFailed:
                        lMessages.Text = string.Format( "<div class='alert alert-warning margin-t-md'><strong>Could Not Authenticate</strong> {0}</div>", installResponse.Message );
                        break;
                    case PurchaseResult.Error:
                        lMessages.Text = string.Format( "<div class='alert alert-warning margin-t-md'><strong>An Error Occurred</strong> {0}</div>", installResponse.Message );
                        break;
                    case PurchaseResult.NoCardOnFile:
                        lMessages.Text = string.Format( "<div class='alert alert-warning margin-t-md'><strong>No Card On File</strong> No credit card is on file for your organization. Please add a card from your <a href='{0}'>Account Page</a>.</div>", ResolveRockUrl( "~/RockShop/Account" ) );
                        break;
                    case PurchaseResult.NotAuthorized:
                        lMessages.Text = string.Format( "<div class='alert alert-warning margin-t-md'><strong>Unauthorized</strong> You are not currently authorized to make purchases for this organization. Please see your organization's primary contact to enable your account for purchases.</div>" );
                        break;
                    case PurchaseResult.PaymentFailed:
                        lMessages.Text = string.Format( "<div class='alert alert-warning margin-t-md'><strong>Payment Error</strong> An error occurred while processing the credit card on file for your organization. The error was: {0}. Please update your card's information from your <a href='{1}'>Account Page</a>.</div>", installResponse.Message, ResolveRockUrl( "~/RockShop/Account" ) );
                        break;
                    case PurchaseResult.Success:
                        ProcessInstall( installResponse );
                        break;
                }
            }
            else
            {
                lMessages.Text = string.Format( "<div class='alert alert-danger margin-t-md'><strong>Install Error</strong> An error occurred while attempting to authenticate your install of this package. The error was: {0}.</div>", ( string.IsNullOrWhiteSpace( errorResponse ) ? "Unknown" : errorResponse ) );
            }
        }