Coder Social home page Coder Social logo

checkout-net-sdk-1's Introduction

REST API SDK for Dotnet V2

Home Image

Welcome to PayPal Dotnet SDK. This repository contains PayPal's Dotnet SDK and samples ffor v2/checkout/orders and v2/payments APIs.

This is a part of the next major PayPal SDK. It includes a simplified interface to only provide simple model objects and blueprints for HTTP calls. This repo currently contains functionality for PayPal Checkout APIs which includes Orders V2 and Payments V2.

Please refer to the PayPal Checkout Integration Guide for more information. Also refer to Setup your SDK for additional information about setting up the SDK's.

Prerequisites

.NET 4.0 or later

An environment which supports TLS 1.2 (see the TLS-update site for more information)

Usage

Binaries

It is not mandatory to fork this repository for using the PayPal SDK. You can refer PayPal Checkout Server SDK for configuring and working with SDK without forking this code.

For contirbuting or referrring the samples, You can fork/refer this repository.

Setting up credentials

Get client ID and client secret by going to https://developer.paypal.com/developer/applications and generating a REST API app. Get Client ID and Secret from there.

using System;
using PayPalCheckoutSdk.Core;
using PayPalCheckoutSdk.Orders;
using BraintreeHttp;
using System.Collections.Generic;
using System.Threading.Tasks;

public class CaptureOrderSample 
{
    static String clientId = "PAYPAL-CLIENT-ID";
    static String secret = "PAYPAL-CLIENT-SECRET";

    public static HttpClient client()
    {
    // Creating a sandbox environment
    PayPalEnvironment environment = new SandboxEnvironment(clientId, secret);

    // Creating a client for the environment
    PayPalHttpClient client = new PayPalHttpClient(environment);
    return client;
    }
}

Examples

Creating an Order

This will create an order and print order id for the created order

public async static Task<HttpResponse> createOrder()
 {
         HttpResponse response;
        // Construct a request object and set desired parameters
        // Here, OrdersCreateRequest() creates a POST request to /v2/checkout/orders
        var order = new OrderRequest() {
                Intent = "CAPTURE",
                PurchaseUnits = new List<PurchaseUnitRequest>()
                {
                    new PurchaseUnitRequest()
                    {
                        Amount = new AmountWithBreakdown()
                        {
                            CurrencyCode = "USD",
                            Value = "100.00"
                        }
                    }
                },
                ApplicationContext = new ApplicationContext()
                {
                    ReturnUrl = "https://www.example.com",
                    CancelUrl = "https://www.example.com"
                }
            };


            // Call API with your client and get a response for your call
            var request = new OrdersCreateRequest();
            request.Prefer("return=representation");
            request.RequestBody(order);
            response = await client().Execute(request);
            var statusCode = response.StatusCode;
            Order result = response.Result<Order>();
            Console.WriteLine("Status: {0}", result.Status);
            Console.WriteLine("Order Id: {0}", result.Id);
            Console.WriteLine("Intent: {0}", result.Intent);
            Console.WriteLine("Links:");
            foreach (LinkDescription link in result.Links)
            {
                 Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
            }
            return response;
    }

Capturing an Order

Before capturing an order, order should be approved by the buyer using the approve link in create order response

  public async static Task<HttpResponse> captureOrder()
    {
            // Construct a request object and set desired parameters
            // Replace ORDER-ID with the approved order id from create order
            var request = new OrdersCaptureRequest("69E54436G09638353");
            request.RequestBody(new OrderActionRequest());
            HttpResponse response = await client().Execute(request);
            var statusCode = response.StatusCode;
            Order result = response.Result<Order>();
            Console.WriteLine("Status: {0}", result.Status);
            Console.WriteLine("Capture Id: {0}", result.Id);
            return response;
    }

Running tests

To run integration tests using your client id and secret, run the test gradle command with the -Pintegration flag

$ PAYPAL_CLIENT_ID=YOUR_SANDBOX_CLIENT_ID PAYPAL_CLIENT_SECRET=YOUR_SANDBOX_CLIENT_SECRET dotnet test -v normal

You may use the client id and secret above for demonstration purposes.

Samples

You can start off by trying out creating and capturing an order.

To try out different samples for both create and authorize intent head to this link.

Note: Update the PayPalClient.cs with your sandbox client credentials or pass your client credentials as environment variable whie executing the samples.

License

Code released under SDK LICENSE

checkout-net-sdk-1's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.