/// <summary> /// Handles the offsite notification send by them via async call. /// </summary> /// <param name="orderNumber">The order number.</param> /// <param name="request">The HTTP request object .</param> /// <param name="paymentMethod">The payment method which is used.</param> /// <returns></returns> public IPaymentResponse HandleOffsiteNotification(int orderNumber, HttpRequest request, PaymentMethod paymentMethod) { IPaymentResponse response = new PaymentResponse() { IsOffsitePayment = true }; //Extract the BitPay settings object this.bitPaySettings = (BitPaySettings)PaymentProcessorHelper.DeserializePaymentSettings <BitPaySettings>(paymentMethod); BitPayServerResponse bitpayServerResponce = GetBitPayServiceResponce(request); bool isNotificationValid = CheckIfNotificationIsValid(orderNumber, bitpayServerResponce); if (!isNotificationValid) { response.GatewayFraudResponse = "BitPay Payment response not valid for this order."; return(response); } var paymentStatus = bitpayServerResponce.Status; if (!paymentStatus.IsNullOrEmpty()) { //check for successful if (paymentStatus == TheBitPayStatusConfirmed || paymentStatus == TheBitPayStatusComplete) { response.IsSuccess = true; } } return(response); }
private static BitPayServerResponse ParseBitPayServerResponce(string responseJson) { BitPayServerResponse bitpayServerResponce = null; using (MemoryStream stream = new MemoryStream(System.Text.Encoding.GetEncoding("utf-8").GetBytes(responseJson))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(BitPayServerResponse)); bitpayServerResponce = (BitPayServerResponse)serializer.ReadObject(stream); } return(bitpayServerResponce); }
/// <summary> /// Submits a sale request to the payment processor /// </summary> /// <param name="data">An instance of type <see cref="IPaymentRequest" /></param> /// <returns>An instance of type <see cref="IPaymentResponse" /></returns> public IPaymentResponse Sale(IPaymentRequest data) { BitPayServerResponse bitpayServerResponce = CreateInvoiceRequest(data); NameValueCollection createPaymentPostValues = new NameValueCollection(); createPaymentPostValues.Add("id", bitpayServerResponce.Id); IPaymentResponse response = CreatePaymentResponse(data.OrderNumber, createPaymentPostValues); return(response); }
private static BitPayServerResponse GetBitPayServiceResponce(HttpRequest request) { string jsonString = String.Empty; HttpContext.Current.Request.InputStream.Position = 0; using (StreamReader inputStream = new StreamReader(request.InputStream)) { jsonString = inputStream.ReadToEnd(); } BitPayServerResponse bitpayServerResponce = ParseBitPayServerResponce(jsonString); return(bitpayServerResponce); }
/// <summary> /// Makes an Create invoice request and returns the BitPay server response /// </summary> /// <param name="data"></param> /// <returns></returns> private BitPayServerResponse CreateInvoiceRequest(IPaymentRequest data) { NameValueCollection headerValues = new NameValueCollection(); byte[] encodedByteApiKey = System.Text.ASCIIEncoding.ASCII.GetBytes(this.bitPaySettings.ApiKey); string base64EncodedApiKey = Convert.ToBase64String(encodedByteApiKey); headerValues.Add("Authorization", "Basic " + base64EncodedApiKey); NameValueCollection postValues = GetPostValues(data); string responseJson = base.PostWebRequest(this.bitPaySettings.CreateInvoiceUrl, headerValues, postValues, Timeout); BitPayServerResponse bitpayServerResponce = ParseBitPayServerResponce(responseJson); return(bitpayServerResponce); }
/// <summary> /// Verifies if the order status changed notification is valid /// </summary> private bool CheckIfNotificationIsValid(int orderNumber, BitPayServerResponse bitpayServerResponce) { if (bitpayServerResponce == null) { return(false); } // We check if the hash stored in the posData propery of the BitPay responce is valid // We use this hash to confirm that the Notification is actually coming from BitPay // In order to check the hash we regenerate it the same way it was generated earlier in the Invoice request string orderHash = MD5.ComputeHashHex( String.Format("{0}:{1}:{2}", this.bitPaySettings.ApiKey, orderNumber.ToString(), this.bitPaySettings.TransactionSpeed)); var posData = bitpayServerResponce.PosData; if (posData.IsNullOrEmpty() || posData != orderHash) { return(false); } return(true); }