Exemplo n.º 1
0
        /// <summary>
        /// Overload of Charge method with Stripe-typed session parameter
        /// </summary>
        public Transaction Charge(StripeSession session, ITransactionContext context, Account from, Account to, Amount amount, bool capture = true, string description = null, object extraData = null)
        {
            var fromActualData = PaySystemHost.AccountToActualData(context, 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()
                {
                    Uri            = new Uri(CHARGE_URI),
                    Caller         = this,
                    UName          = session.SecretKey,
                    Method         = HTTPRequestMethod.POST,
                    BodyParameters = bodyPrms
                };

                dynamic obj = WebClient.GetJson(prms);

                var created = ((long)obj.created).FromSecondsSinceUnixEpochStart();

                var taId = PaySystemHost.GenerateTransactionID(session, context, TransactionType.Charge);

                var ta = new Transaction(taId, TransactionType.Charge, from, to, this.Name, obj.id, amount, created, description);

                StatCharge(amount);

                return(ta);
            }
            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 +
                                              ".Charge(money: {0}, card: {1}, amount: '{2}')".Args(amount.Value, fromActualData.AccountNumber, amount);
                        var stripeEx = PaymentStripeException.Compose(response, errorMessage, wex);
                        throw stripeEx;
                    }
                }

                StatChargeError();

                throw new PaymentStripeException(StringConsts.PAYMENT_CANNOT_CHARGE_PAYMENT_ERROR + this.GetType()
                                                 + " .Capture(session='{0}', card='{1}', amount='{2}')".Args(session, from, amount), ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Transfers funds to customerAccount from current stripe account
        /// (which credentials is supplied in current session)
        /// </summary>
        private Transaction transfer(StripeSession stripeSession, ITransactionContext context, string recipientID, Account customerAccount, Amount amount, string description)
        {
            var actualAccountData = PaySystemHost.AccountToActualData(context, customerAccount);

            try
            {
                var prms = new WebClient.RequestParams()
                {
                    Uri            = new Uri(TRANSFER_URI),
                    Caller         = this,
                    UName          = stripeSession.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.GetJson(prms);

                var created = ((long)obj.created).FromSecondsSinceUnixEpochStart();

                var taId = PaySystemHost.GenerateTransactionID(stripeSession, context, TransactionType.Transfer);

                var ta = new Transaction(taId, TransactionType.Transfer, Account.EmptyInstance, customerAccount, this.Name, obj.id, amount, created, description);

                StatTransfer(amount);

                return(ta);
            }
            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 +
                                              ".transfer(recipientID='{0}', customerAccount='{1}', amount='{2}')".Args(recipientID, actualAccountData, amount);
                        PaymentStripeException stripeEx = PaymentStripeException.Compose(response, errorMessage, wex);
                        if (stripeEx != null)
                        {
                            throw stripeEx;
                        }
                    }
                }

                StatTransferError();

                throw new PaymentStripeException(StringConsts.PAYMENT_CANNOT_TRANSFER_ERROR + this.GetType()
                                                 + " .transfer(customerAccout='{0}')".Args(actualAccountData), ex);
            }
        }
Exemplo n.º 3
0
        public void SyncUnclaimedPayoutTest()
        {
            var ps = PaySystemSync;

            var amount = new Amount("USD", 1.0m);
            var from   = new Account("SYSTEM", 111, 222);
            var to     = new Account("USER", 111, 222);

            object id = null;

            using (var session = ps.StartSession())
            {
                session.StoreAccountData(new ActualAccountData(from)
                {
                    Identity   = from.Identity,
                    IdentityID = from.IdentityID,
                    AccountID  = from.AccountID
                });

                session.StoreAccountData(new ActualAccountData(to)
                {
                    Identity   = from.Identity,
                    IdentityID = from.IdentityID,
                    AccountID  = "*****@*****.**"
                });

                var tran = session.Transfer(from, to, amount);
                Assert.IsNotNull(tran);
                Assert.IsNotNull(tran.ID);
                Assert.IsTrue(tran.Token.AsString().Split(':').Length == 3);
                id = tran.ID;
                Assert.AreEqual(TransactionType.Transfer, tran.Type);
                Assert.AreEqual(amount, tran.Amount);
                Assert.AreEqual(from, tran.From);
                Assert.AreEqual(to, tran.To);
                Assert.AreEqual(TransactionStatus.Unclaimed, tran.Status);
            }

            var transaction = PaySystemHost.FetchTransaction(id);

            Assert.IsNotNull(transaction);
            Assert.AreEqual(TransactionType.Transfer, transaction.Type);
            Assert.AreEqual(amount, transaction.Amount);
            Assert.AreEqual(from, transaction.From);
            Assert.AreEqual(to, transaction.To);
            Assert.AreEqual(TransactionStatus.Unclaimed, transaction.Status);

            var voided = transaction.Void();

            Assert.IsTrue(voided);
            Assert.AreEqual(TransactionType.Transfer, transaction.Type);
            Assert.AreEqual(amount, transaction.Amount);
            Assert.AreEqual(from, transaction.From);
            Assert.AreEqual(to, transaction.To);
            Assert.AreEqual(TransactionStatus.Refunded, transaction.Status);
        }
