private bool doRefund(StripeSession session, Transaction charge, decimal?amount = null, string description = null, object extraData = null) { var fromActualData = session.FetchAccountData(charge.From); var refundAmount = amount ?? charge.Amount.Value; try { var bodyPrms = new Dictionary <string, string>() { { PRM_AMOUNT, ((int)((refundAmount * 100))).ToString() } }; if (description.IsNotNullOrWhiteSpace()) { bodyPrms.Add(PRM_REASON, description); } var prms = new WebClient.RequestParams(this) { UserName = session.SecretKey, Method = HTTPRequestMethod.POST, BodyParameters = bodyPrms }; dynamic obj = WebClient.GetJsonAsDynamic(new Uri(REFUND_URI.Args(charge.Token)), prms); dynamic lastRefund = ((NFX.Serialization.JSON.JSONDataArray)obj.refunds.Data).First(); var created = ((long)obj.created).FromSecondsSinceUnixEpochStart(); StatRefund(charge, amount); return(true); } catch (Exception ex) { StatRefundError(); var wex = ex as System.Net.WebException; if (wex != null) { var response = wex.Response as System.Net.HttpWebResponse; if (response != null) { string errorMessage = this.GetType().Name + ".Refund(money: {0}, card: {1}, description: '{2}')" .Args(charge.Amount, fromActualData.AccountID, description); PaymentStripeException stripeEx = PaymentStripeException.Compose(response, errorMessage, wex); if (stripeEx != null) { throw stripeEx; } } } throw new PaymentStripeException(StringConsts.PAYMENT_CANNOT_CAPTURE_CAPTURED_PAYMENT_ERROR + this.GetType() + " .Refund(session='{0}', charge='{1}')".Args(session, charge), ex); } }
private Transaction doTransfer(StripeSession session, string recipientID, Account customerAccount, Amount amount, string description, object extraData = null) { var actualAccountData = session.FetchAccountData(customerAccount); try { var prms = new WebClient.RequestParams(this) { UserName = session.SecretKey, Method = HTTPRequestMethod.POST, BodyParameters = new Dictionary <string, string>() { { PRM_RECIPIENT, recipientID }, { PRM_AMOUNT, ((int)((amount.Value * 100))).ToString() }, { PRM_CURRENCY, amount.CurrencyISO.ToLower() }, { PRM_DESCRIPTION, description } } }; dynamic obj = WebClient.GetJsonAsDynamic(new Uri(TRANSFER_URI), prms); var created = ((long)obj.created).FromSecondsSinceUnixEpochStart(); var taId = session.GenerateTransactionID(TransactionType.Transfer); var ta = new Transaction(taId, TransactionType.Transfer, TransactionStatus.Success, Account.EmptyInstance, customerAccount, this.Name, obj.id, created, amount, description: description, extraData: extraData); StatTransfer(amount); return(ta); } catch (Exception ex) { StatTransferError(); var wex = ex as System.Net.WebException; if (wex != null) { var response = wex.Response as System.Net.HttpWebResponse; if (response != null) { string errorMessage = this.GetType().Name + ".doTransfer(recipientID='{0}', customerAccount='{1}', amount='{2}')".Args(recipientID, actualAccountData, amount); PaymentStripeException stripeEx = PaymentStripeException.Compose(response, errorMessage, wex); if (stripeEx != null) { throw stripeEx; } } } throw new PaymentStripeException(StringConsts.PAYMENT_CANNOT_TRANSFER_ERROR + this.GetType() + " .doTransfer(customerAccout='{0}')".Args(actualAccountData), ex); } }
/// <summary> /// Creates new recipient in Stripe system. /// Is used as a temporary entity to substitute recipient parameter in Transfer operation then deleted /// </summary> private string createRecipient(StripeSession session, Account recipientAccount, string description) { var recipientActualAccountData = session.FetchAccountData(recipientAccount); try { var bodyPrms = new Dictionary <string, string>() { { PRM_NAME, recipientActualAccountData.AccountTitle }, { PRM_TYPE, recipientActualAccountData.AccountType == AccountType.Corporation ? "corporation" : "individual" }, { PRM_EMAIL, recipientActualAccountData.BillingAddress.EMail } }; fillBodyParametersFromAccount(bodyPrms, recipientActualAccountData); var prms = new WebClient.RequestParams(this) { UserName = session.SecretKey, Method = HTTPRequestMethod.POST, BodyParameters = bodyPrms }; dynamic obj = WebClient.GetJsonAsDynamic(new Uri(RECIPIENT_URI), prms); return(obj.id); } catch (Exception ex) { var wex = ex as System.Net.WebException; if (wex != null) { var response = wex.Response as System.Net.HttpWebResponse; if (response != null) { string errorMessage = this.GetType().Name + ".createRecipient(customerAccout='{0}')".Args(recipientActualAccountData); PaymentStripeException stripeEx = PaymentStripeException.Compose(response, errorMessage, wex); if (stripeEx != null) { throw stripeEx; } } } throw new PaymentStripeException(StringConsts.PAYMENT_CANNOT_CREATE_RECIPIENT_ERROR + this.GetType() + " .Refund(customerAccout='{0}')".Args(recipientActualAccountData), ex); } }
private Transaction doCharge(StripeSession session, Account from, Account to, Amount amount, bool capture = true, string description = null, object extraData = null) { var fromActualData = session.FetchAccountData(from); try { var bodyPrms = new Dictionary <string, string>() { { PRM_AMOUNT, ((int)((amount.Value * 100))).ToString() }, { PRM_CURRENCY, amount.CurrencyISO.ToString().ToLower() }, { PRM_DESCRIPTION, description }, { PRM_CAPTURE, capture.ToString() } }; fillBodyParametersFromAccount(bodyPrms, fromActualData); var prms = new WebClient.RequestParams(this) { UserName = session.SecretKey, Method = HTTPRequestMethod.POST, BodyParameters = bodyPrms }; dynamic obj = WebClient.GetJsonAsDynamic(new Uri(CHARGE_URI), prms); var created = ((long)obj.created).FromSecondsSinceUnixEpochStart(); var taId = session.GenerateTransactionID(TransactionType.Charge); var ta = new Transaction(taId, TransactionType.Charge, TransactionStatus.Success, from, to, this.Name, obj.AsGDID, created, amount, description: description, extraData: extraData); if (capture) { ta.__Apply(Transaction.Operation.Capture(TransactionStatus.Success, created, token: obj.AsGDID, description: description, amount: amount.Value, extraData: extraData)); } StatCharge(amount); return(ta); } catch (Exception ex) { StatChargeError(); var wex = ex as System.Net.WebException; if (wex != null) { var response = wex.Response as System.Net.HttpWebResponse; if (response != null) { string errorMessage = this.GetType().Name + ".Charge(money: {0}, card: {1}, amount: '{2}')".Args(amount.Value, fromActualData.AccountID, amount); var stripeEx = PaymentStripeException.Compose(response, errorMessage, wex); throw stripeEx; } } throw new PaymentStripeException(StringConsts.PAYMENT_CANNOT_CHARGE_PAYMENT_ERROR + this.GetType() + " .Capture(session='{0}', card='{1}', amount='{2}')".Args(session, from, amount), ex); } }