Пример #1
0
 /// <summary>
 /// Returns the customer information on Braintree for the given userID,
 /// or null if not exists.
 /// </summary>
 /// <param name="userID"></param>
 /// <param name="gateway">Optional, to reuse an opened gateway, else a new one is transparently created</param>
 /// <returns></returns>
 public static Braintree.Customer GetBraintreeCustomer(int userID, BraintreeGateway gateway = null)
 {
     gateway = LcPayment.NewBraintreeGateway(gateway);
     try {
         return(gateway.Customer.Find(GetCustomerId(userID)));
     } catch (Braintree.Exceptions.NotFoundException ex) {
     }
     return(null);
 }
Пример #2
0
        /// <summary>
        /// Saves/updates the payment method for the member, at the remote gateway,
        /// updates in place, and returns, the ID/token
        /// </summary>
        /// <returns>The saved payment method ID/token</returns>
        /// <param name="paymentData"></param>
        private static string CollectPaymentMethod(LcPayment.InputPaymentMethod paymentData, int memberUserID)
        {
            // On emulation, discard other steps, just generate
            // a fake ID
            if (LcPayment.TESTING_EMULATEBRAINTREE)
            {
                paymentData.paymentMethodID = LcPayment.CreateFakePaymentMethodId();
                return(paymentData.paymentMethodID);
            }

            // Standard way
            var gateway = LcPayment.NewBraintreeGateway();

            // The input paymentID must be one generated by Braintree, reset any (malicious?) attempt
            // to provide a special temp ID generated by this method
            if (paymentData.IsTemporaryID())
            {
                paymentData.paymentMethodID = null;
            }

            // Find or create Customer on Braintree (for membership subscriptions, the member
            // is a customer of Loconomics).
            var client = LcPayment.GetOrCreateBraintreeCustomer(LcPayment.Membership.GetFeePaymentUserId(memberUserID));

            // Quick way for saved payment method that does not needs to be updated
            if (paymentData.IsSavedID())
            {
                // Just double check payment exists to avoid mistake/malicious attempts:
                if (!paymentData.ExistsOnVault())
                {
                    // Since we have not input data to save, we can only throw an error
                    // invalidSavedPaymentMethod
                    throw new ConstraintException("[[[Chosen payment method has expired]]]");
                }
            }
            else
            {
                // Creates or updates a payment method with the given data

                // Must we set an ID as temporary to prevent it appears as a saved payment method?
                //paymentData.paymentMethodID = LcPayment.TempSavedCardPrefix + ASP.LcHelpers.Channel + "_paymentPlan";

                // Save on Braintree secure Vault
                // It updates the paymentMethodID if a new one was generated
                var saveCardError = paymentData.SaveInVault(client.Id);
                if (!String.IsNullOrEmpty(saveCardError))
                {
                    // paymentDataError
                    throw new ConstraintException(saveCardError);
                }
            }

            return(paymentData.paymentMethodID);
        }
Пример #3
0
        public static PaymentAccount Get(int userID)
        {
            PaymentAccount acc       = null;
            var            btAccount = LcPayment.GetProviderPaymentAccount(userID);

            if (btAccount != null &&
                btAccount.IndividualDetails != null)
            {
                acc = new PaymentAccount
                {
                    userID        = userID,
                    firstName     = btAccount.IndividualDetails.FirstName,
                    lastName      = btAccount.IndividualDetails.LastName,
                    phone         = btAccount.IndividualDetails.Phone,
                    email         = btAccount.IndividualDetails.Email,
                    streetAddress = btAccount.IndividualDetails.Address.StreetAddress,
                    //extendedAddress = btAccount.IndividualDetails.Address.ExtendedAddress,
                    city              = btAccount.IndividualDetails.Address.Locality,
                    postalCode        = btAccount.IndividualDetails.Address.PostalCode,
                    stateProvinceCode = btAccount.IndividualDetails.Address.Region,
                    countryCode       = btAccount.IndividualDetails.Address.CountryCodeAlpha2,
                    birthDate         = btAccount.IndividualDetails.DateOfBirth == null ? null :
                                        btAccount.IndividualDetails.DateOfBirth.IsDateTime() ?
                                        (DateTime?)btAccount.IndividualDetails.DateOfBirth.AsDateTime() :
                                        null,
                    ssn    = String.IsNullOrEmpty(btAccount.IndividualDetails.SsnLastFour) ? "" : btAccount.IndividualDetails.SsnLastFour.PadLeft(10, '*'),
                    status = (btAccount.Status ?? Braintree.MerchantAccountStatus.PENDING).ToString().ToLower()
                };
                // IMPORTANT: We need to strictly check for the null value of IndividualDetails and FundingDetails
                // since errors can arise, see #554
                if (btAccount.FundingDetails != null)
                {
                    acc.routingNumber = btAccount.FundingDetails.RoutingNumber;
                    acc.accountNumber = String.IsNullOrEmpty(btAccount.FundingDetails.AccountNumberLast4) ? "" : btAccount.FundingDetails.AccountNumberLast4.PadLeft(10, '*');
                    // Is Venmo account if there is no bank informatino
                    acc.isVenmo = String.IsNullOrEmpty(acc.accountNumber) && String.IsNullOrEmpty(acc.routingNumber);
                }
            }
            else
            {
                // Automatically fetch personal data from our DB (this will work as a preset)
                var data = LcRest.UserProfile.Get(userID);
                var add  = LcRest.Address.GetHomeAddress(userID);
                acc = new PaymentAccount {
                    userID            = userID,
                    firstName         = data.firstName,
                    lastName          = data.lastName,
                    phone             = data.phone,
                    email             = data.email,
                    streetAddress     = add.addressLine1,
                    postalCode        = add.postalCode,
                    city              = add.city,
                    stateProvinceCode = add.stateProvinceCode
                };
            }
            // Get data from our database as LAST step: both when there is data from Braintree and when not (this will let status to work
            // on localdev environments too, for testing)
            var dbAccount = LcData.GetProviderPaymentAccount(userID);

            if (dbAccount != null)
            {
                // Status from Braintree is not working, or has a big delay setting up the first time so user don't see the status,
                // using our saved copy:
                acc.status = (string)dbAccount.Status;
                //if (btAccount.Status == Braintree.MerchantAccountStatus.SUSPENDED)
                if (dbAccount.status == "suspended")
                {
                    var gw           = LcPayment.NewBraintreeGateway();
                    var notification = gw.WebhookNotification.Parse((string)dbAccount.bt_signature, (string)dbAccount.bt_payload);
                    var errors       = new List <string>();
                    errors.Add(notification.Message);
                    notification.Errors.All().Select(x => x.Code + ": " + x.Message);
                    acc.errors = errors;
                }
            }
            return(acc);
        }