Exemplo n.º 4
0
        /// <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 stripeSession, ITransactionContext context, Account recipientAccount, string description)
        {
            var recipientActualAccountData = PaySystemHost.AccountToActualData(context, recipientAccount);

            try
            {
                var bodyPrms = new Dictionary <string, string>()
                {
                    { PRM_NAME, recipientActualAccountData.AccountTitle },
                    { PRM_TYPE, recipientActualAccountData.AccountType == AccountType.Corporation ? "corporation" : "individual" },
                    { PRM_EMAIL, recipientActualAccountData.BillingEmail }
                };

                fillBodyParametersFromAccount(bodyPrms, recipientActualAccountData);

                var prms = new WebClient.RequestParams()
                {
                    Uri            = new Uri(RECIPIENT_URI),
                    Caller         = this,
                    UName          = stripeSession.SecretKey,
                    Method         = HTTPRequestMethod.POST,
                    BodyParameters = bodyPrms
                };

                dynamic obj = WebClient.GetJson(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);
            }

            throw new NotImplementedException();
        }
Exemplo n.º 5
0
        public override Transaction Refund(PaySession session, ITransactionContext context, ref Transaction charge, Financial.Amount?amount = null, string description = null, object extraData = null)
        {
            var refundAmount = amount ?? charge.Amount;

            var created = DateTime.UtcNow;

            var taId = PaySystemHost.GenerateTransactionID(session, context, TransactionType.Refund);

            var refundTA = Transaction.Refund(taId, this.Name, taId, Account.EmptyInstance, charge.From, refundAmount, created, description, relatedTransactionID: charge.ID);

            StatRefund(charge, amount);

            return(refundTA);
        }
Exemplo n.º 6
0
        private Transaction doTransfer(PaySession session, ITransactionContext context, Account from, Account to, Amount amount, string description = null, object extraData = null)
        {
            var id = Log(MessageType.Info, "doTransfer()", StringConsts.PAYPAL_PAYOUT_MESSAGE.Args(to, amount));

            try
            {
                var payPalSession     = session as PayPalSession;
                var actualAccountData = PaySystemHost.AccountToActualData(context, to);

                var request = new WebClient.RequestParams
                {
                    Caller          = this,
                    Uri             = new Uri(m_ApiUri + URI_PAYOUTS),
                    QueryParameters = new Dictionary <string, string>
                    {
                        { PRM_SYNC_MODE, m_SyncMode.AsString() }
                    },
                    Method      = HTTPRequestMethod.POST,
                    ContentType = ContentType.JSON,
                    Headers     = new Dictionary <string, string>
                    {
                        { HDR_AUTHORIZATION, HDR_AUTHORIZATION_OAUTH.Args(payPalSession.AuthorizationToken.AccessToken) },
                    },
                    Body = getPayoutJSONBody(actualAccountData, amount, description)
                };

                var response = WebClient.GetJson(request);
                Log(MessageType.Info, "doTransfer()", response.ToJSON(), null, id);

                checkPayoutStatus(response, payPalSession);

                var transaction = createPayoutTransaction(session, context, response, to, amount, description);

                StatTransfer(amount);

                return(transaction);
            }
            catch (Exception ex)
            {
                StatTransferError();

                var message = StringConsts.PAYPAL_PAYOUT_ERROR.Args(to, amount, ex.ToMessageWithType());
                var error   = PayPalPaymentException.ComposeError(message, ex);
                Log(MessageType.Error, "doTransfer()", error.Message, ex, id);

                throw error;
            }
        }
Exemplo n.º 7
0
        public void DuplicatePayoutTest()
        {
            var ps = PaySystemSync;

            var amount = new Amount("USD", 1.0m);
            var from   = new Account("SYSTEM", 111, 222);
            var to     = new Account("USER", 111, 222);

            object id = null;

            using (var session = ps.StartSession())
            {
                session.StoreAccountData(new ActualAccountData(from)
                {
                    Identity   = from.Identity,
                    IdentityID = from.IdentityID,
                    AccountID  = from.AccountID
                });

                session.StoreAccountData(new ActualAccountData(to)
                {
                    Identity   = from.Identity,
                    IdentityID = from.IdentityID,
                    AccountID  = PaySystemHost.PaypalValidAccount
                });

                var tran = session.Transfer(from, to, amount);
                Assert.IsNotNull(tran);
                Assert.IsNotNull(tran.ID);
                Assert.IsTrue(tran.Token.AsString().Split(':').Length == 3);
                id = tran.ID;
                Assert.AreEqual(TransactionType.Transfer, tran.Type);
                Assert.AreEqual(amount, tran.Amount);
                Assert.AreEqual(from, tran.From);
                Assert.AreEqual(to, tran.To);
                Assert.AreEqual(TransactionStatus.Success, tran.Status);
            }

            PaySystemHost.SetNextTransactionID(id);

            using (var session = ps.StartSession())
                session.Transfer(from, to, amount);
        }
