Ejemplo n.º 1
0
        private bool doVoid(PayPalSession session, PayPalOAuthToken authToken, Transaction transaction, string description = null, object extraData = null)
        {
            var logID = Log(MessageType.Info, "doVoid()", "Void transaction");

            var payoutItemID = getPayoutItemID(transaction);

            if (payoutItemID.IsNullOrWhiteSpace())
            {
                if (doRefresh(session, authToken, transaction, extraData))
                {
                    session.StoreTransaction(transaction);
                    payoutItemID = getPayoutItemID(transaction);
                }
                else
                {
                    return(false);
                }
            }

            var payoutItem = cancelPayoutItem(payoutItemID, authToken, logID);

            if (payoutItem.TransactionStatus != payoutTransactionStatus.RETURNED)
            {
                return(false);
            }

            transaction.__Apply(Transaction.Operation.Void(TransactionStatus.Refunded, payoutItem.TimeProcessed.Value, payoutItem.Token, description, extraData: extraData));
            return(true);
        }
Ejemplo n.º 2
0
        private void cancelUnclaimedPayout(string itemID, PayPalSession payPalSession)
        {
            try
            {
                var request = new WebClient.RequestParams
                {
                    Caller      = this,
                    Uri         = new Uri(m_ApiUri + URI_CANCEL_UNCLAIMED_PAYOUT.Args(itemID)),
                    Method      = HTTPRequestMethod.POST,
                    ContentType = ContentType.JSON,
                    Headers     = new Dictionary <string, string>
                    {
                        { HDR_AUTHORIZATION, HDR_AUTHORIZATION_OAUTH.Args(payPalSession.AuthorizationToken.AccessToken) },
                    }
                };

                var response = WebClient.GetJson(request);
                Log(MessageType.Info, "cancelUnclaimedPayout()", response.ToJSON());
            }
            catch (Exception ex)
            {
                var message = StringConsts.PAYPAL_PAYOUT_CANCEL_ERROR.Args(ex.ToMessageWithType());
                var error   = PayPalPaymentException.ComposeError(message, ex);
                Log(MessageType.Error, "cancelUnclaimedPayout()", error.Message, ex);

                throw error;
            }
        }
Ejemplo n.º 3
0
        private bool doRefresh(PayPalSession session, PayPalOAuthToken authToken, Transaction transaction, object extraData = null)
        {
            var logID = Log(MessageType.Info, "doRefresh()", "Refresh transaction");

            payoutItemDetails payoutItem = null;
            var payoutItemID             = getPayoutItemID(transaction);

            if (payoutItemID.IsNullOrWhiteSpace())
            {
                var payouts = getPayouts(getBatchID(transaction), authToken, logID);
                switch (payouts.BatchHeader.BatchStatus)
                {
                case payoutBatchStatus.NEW:
                case payoutBatchStatus.PENDING:
                case payoutBatchStatus.PROCESSING:
                    return(false);
                }
                if (!payouts.Items.Any())
                {
                    return(false);
                }
                payoutItem = payouts.Items.FirstOrDefault(item => item.PayoutItem.SenderItemId.Equals(transaction.ID.AsString()));
            }
            else
            {
                payoutItem = getPayoutItem(payoutItemID, authToken, logID);
            }
            if (payoutItem == null)
            {
                return(false);
            }

            var transactionStatus = mapTransactionStatus(payoutItem.TransactionStatus);

            if (transaction.Status == transactionStatus)
            {
                return(false);
            }

            transaction.__Apply(Transaction.Operation.Refresh(transactionStatus, payoutItem.TimeProcessed, payoutItem.Token, payoutItem.Amount, payoutItem.PayoutItemFee.Value, extraData));
            return(true);
        }
