I copy `ClientId` and `ClientSecret` from Paypal:

[![enter image description here](https://i.stack.imgur.com/X6yx9.png)](https://i.stack.imgur.com/X6yx9.png)

to `web.config` like this:

```
<configSections>
    <section name="paypal" type="PayPal.SDKConfigHandler, PayPal"/>
</configSections>
<!--Gremand58@armyspy.com-->
<paypal>
    <settings>
        <add name="mode" value="sandbox"/>
        <add name="connectionTimeout" value="360000"/>
        <add name="requestRetries" value="1"/>
        <add name="clientId" value="---Key---"/>
        <add name="clientSecret" value="---Secret Key---"/>
    </settings>
</paypal>

```

Then I created a class `PaypalConfiguration`:

```
public class PaypalConfiguration
{
    // Variables for storing the clientID and clientSecret key
    public readonly static string clientId;
    public readonly static string clientSecret;

    // Constructor
    static PaypalConfiguration()
    {
        var config = GetConfig();
        clientId = "---Key---";
        clientSecret = "---Secret Key---";
    }

    // getting properties from the web.config
    public static Dictionary<string, string> GetConfig()
    {
        return PayPal.Api.ConfigManager.Instance.GetProperties();
    }

    private static string GetAccessToken()
    {
        // getting accesstocken from paypal
        string accessToken = new OAuthTokenCredential(clientId, clientSecret, GetConfig()).GetAccessToken();
        return accessToken;
    }
    public static APIContext GetAPIContext()
    {
        // return apicontext object by invoking it with the accesstoken
        APIContext apiContext = new APIContext(GetAccessToken());
        apiContext.Config = GetConfig();
        return apiContext;
    }
}

```

And the last I created the `PaymentController` like this:

```
public class PaymentController : Controller
{
    // GET: Payment
    public ActionResult PaymentWithPaypal()
    {
        APIContext apicontext = PaypalConfiguration.GetAPIContext();

        try
        {
            string payerID = Request.Params["PayerID"];

            if (string.IsNullOrEmpty(payerID))
            {
                string baseURi = Request.Url.Scheme + "://" + Request.Url.Authority + "/Payment/PaymentWithPapal?";
                var Guid = Convert.ToString((new Random()).Next(100000000));
                var createPayment = this.CreatePayment(apicontext, baseURi+"guid="+Guid);

                var links = createPayment.links.GetEnumerator();
                string paypalRedirectURL = null;

                while (links.MoveNext())
                {
                    Links lnk = links.Current;

                    if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                    {
                        paypalRedirectURL = lnk.href;
                    }
                }
                Session.Add(Guid, createPayment.id);
                return Redirect(paypalRedirectURL);
            }
            else
            {
                var guid = Request.Params["guid"];
                var executePayment = ExecutePayment(apicontext, payerID, Session[guid] as string);

                if(executePayment.ToString().ToLower()!= "approved")
                {
                    return View("FailureView");
                }
            }
        }
        catch(Exception e)
        {
            return View("FailureView");
        }

        return View("successView");
    }

    private object ExecutePayment(APIContext apicontext, string payerID, string PaymentID)
    {
        var paymentExecution = new PaymentExecution() {payer_id = payerID };
        this.payment = new Payment() { id = PaymentID};
        return this.payment.Execute(apicontext, paymentExecution);
    }

    private PayPal.Api.Payment payment;

    private Payment CreatePayment(APIContext apicontext, string redirectURl)
    {
        var ItemList = new ItemList() { 
            items = new List<Item>()
        };

        if(Session["cart"]!="")
        {
            List<Models.Home.Items> carts = (List<Models.Home.Items>)(Session["cart"]);
            foreach (var item in carts)
            {
                ItemList.items.Add(new Item()
                {
                    name = item.product.ten_xe.ToString(),
                    currency = "TK",
                    price = item.product.gia_ban.ToString(),
                    quantity = item.quantity.ToString(),
                    sku = "sku"
                }) ;
            }

            var payer = new Payer() { payment_method = "paypal" };

            var redirectUrl = new RedirectUrls()
            {
                cancel_url = redirectURl + "&Cancel=true" ,
                return_url = redirectURl
            };

            var details = new Details()
            {
                tax = "1",
                shipping = "1",
                subtotal = "1"
            };

            var amount = new Amount()
            {
                currency = "USD",
                //tax+shipping+subtotal
                total = Session["SesTotal"].ToString(),

                details = details
            };

            var transactionList = new List<Transaction>();
            transactionList.Add(new Transaction()
            {
                description = "Transaction Description",
                invoice_number = "#100000",
                amount = amount,
                item_list = ItemList
            });

            this.payment = new Payment()
            {
                intent = "sale",
                payer = payer,
                transactions = transactionList,
                redirect_urls = redirectUrl
            };
        }

        return this.payment.Create(apicontext);
    }
}

```

When I debug I realize that

```
return this.payment.Create(apicontext);

```

When I execute the line I get an error

> 
> The remote server returned an error: (400) Bad Request.
> 
> 
> 

Why?

I have tried many way to solve this problem in the internet,youtube even stackoverflow but all have no result .Hoping you guys help me solve this problem.Thank you