简体   繁体   中英

Can not determine payment method in asp.net core

I actually wanted to make a payment gateway (Braintree) using asp.net core for testing purposes.but problem is transaction has not happened in Braintree.

Here is my code:-

 public interface IBraintreeService
    {
        IBraintreeGateway CreateGateway();
        IBraintreeGateway GetGateway();
    }



.................................


 public class BraintreeService : IBraintreeService
    {
        private readonly IConfiguration _config;

        public BraintreeService(IConfiguration config)
        {
            _config = config;
        }


        public IBraintreeGateway CreateGateway()
        {
            var newGateway = new BraintreeGateway()
            {
                Environment = Braintree.Environment.SANDBOX,
                MerchantId = _config.GetValue<string>("BraintreeGateway:MerchantId"),
                PublicKey = _config.GetValue<string>("BraintreeGateway:PublicKey"),
                PrivateKey = _config.GetValue<string>("BraintreeGateway:PrivateKey")
            };

            return newGateway;
        }

        public IBraintreeGateway GetGateway()
        {
            return CreateGateway();

        }
    }

Controller

 public class HomeController : Controller
    {
        private readonly IBraintreeService _braintreeService;

        public HomeController(IBraintreeService braintreeService)
        {
            _braintreeService = braintreeService;
        }




        public IActionResult Index()
        {
            var gateway = _braintreeService.GetGateway();
            var clientToken = gateway.ClientToken.Generate();
            ViewBag.ClientToken = clientToken;

            return View();
        }
       
        public IActionResult Create()
        {
            var gateway = _braintreeService.GetGateway();
           

            var request = new TransactionRequest
            {
                Amount = 24,
                PaymentMethodNonce = "hi",
                Options = new TransactionOptionsRequest
                {
                    SubmitForSettlement = true
                }
            };

            Result<Transaction> result = gateway.Transaction.Sale(request);

            if (result.IsSuccess())
            {
                return View("Success");
            }

            return View();
            
        }
    }

Home->Index view


<div class="text-center">
    <section>
        <div class="bt-drop-in-wrapper">
            <div id="bt-dropin"></div>
        </div>
        <a asp-controller="Home" asp-action="Create">Confirm Payment</a>
    </section>
</div>






<script src="https://js.braintreegateway.com/web/dropin/1.22.0/js/dropin.min.js"></script>
    <script>
    var client_token = "@ViewBag.ClientToken";


        braintree.dropin.create({
            authorization: client_token,
            container: '#bt-dropin'


        })

    </script>

Here is my output in index view:-

在此处输入图片说明

When i click Confirm Payment ,the create method of transacton was not succeeded.like

在此处输入图片说明

I already check my public key/ private key etc. I don't understand how I solve this, and how the transaction will succeed. can it's have another way?. please help.

Try like this in your index view.before this, you have to create a model for the Nonce property.

@model YourProjectName.Models.YourModelName


<form id="payment-form"  asp-action="Create" method="post" enctype="multipart/form-data" class="container ml-5">

@Html.HiddenFor(n => n.Nonce, new { @id = "nonce" })

...............................

<input type="submit" value="Confirm Payment" asp-action="Create" asp-controller="Home" class="btn btn-outline-dark form-control" />


</form>

and your controller will be

[HttpPost]
 public IActionResult Create(YourModelName model)
        {
            var gateway = _braintreeService.GetGateway();
           

            var request = new TransactionRequest
            {
                Amount = 24,
                PaymentMethodNonce = model.Nonce,
                Options = new TransactionOptionsRequest
                {
                    SubmitForSettlement = true
                }
            };

            Result<Transaction> result = gateway.Transaction.Sale(request);

            if (result.IsSuccess())
            {
                return View("Success");
            }

            return View();
            
        }
    }

Just update your code like above.hope it will resolve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM