Skip to content

Quick Start

jziaja edited this page Mar 5, 2015 · 1 revision

For those looking to hit the ground running...

1. Install

PM> Install-Package PayPal

2. Configure

Add the following to your web.config or app.config

<configuration>
  <configSections>
    <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
  </configSections>

  <!-- PayPal SDK settings -->
  <paypal>
    <settings>
      <add name="mode" value="sandbox"/>
      <add name="clientId" value="_client_Id_"/>
      <add name="clientSecret" value="_client_secret_"/>
    </settings>
  </paypal>
</configuration>

3. Code

using PayPal.Api;

// Authenticate with PayPal
var config = ConfigManager.Instance.GetProperties();
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
var apiContext = new APIContext(accessToken);

// Make an API call
var payment = Payment.Create(apiContext, new Payment
{
    intent = "sale",
    payer = new Payer
    {
        payment_method = "paypal"
    },
    transactions = new List<Transaction>
    {
        new Transaction
        {
            description = "Transaction description.",
            invoice_number = "001",
            amount = new Amount
            {
                currency = "USD",
                total = "100.00",
                details = new Details
                {
                    tax = "15",
                    shipping = "10",
                    subtotal = "75"
                }
            },
            item_list = new ItemList
            {
                items = new List<Item>
                {
                    new Item
                    {
                        name = "Item Name",
                        currency = "USD",
                        price = "15",
                        quantity = "5",
                        sku = "sku"
                    }
                }
            }
        }
    },
    redirect_urls = new RedirectUrls
    {
        return_url = "http://mysite.com/return",
        cancel_url = "http://mysite.com/cancel"
    }
});

4. Profit...

...with PayPal!