Exemplo n.º 8
0
        public void AsyncPayoutTest()
        {
            var ps = PaySystemAsync;

            var amount = new Amount("USD", 1.0m);
            var from   = new Account("SYSTEM", 111, 222);
            var to     = new Account("USER", 111, 222);

            object id = null;

            using (var session = ps.StartSession())
            {
                session.StoreAccountData(new ActualAccountData(from)
                {
                    Identity   = from.Identity,
                    IdentityID = from.IdentityID,
                    AccountID  = from.AccountID
                });

                session.StoreAccountData(new ActualAccountData(to)
                {
                    Identity   = from.Identity,
                    IdentityID = from.IdentityID,
                    AccountID  = PaySystemHost.PaypalValidAccount
                });

                var tran = session.Transfer(from, to, amount);
                Assert.IsNotNull(tran);
                Assert.IsNotNull(tran.ID);
                id = tran.ID;
                Assert.AreEqual(TransactionType.Transfer, tran.Type);
                Assert.AreEqual(amount, tran.Amount);
                Assert.AreEqual(from, tran.From);
                Assert.AreEqual(to, tran.To);
                Assert.AreEqual(TransactionStatus.Pending, tran.Status);

                Assert.IsNotNull(session.User.AuthToken.Data); // token occured on first call
                Assert.IsInstanceOf <PayPalOAuthToken>(session.User.AuthToken.Data);

                var token = session.User.AuthToken.Data as PayPalOAuthToken;
                Assert.IsTrue(token.ObtainTime > App.TimeSource.Now.AddMinutes(-1));
                Assert.IsTrue(token.ObtainTime < App.TimeSource.Now);
                Assert.AreEqual(3600, token.ExpirationMarginSec);
                Assert.IsNotNullOrEmpty(token.ApplicationID);
                Assert.IsTrue(token.ExpiresInSec > 0);
                Assert.IsNotNullOrEmpty(token.AccessToken);
                Assert.IsNotNullOrEmpty(token.Scope);
                Assert.IsNotNullOrEmpty(token.Nonce);
            }

            var transaction = PaySystemHost.FetchTransaction(id);

            Assert.IsNotNull(transaction);
            for (int i = 0; i < 5 && (i == 0 || !transaction.Refresh()); i++)
            {
                if (i != 0)
                {
                    Console.WriteLine("...try refresh #" + i);
                }

                Assert.AreEqual(TransactionType.Transfer, transaction.Type);
                Assert.AreEqual(amount, transaction.Amount);
                Assert.AreEqual(from, transaction.From);
                Assert.AreEqual(to, transaction.To);
                Assert.AreEqual(TransactionStatus.Pending, transaction.Status);

                var sleep = ExternalRandomGenerator.Instance.NextScaledRandomInteger(5000, 10000);
                Console.WriteLine("Sleep {0} ms...".Args(sleep));
                Thread.Sleep(sleep);
            }

            Assert.IsNotNull(transaction);
            Assert.IsTrue(transaction.Token.AsString().Split(':').Length == 3);
            Assert.AreEqual(TransactionType.Transfer, transaction.Type);
            Assert.AreEqual(amount, transaction.Amount);
            Assert.AreEqual(from, transaction.From);
            Assert.AreEqual(to, transaction.To);
            Assert.AreEqual(TransactionStatus.Success, transaction.Status);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Overload of Refund method with Stripe-typed session parameter
        /// Developers, don't call this method directly. Call Transaction.Refund instead.
        /// </summary>
        public Transaction Refund(StripeSession session, ITransactionContext context, ref Transaction charge, Amount?amount = null, string description = null, object extraData = null)
        {
            var fromActualData = PaySystemHost.AccountToActualData(context, charge.From);

            var refundAmount = amount ?? charge.Amount;

            try
            {
                var bodyPrms = new Dictionary <string, string>()
                {
                    { PRM_AMOUNT, ((int)((refundAmount.Value * 100))).ToString() }
                };

                if (description.IsNotNullOrWhiteSpace())
                {
                    bodyPrms.Add(PRM_REASON, description);
                }

                var prms = new WebClient.RequestParams()
                {
                    Uri            = new Uri(REFUND_URI.Args(charge.ProcessorToken)),
                    Caller         = this,
                    UName          = session.SecretKey,
                    Method         = HTTPRequestMethod.POST,
                    BodyParameters = bodyPrms
                };

                dynamic obj = WebClient.GetJson(prms);

                dynamic lastRefund = ((NFX.Serialization.JSON.JSONDataArray)obj.refunds.Data).First();

                var created = ((long)obj.created).FromSecondsSinceUnixEpochStart();

                var taId = PaySystemHost.GenerateTransactionID(session, context, TransactionType.Refund);

                var refundTA = new Transaction(taId, TransactionType.Refund, Account.EmptyInstance, charge.From, this.Name,
                                               lastRefund["id"], refundAmount, created, description,
                                               isCaptured: true, canRefund: false);

                StatRefund(charge, amount);

                return(refundTA);
            }
            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 +
                                              ".Refund(money: {0}, card: {1}, description: '{2}')"
                                              .Args(charge.Amount, fromActualData.AccountNumber, description);
                        PaymentStripeException stripeEx = PaymentStripeException.Compose(response, errorMessage, wex);
                        if (stripeEx != null)
                        {
                            throw stripeEx;
                        }
                    }
                }

                StatRefundError();

                throw new PaymentStripeException(StringConsts.PAYMENT_CANNOT_CAPTURE_CAPTURED_PAYMENT_ERROR + this.GetType()
                                                 + " .Refund(session='{0}', charge='{1}')".Args(session, charge), ex);
            }
        }
