예제 #1
0
        /// <summary>
        /// Fetch an existing banked payment session object referenced by the payment ID
        /// </summary>
        /// <param name="mPaymentId">String ID of the payment session to fetch</param>
        /// <returns>BankedPaymentSession object representing the payment session retrieved from the API. Returns null if the payment ID is invalid or cannot be found by the API</returns>
        public BankedPaymentSession GetPaymentSession(string mPaymentId)
        {
            if (mPaymentId != null && mPaymentId != "")
            {
                using (WebClient mBankedClient = new WebClient()) {
                    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(BankedAPIPublicKey + ":" + BankedAPIPrivateKey));
                    mBankedClient.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
                    try {
                        string mDownloadUrl = BANKED_API_URL_PAYMENT_SESSION + mPaymentId;
                        string mResult      = mBankedClient.DownloadString(mDownloadUrl);
                        BankedPaymentSession mReturnedSession = JsonConvert.DeserializeObject <BankedPaymentSession>(mResult);
                        return(mReturnedSession);
                    } catch (WebException wex) {
                        HttpWebResponse responseDetails = (System.Net.HttpWebResponse)wex.Response;
                        switch (responseDetails.StatusCode)
                        {
                        case ((HttpStatusCode)404):
                            return(null);

                            break;

                        case HttpStatusCode.Unauthorized:
                            throw new BankedAuthException("Invalid credentials - check public key and secret key", wex);
                            break;

                        default:
                            throw new BankedException("Unknown HTTP error when attempting to create payment session", wex);
                            break;
                        }
                    }
                }
            }
            return(null);
        }
예제 #2
0
        /// <summary>
        /// Create a new payment session using the Banked API
        /// </summary>
        /// <param name="mSessionToCreate">BankedPaymentSession object representing the payment session to create using the API</param>
        /// <returns>BankedPaymentSession object representing the newly created payment session</returns>
        public BankedPaymentSession CreatePaymentSession(BankedPaymentSession mSessionToCreate)
        {
            JsonSerializerSettings mJsonSettings = new JsonSerializerSettings();

            mJsonSettings.NullValueHandling = NullValueHandling.Ignore;
            string mPaymentSessionData = JsonConvert.SerializeObject(mSessionToCreate, mJsonSettings);

            using (WebClient mBankedClient = new WebClient()) {
                mBankedClient.Headers.Add("content-type", "application/json");
                string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(BankedAPIPublicKey + ":" + BankedAPIPrivateKey));
                mBankedClient.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
                try {
                    string mResult = mBankedClient.UploadString(BANKED_API_URL_PAYMENT_SESSION, mPaymentSessionData);
                    BankedPaymentSession mReturnedSession = JsonConvert.DeserializeObject <BankedPaymentSession>(mResult);
                    return(mReturnedSession);
                } catch (WebException wex) {
                    HttpWebResponse responseDetails = (System.Net.HttpWebResponse)wex.Response;
                    switch (responseDetails.StatusCode)
                    {
                    case ((HttpStatusCode)422):
                    case HttpStatusCode.BadRequest:
                        using (StreamReader r = new StreamReader(responseDetails.GetResponseStream())) {
                            string       mErrorDetail  = r.ReadToEnd();
                            BankedErrors bErrors       = JsonConvert.DeserializeObject <BankedErrors>(mErrorDetail);
                            string       mErrorDetails = "";
                            foreach (BankedError bError in bErrors.Errors)
                            {
                                mErrorDetails += bError.Code + ":" + bError.Message + ",";
                            }
                            throw new BankedException(mErrorDetails, wex);
                        }
                        break;

                    default:
                        using (StreamReader r = new StreamReader(responseDetails.GetResponseStream())) {
                            string mErrorDetail = r.ReadToEnd();
                            throw new BankedException("Unknown HTTP error when attempting to create payment session: " + mErrorDetail, wex);
                        }
                        break;
                    }
                }
            }
            return(null);
        }