Esempio n. 1
0
        private PaymentsHelper GetPaymentsHelper()
        {
            // Check api token
            if (_token == null)
            {
                // Create new token for api calls
                _token = AuthHelper.CreateAccessToken(ClientIdTxt.Text, ClientSecretTxt.Text);
            }
            else
            {
                // Check token expiration time
                if (_token.DateExpired.CompareTo(DateTime.Now) <= 0)
                {
                    // Regenerate token
                    _token = AuthHelper.CreateAccessToken(ClientIdTxt.Text, ClientSecretTxt.Text);
                }
            }

            // Set PaymentHelper
            if (_ph == null)
            {
                _ph = new PaymentsHelper();
            }
            _ph.AccessToken = _token.AccessToken;

            return(_ph);
        }
Esempio n. 2
0
        // Create a checkout preference
        private Preference CreatePreference(Int16 amount, string email)
        {
            // Set Checkout Helper
            CheckoutHelper ch = new CheckoutHelper();

            // Create token
            Token token = AuthHelper.CreateAccessToken(Properties.Settings.Default.ClientId, Properties.Settings.Default.ClientSecret);

            ch.AccessToken = token.AccessToken;

            // Set item
            Item item = new Item();

            item.CurrencyId  = Properties.Settings.Default.CurrencyId;
            item.Description = Properties.Settings.Default.Description;
            item.Id          = Properties.Settings.Default.ItemId;
            item.PictureUrl  = Properties.Settings.Default.ImageUrl;
            item.Quantity    = 1;
            item.Title       = Properties.Settings.Default.Title;
            item.UnitPrice   = amount;

            // Set preference
            Preference preference = new Preference();

            preference.BackUrls          = new ResponseUrls();
            preference.BackUrls.Failure  = Properties.Settings.Default.FailureUrl;
            preference.BackUrls.Pending  = Properties.Settings.Default.PendingUrl;
            preference.BackUrls.Success  = Properties.Settings.Default.SuccessUrl;
            preference.ExternalReference = "credit_card-rejected-cc_rejected_call_for_authorize";  // your id for this transaction
            preference.Items             = new ItemList();
            preference.Items.Add(item);
            preference.Payer       = new UserEx();
            preference.Payer.Email = email;
            //preference.CustomPaymentChoices = new PaymentChoices();
            //preference.CustomPaymentChoices.ExcludedTypes = new IdList();  // remove ticket and bank transfer options
            //preference.CustomPaymentChoices.ExcludedTypes.Add("ticket");
            //preference.CustomPaymentChoices.ExcludedTypes.Add("bank_transfer");

            // Create preference
            preference = ch.CreatePreference(preference);

            return(preference);
        }
    // Create a checkout preference
    private Preference CreatePreference(Int16 amount, string email)
    {
        // Set Checkout Helper
        CheckoutHelper ch = new CheckoutHelper();

        // Create token
        Token token = AuthHelper.CreateAccessToken(ConfigurationManager.AppSettings["ClientId"], ConfigurationManager.AppSettings["ClientSecret"]);

        ch.AccessToken = token.AccessToken;

        // Set item
        Item item = new Item();

        item.CurrencyId  = ConfigurationManager.AppSettings["CurrencyId"];
        item.Description = ConfigurationManager.AppSettings["Description"];
        item.Id          = ConfigurationManager.AppSettings["ItemId"];
        item.PictureUrl  = ConfigurationManager.AppSettings["ImageUrl"];
        item.Quantity    = 1;
        item.Title       = ConfigurationManager.AppSettings["Title"];
        item.UnitPrice   = amount;

        // Set preference
        Preference preference = new Preference();

        preference.BackUrls          = new ResponseUrls();
        preference.BackUrls.Failure  = ConfigurationManager.AppSettings["FailureUrl"];
        preference.BackUrls.Pending  = ConfigurationManager.AppSettings["PendingUrl"];
        preference.BackUrls.Success  = ConfigurationManager.AppSettings["SuccessUrl"];
        preference.ExternalReference = "my id";  // your id for this transaction
        preference.Items             = new ItemList();
        preference.Items.Add(item);
        preference.Payer       = new UserEx();
        preference.Payer.Email = email;

        // Create preference
        preference = ch.CreatePreference(preference);

        return(preference);
    }