Exemplo n.º 10
0
        public override Transaction Transfer(PaySession session, ITransactionContext context, Account from, Account to, Financial.Amount amount, string description = null, object extraData = null)
        {
            var actualAccountData = PaySystemHost.AccountToActualData(context, to);

            if (actualAccountData == null)
            {
                StatTransferError();
                throw new PaymentMockException(StringConsts.PAYMENT_UNKNOWN_ACCOUNT_ERROR.Args(from) + this.GetType().Name + ".Transfer");
            }

            AccountData accountData = null;

            accountData = m_Accounts.DebitBankCorrect.FirstOrDefault(c => c.AccountNumber == actualAccountData.AccountNumber &&
                                                                     c.CardExpirationYear == actualAccountData.CardExpirationYear &&
                                                                     c.CardExpirationMonth == actualAccountData.CardExpirationMonth &&
                                                                     c.CardVC == actualAccountData.CardVC);

            if (accountData != null)
            {
                var created = DateTime.Now;

                var taId = PaySystemHost.GenerateTransactionID(session, context, TransactionType.Transfer);

                var ta = new Transaction(taId, TransactionType.Transfer, Account.EmptyInstance, to, this.Name, taId, amount, created, description);

                StatTransfer(amount);

                return(ta);
            }

            accountData = m_Accounts.DebitCardCorrect.FirstOrDefault(c => c.AccountNumber == actualAccountData.AccountNumber &&
                                                                     c.CardExpirationYear == actualAccountData.CardExpirationYear &&
                                                                     c.CardExpirationMonth == actualAccountData.CardExpirationMonth &&
                                                                     c.CardVC == actualAccountData.CardVC);

            if (accountData != null)
            {
                var created = DateTime.Now;

                var taId = PaySystemHost.GenerateTransactionID(session, context, TransactionType.Transfer);

                var ta = new Transaction(taId, TransactionType.Transfer, Account.EmptyInstance, to, this.Name, taId, amount, created, description);

                StatTransfer(amount);

                return(ta);
            }

            accountData = m_Accounts.DebitCardCorrectWithAddr.FirstOrDefault(c => c.AccountNumber == actualAccountData.AccountNumber &&
                                                                             c.CardExpirationYear == actualAccountData.CardExpirationYear &&
                                                                             c.CardExpirationMonth == actualAccountData.CardExpirationMonth &&
                                                                             c.CardVC == actualAccountData.CardVC &&
                                                                             c.BillingAddress1 != actualAccountData.BillingAddress1 &&
                                                                             c.BillingAddress2 != actualAccountData.BillingAddress2 &&
                                                                             c.BillingCountry != actualAccountData.BillingCountry &&
                                                                             c.BillingCity != actualAccountData.BillingCity &&
                                                                             c.BillingPostalCode != actualAccountData.BillingPostalCode &&
                                                                             c.BillingRegion != actualAccountData.BillingRegion &&
                                                                             c.BillingEmail != actualAccountData.BillingEmail &&
                                                                             c.BillingPhone != actualAccountData.BillingPhone);

            if (accountData != null)
            {
                var created = DateTime.Now;

                var taId = PaySystemHost.GenerateTransactionID(session, context, TransactionType.Transfer);

                var ta = new Transaction(taId, TransactionType.Transfer, Account.EmptyInstance, to, this.Name, taId, amount, created, description);

                StatTransfer(amount);

                return(ta);
            }

            StatTransferError();
            throw new PaymentException(StringConsts.PAYMENT_INVALID_CARD_NUMBER_ERROR + this.GetType().Name + ".Transfer");
        }