Ejemplo n.º 4
0
        private void checkPayoutStatus(JSONDataMap response, PayPalSession payPalSession)
        {
            var batchStatus = response.GetNodeByPath(RESPONSE_BATCH_HEADER, RESPONSE_BATCH_STATUS).AsString();

            if (!batchStatus.EqualsIgnoreCase(RESPONSE_SUCCESS))
            {
                throwPaymentException(response.GetNodeByPath(RESPONSE_ERRORS, RESPONSE_ERRORS_NAME).AsString(),
                                      response.GetNodeByPath(RESPONSE_ERRORS, RESPONSE_ERRORS_MESSAGE).AsString(),
                                      response.GetNodeByPath(RESPONSE_ERRORS, RESPONSE_ERRORS_DETAILS).AsString());
            }

            var items = response[RESPONSE_ITEMS] as JSONDataArray;

            if (items == null || items.Count <= 0)
            {
                return;
            }

            // for the moment there is only one possible payment in a batch
            var item   = (JSONDataMap)items[0];
            var status = item[RESPONSE_ITEM_TRAN_STATUS].AsString();

            //todo: cancel unclaimed transaction?
            if (status.EqualsIgnoreCase(RESPONSE_ITEM_UNCLAIMED))
            {
                var itemID = item[RESPONSE_ITEMS_ITEMID].AsString();
                cancelUnclaimedPayout(itemID, payPalSession);

                throwPaymentException(item.GetNodeByPath(RESPONSE_ITEM_ERRORS, RESPONSE_ITEM_ERRORS_NAME).AsString(),
                                      item.GetNodeByPath(RESPONSE_ITEM_ERRORS, RESPONSE_ITEM_ERRORS_MESSAGE).AsString(),
                                      EMPTY);
            }

            //todo: other statuses https://developer.paypal.com/docs/api/payments.payouts-batch#payouts_get
            if (!status.EqualsIgnoreCase(RESPONSE_ITEM_SUCCESS))
            {
                throwPaymentException(item.GetNodeByPath(RESPONSE_ITEM_ERRORS, RESPONSE_ITEM_ERRORS_NAME).AsString(),
                                      item.GetNodeByPath(RESPONSE_ITEM_ERRORS, RESPONSE_ITEM_ERRORS_MESSAGE).AsString(),
                                      status);
            }
        }
Ejemplo n.º 5
0
        private bool doTransfer(PayPalSession session, PayPalOAuthToken authToken, Transaction transaction, object extraData = null)
        {
            var logID = Log(MessageType.Info, "doTransfer()", "Transfer {0}".Args(transaction.Amount));

            var fromActualData = session.FetchAccountData(transaction.From);
            var toActualData   = session.FetchAccountData(transaction.To);

            var payouts = createPayouts(
                new payout
            {
                SenderBatchHeader = new payoutSenderBatchHeader
                {
                    SenderBatchID = transaction.ID.AsString(),
                    EmailSubject  = m_PayoutEmailSubject,
                    RecipientType = payoutRecipientType.EMAIL
                },
                Items = new List <payoutItem>
                {
                    new payoutItem
                    {
                        SenderItemId  = transaction.ID.AsString(),
                        RecipientType = payoutRecipientType.EMAIL,
                        Amount        = transaction.Amount,
                        Note          = transaction.Description,
                        Receiver      = toActualData.AccountID.AsString()
                    }
                }
            }, authToken, logID);

            switch (payouts.BatchHeader.BatchStatus)
            {
            case payoutBatchStatus.NEW:
            case payoutBatchStatus.PENDING:
            case payoutBatchStatus.PROCESSING:
                transaction.__Apply(Transaction.Operation.Refresh(TransactionStatus.Pending, App.TimeSource.UTCNow, payouts.BatchHeader.PayoutBatchID, transaction.Amount.Value, extraData: extraData));
                return(true);

            case payoutBatchStatus.SUCCESS:
                var payoutItem = payouts.Items.FirstOrAnyOrDefault(item => item.PayoutItem.SenderItemId.Equals(transaction.ID.ToString()));
                if (payoutItem == null)
                {
                    throw new PaymentException();
                }

                var transactionStatus = mapTransactionStatus(payoutItem.TransactionStatus);
                switch (transactionStatus)
                {
                case TransactionStatus.Pending:
                case TransactionStatus.Unclaimed:
                case TransactionStatus.Refunded:
                    break;

                case TransactionStatus.Success:
                    StatTransfer(transaction.Amount);
                    break;

                default:
                    throw new PaymentException();
                }
                transaction.__Apply(Transaction.Operation.Refresh(transactionStatus, payouts.BatchHeader.TimeCreated.Value, payoutItem.Token, transaction.Amount.Value, payoutItem.PayoutItemFee.Value, extraData: extraData));
                return(true);
            }
            throw new PaymentException();
        }
