Пример #1
0
        public async Task <IActionResult> C2BPayment(CustomerToBusinessViewModel customerToBusinessViewModel)
        {
            MpesaResponse c2BResults;

            try
            {
                var c2BPayment = new CustomerToBusinessSimulate
                                 (
                    shortCode: _mpesaApiConfiguration.C2BShortCode,
                    commandId: Transaction_Type.CustomerPayBillOnline,
                    amount: customerToBusinessViewModel.Amount,
                    msisdn: _mpesaApiConfiguration.C2BMSISDNTest,
                    billReferenceNumber: customerToBusinessViewModel.PaymentReference
                                 );

                c2BResults = await _mpesaClient.MakeC2BPaymentAsync(c2BPayment, await RetrieveAccessToken(), MpesaRequestEndpoint.CustomerToBusinessSimulate);
            }
            catch (MpesaAPIException ex)
            {
                _logger.LogError(ex, ex.Message);
                return(View().WithDanger("Error", ex.Message));
            }

            return(View().WithSuccess("Success", "You have successfully submitted C2B payment."));
        }
Пример #2
0
        /// <summary>
        /// Simulates a Customer to Business payment request.
        /// </summary>
        /// <param name="customerToBusinessSimulate">C2B data transfer object</param>
        /// <param name="accesstoken">Acccesstoken retrieved by the <c>GetAuthTokenAsync</c> method.</param>
        /// <param name="mpesaRequestEndpoint">Set to <c>MpesaRequestEndpoint.CustomerToBusinessSimulate</c></param>
        /// <param name="cancellationToken">Cancellation Token</param>
        /// <returns>A JSON string containing data from MPESA API reposnse.</returns>
        /// <remarks>
        /// Use only for Simulation/testing. In production use <c>RegisterC2BUrlAsync</c> method to register
        /// endpoints in your application that receive customer initiated transactions from the MPESA API
        /// for confirmation and/or validation
        /// </remarks>
        public async Task <MpesaResponse> MakeC2BPaymentAsync(CustomerToBusinessSimulate customerToBusinessSimulate, string accesstoken, string mpesaRequestEndpoint, CancellationToken cancellationToken = default)
        {
            var validator = new CustomerToBusinessSimulateTransactionValidator();
            var results   = await validator.ValidateAsync(customerToBusinessSimulate, cancellationToken);

            return(!results.IsValid
                ? throw new MpesaAPIException(HttpStatusCode.BadRequest, string.Join(Environment.NewLine, results.Errors.Select(x => x.ErrorMessage.ToString())))
                : await MpesaPostRequestAsync <MpesaResponse>(customerToBusinessSimulate, accesstoken, mpesaRequestEndpoint, cancellationToken));
        }