2022
Payout using Paypal Payment Gateway
346
Payout
Payout basically means paying out funds to someone else from your account. So sometimes when we use paypal payment gateway we often need to transfer funds to other paypal users. So we will see how you can make payouts in Paypal Payment gateway api's.
So , first of all we have to install following nuget packages in your Asp.net core application.
After you have installed following packages. Now , we can add a controller where we have to add functionality for payout.
You can copy following code for that.
public class HomeController : Controller { private readonly ILogger<HomeController> _logger; private IHttpContextAccessor httpContextAccessor; IConfiguration _configuration; public HomeController(ILogger<HomeController> logger, IHttpContextAccessor context, IConfiguration iconfiguration) { _logger = logger; httpContextAccessor = context; _configuration = iconfiguration; } public IActionResult Index() { return View(); } private PayPal.Api.Payout payout; public ActionResult PaymentWithPaypal(string Cancel = null, string blogId = "", string PayerID = "", string guid = "") { //getting the apiContext var ClientID = _configuration.GetValue<string>("PayPal:Key"); var ClientSecret = _configuration.GetValue<string>("PayPal:Secret"); var mode = _configuration.GetValue<string>("PayPal:mode"); APIContext apiContext = PaypalConfiguration.GetAPIContext(ClientID, ClientSecret, mode); this.payout = new Payout() { items = new List<PayoutItem> { new PayoutItem { receiver="shubhambatra1994@gmail.com", amount=new Currency{currency="usd",value="1.00"}, note="Test", recipient_type=PayoutRecipientType.EMAIL, sender_item_id=Guid.NewGuid().ToString() } } }; this.payout.sender_batch_header = new PayoutSenderBatchHeader { sender_batch_id = Guid.NewGuid().ToString(), email_subject = "Test", recipient_type = PayoutRecipientType.EMAIL }; var response = this.payout.Create(apiContext); return View("PaymentSuccess"); } }
You have to also add the credentials in the appseting.json file so you can copy the code from here
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "PayPal": { "Key": "AfIlrsAAigAdLni0SFFIUrfLObfNvqtCT2mcBvNUxgLTxPUs_o21gVTjoggSpYFcF2hqkMhfVUSqCv1w", "Secret": "ECeznQaAnGPzbeq1mNvyZATUIrZxfsA-XJlZgrDwjJSI1hj1lqT5r1nISJLYeR2xwBarEg5Mq4n18Lm7", "mode": "sandbox" } }
Here , you have to remember that you can share funds to another paypal user using
1. Email
2. Paypal AccountId
3. Phone
in the article above we have used PayoutRecipientType.EMAIL there you can change the transfer mode as per your requirements. Now adding following code in a class file.
using PayPal.Api; using System.Collections.Generic; namespace PaypalCore.Models { public static class PaypalConfiguration { //Variables for storing the clientID and clientSecret key //Constructor static PaypalConfiguration() { } // getting properties from the web.config public static Dictionary<string, string> GetConfig(string mode) { return new Dictionary<string, string>() { {"mode",mode} }; } private static string GetAccessToken(string ClientId, string ClientSecret, string mode) { // getting accesstocken from paypal string accessToken = new OAuthTokenCredential(ClientId, ClientSecret, new Dictionary<string, string>() { {"mode",mode} }).GetAccessToken(); return accessToken; } public static APIContext GetAPIContext(string clientId, string clientSecret, string mode) { // return apicontext object by invoking it with the accesstoken APIContext apiContext = new APIContext(GetAccessToken(clientId, clientSecret, mode)); apiContext.Config = GetConfig(mode); return apiContext; } } }
So, now you can add one view and add following code there
<div class="row"> <a class="btn btn-primary" href="/Home/PaymentWithPaypal">Pay Now</a> </div>
So, this is how you can make payout using paypal payment gateway in Asp.net core.