Exemplo n.º 11
0
        public override Transaction Charge(PaySession session, ITransactionContext context, Account from, Account to, Financial.Amount amount, bool capture = true, string description = null, object extraData = null)
        {
            var fromActualData = PaySystemHost.AccountToActualData(context, from);

            if (fromActualData == null)
            {
                StatChargeError();
                throw new PaymentMockException(StringConsts.PAYMENT_UNKNOWN_ACCOUNT_ERROR.Args(from) + this.GetType().Name + ".Charge");
            }

            if (m_Accounts.CreditCardDeclined.Any(c => c.AccountNumber == fromActualData.AccountNumber))
            {
                StatChargeError();
                throw new PaymentMockException(this.GetType().Name + ".Charge: card '{0}' declined".Args(fromActualData));
            }

            if (m_Accounts.CreditCardLuhnError.Any(c => c.AccountNumber == fromActualData.AccountNumber))
            {
                StatChargeError();
                throw new PaymentMockException(this.GetType().Name + ".Charge: card number '{0}' is incorrect".Args(fromActualData));
            }


            AccountData foundAccount = null;

            foundAccount = m_Accounts.CreditCardsCorrect.FirstOrDefault(c => c.AccountNumber == fromActualData.AccountNumber);

            if (foundAccount != null)
            {
                if (foundAccount.CardExpirationYear != fromActualData.CardExpirationYear)
                {
                    StatChargeError();
                    throw new PaymentMockException(StringConsts.PAYMENT_INVALID_EXPIRATION_DATE_ERROR
                                                   .Args(fromActualData.CardExpirationYear, fromActualData.CardExpirationMonth) + this.GetType().Name + ".Charge");
                }

                if (foundAccount.CardExpirationMonth != fromActualData.CardExpirationMonth)
                {
                    StatChargeError();
                    throw new PaymentMockException(StringConsts.PAYMENT_INVALID_EXPIRATION_DATE_ERROR
                                                   .Args(fromActualData.CardExpirationYear, fromActualData.CardExpirationMonth) + this.GetType().Name + ".Charge");
                }

                if (foundAccount.CardVC != fromActualData.CardVC)
                {
                    StatChargeError();
                    throw new PaymentMockException(StringConsts.PAYMENT_INVALID_CVC_ERROR + this.GetType().Name + ".Charge");
                }

                var created = DateTime.UtcNow;

                var taId = PaySystemHost.GenerateTransactionID(session, context, TransactionType.Charge);

                var ta = new Transaction(taId, TransactionType.Charge, from, to, this.Name, taId, amount, created, description);

                StatCharge(amount);

                return(ta);
            }

            foundAccount = m_Accounts.CreditCardCorrectWithAddr.FirstOrDefault(c => c.AccountNumber == fromActualData.AccountNumber);

            if (foundAccount != null)
            {
                if (foundAccount.CardExpirationYear != fromActualData.CardExpirationYear)
                {
                    StatChargeError();
                    throw new PaymentMockException(StringConsts.PAYMENT_INVALID_EXPIRATION_DATE_ERROR
                                                   .Args(fromActualData.CardExpirationYear, fromActualData.CardExpirationMonth) + this.GetType().Name + ".Charge");
                }

                if (foundAccount.CardExpirationMonth != fromActualData.CardExpirationMonth)
                {
                    StatChargeError();
                    throw new PaymentMockException(StringConsts.PAYMENT_INVALID_EXPIRATION_DATE_ERROR
                                                   .Args(fromActualData.CardExpirationYear, fromActualData.CardExpirationMonth) + this.GetType().Name + ".Charge");
                }

                if (foundAccount.CardVC != fromActualData.CardVC)
                {
                    StatChargeError();
                    throw new PaymentMockException(StringConsts.PAYMENT_INVALID_CVC_ERROR.Args(fromActualData.CardVC) + this.GetType().Name + ".Charge");
                }

                if (foundAccount.BillingAddress1 != fromActualData.BillingAddress1 ||
                    foundAccount.BillingAddress2 != fromActualData.BillingAddress2 ||
                    foundAccount.BillingCountry != fromActualData.BillingCountry ||
                    foundAccount.BillingCity != fromActualData.BillingCity ||
                    foundAccount.BillingPostalCode != fromActualData.BillingPostalCode ||
                    foundAccount.BillingRegion != fromActualData.BillingRegion ||
                    foundAccount.BillingEmail != fromActualData.BillingEmail ||
                    foundAccount.BillingPhone != fromActualData.BillingPhone)
                {
                    StatChargeError();
                    throw new PaymentMockException(StringConsts.PAYMENT_INVALID_ADDR_ERROR + this.GetType().Name + ".Charge");
                }

                var created = DateTime.UtcNow;

                var taId = PaySystemHost.GenerateTransactionID(session, context, TransactionType.Charge);

                var ta = new Transaction(taId, TransactionType.Charge, from, to, this.Name, taId, amount, created, description);

                StatCharge(amount);

                return(ta);
            }

            throw new PaymentException(StringConsts.PAYMENT_INVALID_CARD_NUMBER_ERROR + this.GetType().Name + ".Charge");
        }