Esempio n. 4
0
        protected void Page_Load(object sender, EventArgs e)
        {
            // Get collection id
            Int32 id = Convert.ToInt32(Request["id"]);

            if (id != 0)
            {
                try
                {
                    // Create Payments helper
                    PaymentsHelper ph = new PaymentsHelper();

                    // Create a token for the API's calls
                    // Remember 1st!
                    // Change the property settings (Settings.settings) for the following variables:
                    // Your ClientId, Your ClientSecret
                    Token token = AuthHelper.CreateAccessToken(Properties.Settings.Default.ClientId, Properties.Settings.Default.ClientSecret);
                    ph.AccessToken = token.AccessToken;

                    // Get Collection Notification
                    CollectionNotification cn = ph.GetCollectionNotification(id);

                    // Here goes your code: do something with the notification!
                    // Remember: IPN system waits for your reply about 500ms. If this method times out that threshold it will retry the
                    // notification again. So prepare your code to be fast enough and/or to support retries (eg., ask if the collection was
                    // already processed!).
                    // This example just shows collection's attribute values
                    Label1.Text = "<b>currency id:</b> " + cn.Collection.CurrencyId + "<br/>";
                    Label1.Text = Label1.Text + "<b>collector id:</b> " + cn.Collection.Collector.Id.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>collector first name:</b> " + cn.Collection.Collector.FirstName + "<br/>";
                    Label1.Text = Label1.Text + "<b>collector last name:</b> " + cn.Collection.Collector.LastName + "<br/>";
                    Label1.Text = Label1.Text + "<b>collector nickname:</b> " + cn.Collection.Collector.Nickname + "<br/>";
                    Label1.Text = Label1.Text + "<b>collector email:</b> " + cn.Collection.Collector.Email + "<br/>";
                    Label1.Text = Label1.Text + "<b>collector phone areacode:</b> " + cn.Collection.Collector.Phone.AreaCode + "<br/>";
                    Label1.Text = Label1.Text + "<b>collector phone number:</b> " + cn.Collection.Collector.Phone.Number + "<br/>";
                    Label1.Text = Label1.Text + "<b>collector phone extension:</b> " + cn.Collection.Collector.Phone.Extension + "<br/>";
                    Label1.Text = Label1.Text + "<b>date approved:</b> " + cn.Collection.DateApproved.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>date created:</b> " + cn.Collection.DateCreated.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>external reference:</b> " + cn.Collection.ExternalReference + "<br/>";
                    Label1.Text = Label1.Text + "<b>finance charge:</b> " + cn.Collection.FinanceCharge.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>id:</b> " + cn.Collection.Id.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>installments:</b> " + cn.Collection.Installments.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>last modified:</b> " + cn.Collection.LastModified.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>marketplace:</b> " + cn.Collection.Marketplace.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>mercadopago fee:</b> " + cn.Collection.MercadoPagoFee.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>money release date:</b> " + cn.Collection.MoneyReleaseDate.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>net received amount:</b> " + cn.Collection.NetReceivedAmount.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>operation type:</b> " + cn.Collection.OperationType + "<br/>";
                    Label1.Text = Label1.Text + "<b>order id:</b> " + cn.Collection.OrderId + "<br/>";
                    Label1.Text = Label1.Text + "<b>payer id:</b> " + cn.Collection.Payer.Id.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>payer first name:</b> " + cn.Collection.Payer.FirstName + "<br/>";
                    Label1.Text = Label1.Text + "<b>payer last name:</b> " + cn.Collection.Payer.LastName + "<br/>";
                    Label1.Text = Label1.Text + "<b>payer nickname:</b> " + cn.Collection.Payer.Nickname + "<br/>";
                    Label1.Text = Label1.Text + "<b>payer email:</b> " + cn.Collection.Payer.Email + "<br/>";
                    Label1.Text = Label1.Text + "<b>payer phone areacode:</b> " + cn.Collection.Payer.Phone.AreaCode + "<br/>";
                    Label1.Text = Label1.Text + "<b>payer phone number:</b> " + cn.Collection.Payer.Phone.Number + "<br/>";
                    Label1.Text = Label1.Text + "<b>payer phone extension:</b> " + cn.Collection.Payer.Phone.Extension + "<br/>";
                    Label1.Text = Label1.Text + "<b>payment type:</b> " + cn.Collection.PaymentType + "<br/>";
                    Label1.Text = Label1.Text + "<b>reason:</b> " + cn.Collection.Reason + "<br/>";
                    Label1.Text = Label1.Text + "<b>released:</b> " + cn.Collection.Released.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>shipping cost:</b> " + cn.Collection.ShippingCost.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>site id:</b> " + cn.Collection.SiteId + "<br/>";
                    Label1.Text = Label1.Text + "<b>status:</b> " + cn.Collection.Status + "<br/>";
                    Label1.Text = Label1.Text + "<b>status detail:</b> " + cn.Collection.StatusDetail + "<br/>";
                    Label1.Text = Label1.Text + "<b>total paid amount:</b> " + cn.Collection.TotalPaidAmount.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>transaction amount:</b> " + cn.Collection.TransactionAmount.ToString() + "<br/>";
                    Label1.Text = Label1.Text + "<b>json:</b> " + cn.ToJSON().ToString();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        private void RunProcess()
        {
            // Show start message
            _trayIcon.ShowBalloonTip(200, "Ready!", "Waiting for payments ...", ToolTipIcon.None);

            // Set MercadoPago helper
            MercadoPagoHelper mph = new MercadoPagoHelper();

            // Set helper environment
            if (SandboxEnvButton.Checked)
            {
                mph.EnvironmentScope = MercadoPagoSDK.Environment.Scopes.Sandbox;
            }
            else
            {
                mph.EnvironmentScope = MercadoPagoSDK.Environment.Scopes.Live;
            }

            // Set helper credentials
            try
            {
                Token token = AuthHelper.CreateAccessToken(_clientId, _clientSecret);
                mph.AccessToken = token.AccessToken;
            }
            catch (Exception ex)
            {
                _trayIcon.ShowBalloonTip(200, "Error resolving credentials", ex.Message, ToolTipIcon.Error);
                _manualStop = true;
            }

            // Iterate until manual stop ...
            while (!_manualStop)
            {
                // Search collections
                List <Int32> pendingCollections = new List <Int32>();
                try
                {
                    pendingCollections = mph.GetPendingCollections(_lastProcessedCollectionId);
                }
                catch (Exception ex)
                {
                    _trayIcon.ShowBalloonTip(200, "Error retrieving collections", ex.Message, ToolTipIcon.Error);
                    _manualStop = true;
                }

                // For each non-processed collection, get notification and post message to the local listener
                if (pendingCollections.Count != 0)
                {
                    if (_lastProcessedCollectionId != null)
                    {
                        for (int i = (pendingCollections.Count - 1); i >= 0; i--)
                        {
                            try
                            {
                                // Post IPN message
                                _trayIcon.ShowBalloonTip(200, "Found!", "Collection: " + pendingCollections[i].ToString(), ToolTipIcon.None);
                                mph.PostIPNMessage(_endPoint, pendingCollections[i]);
                            }
                            catch (Exception ex)
                            {
                                // In this version of the tool any possible error is ignored. Don't worry about debugging problems
                                // or timeouts. But this hides potential local IPN url configuration errors.
                            }
                        }
                    }

                    // Mark last processed collection
                    _lastProcessedCollectionId = pendingCollections[0];
                }
                else
                {
                    if (_lastProcessedCollectionId == null)
                    {
                        _lastProcessedCollectionId = -1;
                    }
                }

                // Wait for next loop ...
                Thread.Sleep(3000);
            }

            // Rebuild menu, enable start, disable stop
            _trayMenu.MenuItems[0].Enabled = true;
            _trayMenu.MenuItems[1].Enabled = false;
            _trayMenu.MenuItems[2].Enabled = true;
            _trayMenu.MenuItems[3].Enabled = true;
            System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
            Stream myStream = myAssembly.GetManifestResourceStream("LocalIPNSimulator.Resources.icon_red.ico");

            _trayIcon.Icon = new Icon(myStream);
        }