Ejemplo n.º 6
0
        private void checkPayoutStatus(JSONDataMap response, PayPalSession payPalSession)
        {
            var batchStatus = response.GetNodeByPath(RESPONSE_BATCH_HEADER, RESPONSE_BATCH_STATUS).AsString();
              if (!batchStatus.EqualsIgnoreCase(RESPONSE_SUCCESS))
              {
                throwPaymentException(response.GetNodeByPath(RESPONSE_ERRORS, RESPONSE_ERRORS_NAME).AsString(),
                                      response.GetNodeByPath(RESPONSE_ERRORS, RESPONSE_ERRORS_MESSAGE).AsString(),
                                      response.GetNodeByPath(RESPONSE_ERRORS, RESPONSE_ERRORS_DETAILS).AsString());
              }

              var items = response[RESPONSE_ITEMS] as JSONDataArray;
              if (items == null || items.Count<=0) return;

              // for the moment there is only one possible payment in a batch
              var item = (JSONDataMap)items[0];
              var status = item[RESPONSE_ITEM_TRAN_STATUS].AsString();

              //todo: cancel unclaimed transaction?
              if (status.EqualsIgnoreCase(RESPONSE_ITEM_UNCLAIMED))
              {
                var itemID = item[RESPONSE_ITEMS_ITEMID].AsString();
                cancelUnclaimedPayout(itemID, payPalSession);

                throwPaymentException(item.GetNodeByPath(RESPONSE_ITEM_ERRORS, RESPONSE_ITEM_ERRORS_NAME).AsString(),
                                      item.GetNodeByPath(RESPONSE_ITEM_ERRORS, RESPONSE_ITEM_ERRORS_MESSAGE).AsString(),
                                      EMPTY);
              }

              //todo: other statuses https://developer.paypal.com/docs/api/payments.payouts-batch#payouts_get
              if (!status.EqualsIgnoreCase(RESPONSE_ITEM_SUCCESS))
              {
                throwPaymentException(item.GetNodeByPath(RESPONSE_ITEM_ERRORS, RESPONSE_ITEM_ERRORS_NAME).AsString(),
                                      item.GetNodeByPath(RESPONSE_ITEM_ERRORS, RESPONSE_ITEM_ERRORS_MESSAGE).AsString(),
                                      status);
              }
        }
Ejemplo n.º 7
0
        private void cancelUnclaimedPayout(string itemID, PayPalSession payPalSession)
        {
            try
            {
                var request = new WebClient.RequestParams
                {
                    Caller = this,
                    Uri = new Uri(m_ApiUri + URI_CANCEL_UNCLAIMED_PAYOUT.Args(itemID)),
                    Method = HTTPRequestMethod.POST,
                    ContentType = ContentType.JSON,
                    Headers = new Dictionary<string, string>
                        {
                            { HDR_AUTHORIZATION, HDR_AUTHORIZATION_OAUTH.Args(payPalSession.AuthorizationToken.AccessToken) },
                        }
                };

                var response = WebClient.GetJson(request);
                Log(MessageType.Info, "cancelUnclaimedPayout()", response.ToJSON());
            }
            catch (Exception ex)
            {
                var message = StringConsts.PAYPAL_PAYOUT_CANCEL_ERROR.Args(ex.ToMessageWithType());
                var error = PayPalPaymentException.ComposeError(message, ex);
                Log(MessageType.Error, "cancelUnclaimedPayout()", error.Message, ex);

                throw error;
            }
        }