Exemplo n.º 12
0
        public Transaction Charge(BraintreeSession session, ITransactionContext context, Account from, Account to, Amount amount, bool capture = true, string description = null, object extraData = null)
        {
            var orderContex = context as IOrderTransactionContext;

            var fromActualData = PaySystemHost.AccountToActualData(context, from);

            try
            {
                var isWeb   = fromActualData.Account.IsWebTerminalToken;
                var request = new JSONDataMap();

                var transaction = (JSONDataMap)(request["transaction"] = new JSONDataMap());
                transaction["type"]   = "sale";
                transaction["amount"] = amount.Value;
                transaction[isWeb ? "payment_method_nonce" : "payment_method_token"] = fromActualData.Account.AccountID;
                if (orderContex != null)
                {
                    transaction["order_id"] = orderContex.OrderId;
                    if (orderContex.IsNewCustomer)
                    {
                        var customer = (JSONDataMap)(transaction["customer"] = new JSONDataMap());
                        customer["id"] = orderContex.CustomerId;
                    }
                    else
                    {
                        transaction["customer_id"] = orderContex.CustomerId;
                    }
                }

                var billing = (JSONDataMap)(transaction["billing"] = new JSONDataMap());
                if (fromActualData.FirstName.IsNotNullOrWhiteSpace())
                {
                    billing["first_name"] = fromActualData.FirstName;
                }
                if (fromActualData.LastName.IsNotNullOrWhiteSpace())
                {
                    billing["last_name"] = fromActualData.LastName;
                }
                if (fromActualData.BillingAddress.Address1.IsNotNullOrWhiteSpace())
                {
                    billing["street_address"] = fromActualData.BillingAddress.Address1;
                }
                if (fromActualData.BillingAddress.Address2.IsNotNullOrWhiteSpace())
                {
                    billing["extended_address"] = fromActualData.BillingAddress.Address2;
                }
                if (fromActualData.BillingAddress.City.IsNotNullOrWhiteSpace())
                {
                    billing["locality"] = fromActualData.BillingAddress.City;
                }
                if (fromActualData.BillingAddress.Country.IsNotNullOrWhiteSpace())
                {
                    billing["country_code_alpha3"] = fromActualData.BillingAddress.Country;
                }
                if (fromActualData.BillingAddress.Region.IsNotNullOrWhiteSpace())
                {
                    billing["region"] = fromActualData.BillingAddress.Region;
                }
                if (fromActualData.BillingAddress.PostalCode.IsNotNullOrWhiteSpace())
                {
                    billing["postal_code"] = fromActualData.BillingAddress.PostalCode;
                }
                if (fromActualData.BillingAddress.Company.IsNotNullOrWhiteSpace())
                {
                    billing["company"] = fromActualData.BillingAddress.Company;
                }

                var options = (JSONDataMap)(transaction["options"] = new JSONDataMap());
                options["submit_for_settlement"] = capture;
                if (orderContex != null)
                {
                    options["store_in_vault_on_success"] = isWeb;
                }

                dynamic obj = getResponse(session, URI_Transactions(session.MerchantID), HTTPRequestMethod.POST, request);

                string created = obj.transaction.createdAt;

                var taId = PaySystemHost.GenerateTransactionID(session, context, TransactionType.Charge);

                string customerID    = obj.transaction.customer.id;
                string token         = obj.transaction.creditCard.token;
                string transactionID = obj.transaction.id;

                var ta = Transaction.Charge(taId, this.Name, "{0}:{1}:{2}".Args(customerID, token, transactionID), from, to, amount, created.AsDateTime(), description);

                StatCharge(amount);

                return(ta);
            }
            catch (Exception ex)
            {
                StatChargeError();

                var wex = ex as System.Net.WebException;
                if (wex != null)
                {
                    using (var sr = new System.IO.StreamReader(wex.Response.GetResponseStream()))
                    {
                        var respStr = sr.ReadToEnd();
                        var resp    = respStr.IsNotNullOrWhiteSpace() ? respStr.JSONToDynamic() : null;
                        // TODO Exception Handilng
                    }
                }

                throw new PaymentException(StringConsts.PAYMENT_CANNOT_CHARGE_PAYMENT_ERROR + this.GetType()
                                           + " .Capture(session='{0}', card='{1}', amount='{2}')".Args(session, from, amount), ex);
            }
        }
