/// <summary>
        /// 
        /// </summary>
        /// <remarks>Thanks to Melanie for reminding me about 
        /// EventManager.OnMoneyTransfer being the critical function,
        /// and not ApplyCharge.</remarks>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnMoneyTransfer(object sender, EventManager.MoneyTransferArgs e)
        {
            if (!m_active)
                return;

            IClientAPI user = null;
            Scene scene = null;

            // Find the user's controlling client.
            lock (m_scenes) {
                foreach (Scene sc in m_scenes) {
                    ScenePresence av = sc.GetScenePresence (e.sender);

                    if ((av != null) && (av.IsChildAgent == false)) {
                        // Found the client,
                        // and their root scene.
                        user = av.ControllingClient;
                        scene = sc;
                    }
                }
            }

            if (scene == null || user == null) {
                m_log.Warn ("[PayPal] Unable to find scene or user! Aborting transaction.");
                return;
            }

            PayPalTransaction txn;

            if (e.transactiontype == 5008) {
                // Object was paid, find it.
                SceneObjectPart sop = scene.GetSceneObjectPart (e.receiver);
                if (sop == null) {
                    m_log.Warn ("[PayPal] Unable to find SceneObjectPart that was paid. Aborting transaction.");
                    return;
                }

                string email;

                if (sop.OwnerID == sop.GroupID) {
                    if (m_allowGroups) {
                        if (!GetEmail (scene.RegionInfo.ScopeID, sop.OwnerID, out email)) {
                            m_log.Warn ("[PayPal] Unknown email address of group " + sop.OwnerID);
                            return;
                        }
                    } else {
                        m_log.Warn ("[PayPal] Payments to group owned objects is disabled.");
                        return;
                    }
                } else {
                    if (!GetEmail (scene.RegionInfo.ScopeID, sop.OwnerID, out email)) {
                        m_log.Warn ("[PayPal] Unknown email address of user " + sop.OwnerID);
                        return;
                    }
                }

                m_log.Info ("[PayPal] Start: " + e.sender + " wants to pay object " + e.receiver + " owned by " +
                            sop.OwnerID + " with email " + email + " US$ cents " + e.amount);

                txn = new PayPalTransaction (e.sender, sop.OwnerID, email, e.amount, scene, e.receiver,
                                             e.description + " T:" + e.transactiontype,
                                             PayPalTransaction.InternalTransactionType.Payment);
            } else {
                // Payment to a user.
                string email;
                if (!GetEmail (scene.RegionInfo.ScopeID, e.receiver, out email)) {
                    m_log.Warn ("[PayPal] Unknown email address of user " + e.receiver);
                    return;
                }

                m_log.Info ("[PayPal] Start: " + e.sender + " wants to pay user " + e.receiver + " with email " +
                            email + " US$ cents " + e.amount);

                txn = new PayPalTransaction (e.sender, e.receiver, email, e.amount, scene, e.description + " T:" +
                                             e.transactiontype, PayPalTransaction.InternalTransactionType.Payment);
            }

            // Add transaction to queue
            lock (m_transactionsInProgress)
                m_transactionsInProgress.Add (txn.TxID, txn);

            string baseUrl = m_scenes[0].RegionInfo.ExternalHostName + ":" + m_scenes[0].RegionInfo.HttpPort;

            user.SendLoadURL ("PayPal", txn.ObjectID, txn.To, false, "Confirm payment?", "http://" +
                              baseUrl + "/pp/?txn=" + txn.TxID);
        }
        void TransferSuccess(PayPalTransaction transaction)
        {
            if (transaction.InternalType == PayPalTransaction.InternalTransactionType.Payment) {
                if (transaction.ObjectID == UUID.Zero) {
                    // User 2 User Transaction
                    m_log.Info ("[PayPal] Success: " + transaction.From + " did pay user " +
                                transaction.To + " US$ cents " + transaction.Amount);

                    IUserAccountService userAccountService = m_scenes[0].UserAccountService;
                    UserAccount ua;

                    // Notify receiver
                    ua = userAccountService.GetUserAccount (transaction.From, "", "");
                    SendInstantMessage (transaction.To, ua.FirstName + " " + ua.LastName +
                                        " did pay you US$ cent " + transaction.Amount);

                    // Notify sender
                    ua = userAccountService.GetUserAccount (transaction.To, "", "");
                    SendInstantMessage (transaction.From, "You did pay " + ua.FirstName + " " +
                                        ua.LastName + " US$ cent " + transaction.Amount);
                } else {
                    if (OnObjectPaid != null) {
                        m_log.Info ("[PayPal] Success: " + transaction.From + " did pay object " +
                                    transaction.ObjectID + " owned by " + transaction.To +
                                    " US$ cents " + transaction.Amount);

                        OnObjectPaid (transaction.ObjectID, transaction.From, transaction.Amount);
                    }
                }
            } else if (transaction.InternalType == PayPalTransaction.InternalTransactionType.Purchase) {
                if (transaction.ObjectID == UUID.Zero) {
                    m_log.Error ("[PayPal] Unable to find Object bought! UUID Zero.");
                } else {
                    Scene s = LocateSceneClientIn (transaction.From);
                    SceneObjectPart part = s.GetSceneObjectPart (transaction.ObjectID);
                    if (part == null) {
                        m_log.Error ("[PayPal] Unable to find Object bought! UUID = " + transaction.ObjectID);
                        return;
                    }

                    m_log.Info ("[PayPal] Success: " + transaction.From + " did buy object " +
                                transaction.ObjectID + " from " + transaction.To + " paying US$ cents " +
                                transaction.Amount);

                    IBuySellModule module = s.RequestModuleInterface<IBuySellModule> ();
                    if (module == null) {
                        m_log.Error ("[PayPal] Missing BuySellModule! Transaction failed.");
                    } else {
                        ScenePresence sp = s.GetScenePresence(transaction.From);
                        if (sp != null)
                            module.BuyObject (sp.ControllingClient,
                                          transaction.InternalPurchaseFolderID, part.LocalId,
                                          transaction.InternalPurchaseType, transaction.Amount);
                    }
                }
            } else if (transaction.InternalType == PayPalTransaction.InternalTransactionType.Land) {
                // User 2 Land Transaction
                EventManager.LandBuyArgs e = transaction.E;

                lock (e) {
                    e.economyValidated = true;
                }

                Scene s = LocateSceneClientIn (transaction.From);
                ILandObject land = s.LandChannel.GetLandObject ((int)e.parcelLocalID);

                if (land == null) {
                    m_log.Error ("[PayPal] Unable to find Land bought! UUID = " + e.parcelLocalID);
                    return;
                }

                m_log.Info ("[PayPal] Success: " + e.agentId + " did buy land from " + e.parcelOwnerID +
                            " paying US$ cents " + e.parcelPrice);

                land.UpdateLandSold (e.agentId, e.groupId, e.groupOwned, (uint)e.transactionID,
                                     e.parcelPrice, e.parcelArea);
            } else {
                m_log.Error ("[PayPal] Unknown Internal Transaction Type.");
                return;
            }
            // Cleanup.
            lock (m_transactionsInProgress)
                m_transactionsInProgress.Remove (transaction.TxID);
        }
        public void ObjectBuy(IClientAPI remoteClient, UUID agentID, UUID sessionID, UUID groupID,
                               UUID categoryID, uint localID, byte saleType, int salePrice)
        {
            if (!m_active)
                return;

            IClientAPI user = null;
            Scene scene = null;

            // Find the user's controlling client.
            lock (m_scenes) {
                foreach (Scene sc in m_scenes) {
                    ScenePresence av = sc.GetScenePresence (agentID);

                    if ((av != null) && (av.IsChildAgent == false)) {
                        // Found the client,
                        // and their root scene.
                        user = av.ControllingClient;
                        scene = sc;
                    }
                }
            }

            if (scene == null || user == null) {
                m_log.Warn ("[PayPal] Unable to find scene or user! Aborting transaction.");
                return;
            }

            if (salePrice == 0) {
                IBuySellModule module = scene.RequestModuleInterface<IBuySellModule> ();
                if (module == null) {
                    m_log.Error ("[PayPal] Missing BuySellModule! Transaction failed.");
                    return;
                }
                module.BuyObject (remoteClient, categoryID, localID, saleType, salePrice);
                return;
            }

            SceneObjectPart sop = scene.GetSceneObjectPart (localID);
            if (sop == null) {
                m_log.Error ("[PayPal] Unable to find SceneObjectPart that was paid. Aborting transaction.");
                return;
            }

            string email;

            if (sop.OwnerID == sop.GroupID) {
                if (m_allowGroups) {
                    if (!GetEmail (scene.RegionInfo.ScopeID, sop.OwnerID, out email)) {
                        m_log.Warn ("[PayPal] Unknown email address of group " + sop.OwnerID);
                        return;
                    }
                } else {
                    m_log.Warn ("[PayPal] Purchase of group owned objects is disabled.");
                    return;
                }
            } else {
                if (!GetEmail (scene.RegionInfo.ScopeID, sop.OwnerID, out email)) {
                    m_log.Warn ("[PayPal] Unknown email address of user " + sop.OwnerID);
                    return;
                }
            }

            m_log.Info ("[PayPal] Start: " + agentID + " wants to buy object " + sop.UUID + " from " + sop.OwnerID +
                        " with email " + email + " costing US$ cents " + salePrice);

            PayPalTransaction txn = new PayPalTransaction (agentID, sop.OwnerID, email, salePrice, scene, sop.UUID,
                                                           "Item Purchase - " + sop.Name + " (" + saleType + ")",
                                                           PayPalTransaction.InternalTransactionType.Purchase,
                                                           categoryID, saleType);

            // Add transaction to queue
            lock (m_transactionsInProgress)
                m_transactionsInProgress.Add (txn.TxID, txn);

            string baseUrl = m_scenes[0].RegionInfo.ExternalHostName + ":" + m_scenes[0].RegionInfo.HttpPort;

            user.SendLoadURL ("PayPal", txn.ObjectID, txn.To, false, "Confirm purchase?", "http://" +
                              baseUrl + "/pp/?txn=" + txn.TxID);
        }
        private void processLandBuy(Object osender, EventManager.LandBuyArgs e)
        {
            if (!m_active)
                return;

            if (e.parcelPrice == 0)
                return;

            IClientAPI user = null;
            Scene scene = null;

            // Find the user's controlling client.
            lock (m_scenes) {
                foreach (Scene sc in m_scenes) {
                    ScenePresence av = sc.GetScenePresence (e.agentId);

                    if ((av != null) && (av.IsChildAgent == false)) {
                        // Found the client,
                        // and their root scene.
                        user = av.ControllingClient;
                        scene = sc;
                    }
                }
            }

            if (scene == null || user == null) {
                m_log.Error ("[PayPal] Unable to find scene or user! Aborting transaction.");
                return;
            }

            string email;

            if ((e.parcelOwnerID == e.groupId) || e.groupOwned) {
                if (m_allowGroups) {
                    if (!GetEmail (scene.RegionInfo.ScopeID, e.parcelOwnerID, out email)) {
                        m_log.Warn ("[PayPal] Unknown email address of group " + e.parcelOwnerID);
                        return;
                    }
                } else {
                    m_log.Warn ("[PayPal] Purchases of group owned land is disabled.");
                    return;
                }
            } else {
                if (!GetEmail (scene.RegionInfo.ScopeID, e.parcelOwnerID, out email)) {
                    m_log.Warn ("[PayPal] Unknown email address of user " + e.parcelOwnerID);
                    return;
                }
            }

            m_log.Info ("[PayPal] Start: " + e.agentId + " wants to buy land from " + e.parcelOwnerID +
                        " with email " + email + " costing US$ cents " + e.parcelPrice);

            PayPalTransaction txn;
            txn = new PayPalTransaction (e.agentId, e.parcelOwnerID, email, e.parcelPrice, scene,
                                         "Buy Land", PayPalTransaction.InternalTransactionType.Land, e);

            // Add transaction to queue
            lock (m_transactionsInProgress)
                m_transactionsInProgress.Add (txn.TxID, txn);

            string baseUrl = m_scenes[0].RegionInfo.ExternalHostName + ":" + m_scenes[0].RegionInfo.HttpPort;

            user.SendLoadURL ("PayPal", txn.ObjectID, txn.To, false, "Confirm payment?", "http://" +
                              baseUrl + "/pp/?txn=" + txn.TxID);
        }