public async Task <Response <string> > PayoutTransaction(List <PaymentPlan> request)
        {
            var bulkTransfer = new BulkTransferRequest();

            foreach (var plan in request)
            {
                foreach (var detail in plan.Receivers)
                {
                    //TODO: see how you can retrieve transaction reference from bulk transfer foreach transaction created.
                    //TODO: it's the reason the block below is commented out.
                    // bulkTransfer.Transfers.Add(
                    //     new BulkTransfer
                    //     {
                    //         Amount = plan.Amount.ToString(),
                    //         Reason = plan.Title,
                    //         Recipient = detail.PaystackRecipientCode
                    //     });
                    var transaction = new Transaction
                    {
                        UserId              = plan.UserId,
                        Title               = plan.Title,
                        Description         = plan.Description,
                        Amount              = plan.Amount,
                        TransactionStatus   = TransactionStatus.Initiated,
                        TransactionType     = TransactionType.Withdrawal,
                        UserPreviousBalance = 0,
                        UserCurrentBalance  = 0,
                        PlanId              = plan.Id
                    };
                    await this.AddAsync(transaction);

                    var singleTransferRequest = new SingleTransferRequest
                    {
                        //Paystack accepts amount in kobo, please do not change except standards have changed.
                        Amount    = (plan.Amount * 100).ToString(),
                        Reason    = plan.Title,
                        Recipient = detail.PaystackRecipientCode
                    };
                    var initiateSingleTransfer =
                        await _paymentService.InitiateSingleTransfer(singleTransferRequest);

                    if (initiateSingleTransfer.status)
                    {
                        transaction.Reference             = initiateSingleTransfer.data.Reference;
                        transaction.TransactionStatus     = TransactionStatus.Pending;
                        transaction.SuccessfullyInitiated = true;
                    }
                    else
                    {
                        transaction.TransactionStatus = TransactionStatus.Failed;
                    }

                    await this.UpdateAsync(transaction);
                }
                //verify transactions //TODO: leaving this task to webhook or a different cronjob that'll do the check;
                //TODO: this means you'll need to add a flag to each transaction or TransactionStatus might suffice

                //send out notifications
                //TODO: do this on success; i.e leave it to the webhook handler

                //TODO: Do everything for one plan at a time and save success or failure tied to that plan in a different response body
            }

            return(new Response <string>("done."));
        }
Пример #2
0
        public async Task <PaystackBaseResponse <SingleTransferResponse> > InitiateSingleTransfer(SingleTransferRequest request)
        {
            var token   = _externalAuthService.GetPaystackToken();
            var headers = new Dictionary <string, string> {
                { "Authorization", $"Bearer {token}" }
            };
            var response = await _httpClient.PostAsJsonAsync <PaystackBaseResponse <SingleTransferResponse> >
                               (request, $"{_paystackSettings.BaseUrl}{_paystackSettings.SingleTransferUrl}", headers);

            return(response);
        }