Exemplo n.º 13
0
        public Transaction Charge(BraintreeSession session, ITransactionContext context, Account from, Account to, Amount amount, bool capture = true, string description = null, object extraData = null)
        {
            var orderContex = context as IOrderTransactionContext;

            var fromActualData = PaySystemHost.AccountToActualData(context, from);

            try
            {
                var isWeb       = fromActualData.Account.IsWebTerminalToken;
                var transaction = new XElement("transaction",
                                               new XElement("type", "sale"),
                                               new XElement("amount", amount.Value),
                                               new XElement(isWeb ? "payment-method-nonce" : "payment-method-token", fromActualData.Account.AccountID));
                if (orderContex != null)
                {
                    transaction.Add(new XElement("order-id", orderContex.OrderId));
                    if (isWeb)
                    {
                        if (orderContex.IsNewCustomer)
                        {
                            transaction.Add(new XElement("customer", new XElement("id", orderContex.CustomerId)));
                        }
                        else
                        {
                            try
                            {
                                getResponse(session, URI_Customer(session.MerchantID, orderContex.CustomerId.AsString()), method: HTTPRequestMethod.GET);
                                transaction.Add(new XElement("customer-id", orderContex.CustomerId));
                            }
                            catch
                            {
                                transaction.Add(new XElement("customer", new XElement("id", orderContex.CustomerId)));
                            }
                        }
                    }
                }
                if (isWeb)
                {
                    var billing = new XElement("billing");

                    if (fromActualData.FirstName.IsNotNullOrWhiteSpace())
                    {
                        billing.Add(new XElement("first-name", fromActualData.FirstName));
                    }
                    if (fromActualData.LastName.IsNotNullOrWhiteSpace())
                    {
                        billing.Add(new XElement("last-name", fromActualData.LastName));
                    }
                    if (fromActualData.BillingAddress.Address1.IsNotNullOrWhiteSpace())
                    {
                        billing.Add(new XElement("street-address", fromActualData.BillingAddress.Address1));
                    }
                    if (fromActualData.BillingAddress.Address2.IsNotNullOrWhiteSpace())
                    {
                        billing.Add(new XElement("extended-address", fromActualData.BillingAddress.Address2));
                    }
                    if (fromActualData.BillingAddress.City.IsNotNullOrWhiteSpace())
                    {
                        billing.Add(new XElement("locality", fromActualData.BillingAddress.City));
                    }
                    if (fromActualData.BillingAddress.Country.IsNotNullOrWhiteSpace())
                    {
                        billing.Add(new XElement("country-code-alpha3", fromActualData.BillingAddress.Country));
                    }
                    if (fromActualData.BillingAddress.Region.IsNotNullOrWhiteSpace())
                    {
                        billing.Add(new XElement("region", fromActualData.BillingAddress.Region));
                    }
                    if (fromActualData.BillingAddress.PostalCode.IsNotNullOrWhiteSpace())
                    {
                        billing.Add(new XElement("postal-code", fromActualData.BillingAddress.PostalCode));
                    }
                    if (fromActualData.BillingAddress.Company.IsNotNullOrWhiteSpace())
                    {
                        billing.Add(new XElement("company", fromActualData.BillingAddress.Company));
                    }

                    transaction.Add(billing);
                }

                var options = new XElement("options", new XElement("submit-for-settlement", capture));
                if (orderContex != null && isWeb)
                {
                    options.Add(new XElement("store-in-vault-on-success", true));
                }

                transaction.Add(options);

                var response = getResponse(session, URI_Transactions(session.MerchantID), new XDocument(transaction));

                transaction = response.Root;

                var transactionID = transaction.Element("id").Value;
                var status        = transaction.Element("status").Value;
                var captured      = status.EqualsOrdIgnoreCase("submitted_for_settlement");
                var created       = transaction.Element("created-at").Value.AsDateTime();
                var customerID    = transaction.Element("customer").Element("id").Value;
                var token         = transaction.Element("credit-card").Element("token").Value;

                var amountAuth     = new Amount(amount.CurrencyISO, transaction.Element("amount").Value.AsDecimal());
                var amountCaptured = captured ? (Amount?)amountAuth : null;

                var taId = PaySystemHost.GenerateTransactionID(session, context, TransactionType.Charge);
                var ta   = Transaction.Charge(taId, Name,
                                              "{0}:{1}:{2}".Args(customerID, token, transactionID),
                                              from, to,
                                              amountAuth, created, description,
                                              amountCaptured: amountCaptured);

                StatCharge(amount);

                return(ta);
            }
            catch (Exception ex)
            {
                StatChargeError();
                if (ex is PaymentException)
                {
                    throw ex;
                }
                throw new PaymentException(StringConsts.PAYMENT_CANNOT_CHARGE_PAYMENT_ERROR + GetType().Name
                                           + " .Charge(session='{0}', card='{1}', amount='{2}')".Args(session, from, amount), ex);
            }
        }