Skip to content

mantyr/PayPal-Go-SDK

 
 

Repository files navigation

Build Status Godoc Chat at https://gitter.im/logpacker/PayPal-Go-SDK

GO client for PayPal REST API

Coverage

  • POST /v1/oauth2/token
  • POST /v1/payments/payment
  • GET /v1/payments/payment/ID
  • GET /v1/payments/payment
  • GET /v1/payments/authorization/ID
  • POST /v1/payments/authorization/ID/capture
  • POST /v1/payments/authorization/ID/void
  • POST /v1/payments/authorization/ID/reauthorize
  • GET /v1/payments/sale/ID
  • POST /v1/payments/sale/ID/refund
  • GET /v1/payments/refund/ID
  • GET /v1/payments/orders/ID
  • POST /v1/payments/orders/ID/authorize
  • POST /v1/payments/orders/ID/capture
  • POST /v1/payments/orders/ID/do-void
  • POST /v1/identity/openidconnect/tokenservice
  • GET /v1/identity/openidconnect/userinfo/?schema=SCHEMA
  • POST /v1/payments/payouts?sync_mode=true

Missing endpoints

It is possible that some endpoints are missing in this SDK Client, but you can use built-in paypalsdk functions to perform a request: NewClient -> NewRequest -> SendWithAuth

Create Client

import "github.com/logpacker/PayPal-Go-SDK"
// Create a client instance
c, err := paypalsdk.NewClient("clientID", "secretID", paypalsdk.APIBaseSandBox)
c.SetLogFile("/tpm/paypal-debug.log") // Set log file if necessary

Get access token

// When you will have authorization_code you can get an access_token
accessToken, err := c.GetAccessToken()

Create direct paypal payment

// Now we can create a paypal payment
amount := paypalsdk.Amount{
    Total:    "7.00",
    Currency: "USD",
}
redirectURI := "http://example.com/redirect-uri"
cancelURI := "http://example.com/cancel-uri"
description := "Description for this payment"
paymentResult, err := c.CreateDirectPaypalPayment(amount, redirectURI, cancelURI, description)

// If paymentResult.ID is not empty and paymentResult.Links is also
// we can redirect user to approval page (paymentResult.Links[0]).
// After approval user will be redirected to return_url from Request with PaymentID

Create any payment

p := paypalsdk.Payment{
    Intent: "sale",
    Payer: &paypalsdk.Payer{
        PaymentMethod: "credit_card",
        FundingInstruments: []paypalsdk.FundingInstrument{paypalsdk.FundingInstrument{
            CreditCard: &paypalsdk.CreditCard{
                Number:      "4111111111111111",
                Type:        "visa",
                ExpireMonth: "11",
                ExpireYear:  "2020",
                CVV2:        "777",
                FirstName:   "John",
                LastName:    "Doe",
            },
        }},
    },
    Transactions: []paypalsdk.Transaction{paypalsdk.Transaction{
        Amount: &paypalsdk.Amount{
            Currency: "USD",
            Total:    "7.00",
        },
        Description: "My Payment",
    }},
    RedirectURLs: &paypalsdk.RedirectURLs{
        ReturnURL: "http://...",
        CancelURL: "http://...",
    },
}
paymentResponse, err := client.CreatePayment(p)

Execute approved payment

// And the last step is to execute approved payment
// paymentID is returned via return_url
paymentID := "PAY-17S8410768582940NKEE66EQ"
// payerID is returned via return_url
payerID := "7E7MGXCWTTKK2"
executeResult, err := c.ExecuteApprovedPayment(paymentID, payerID)

Get payment by ID

// Get created payment info
payment, err := c.GetPayment(paymentID)

Get list of payments

// Get all payments slice
payments, err := c.GetPayments()

Get authorization by ID

authID := "2DC87612EK520411B"
auth, err := c.GetAuthorization(authID)

Capture authorization

capture, err := c.CaptureAuthorization(authID, &paypalsdk.Amount{Total: "7.00", Currency: "USD"}, true)

Void authorization

auth, err := c.VoidAuthorization(authID)

Reauthorize authorization

auth, err := c.ReauthorizeAuthorization(authID, &paypalsdk.Amount{Total: "7.00", Currency: "USD"})

Get Sale by ID

saleID := "36C38912MN9658832"
sale, err := c.GetSale(saleID)

Refund Sale by ID

// Full
refund, err := c.RefundSale(saleID, nil)
// Partial
refund, err := c.RefundSale(saleID, &paypalsdk.Amount{Total: "7.00", Currency: "USD"})

Get Refund by ID

orderID := "O-4J082351X3132253H"
refund, err := c.GetRefund(orderID)

Get Order by ID

order, err := c.GetOrder(orderID)

Authorize Order

auth, err := c.AuthorizeOrder(orderID, &paypalsdk.Amount{Total: "7.00", Currency: "USD"})

Capture Order

capture, err := c.CaptureOrder(orderID, &paypalsdk.Amount{Total: "7.00", Currency: "USD"}, true, nil)

Void Order

order, err := c.VoidOrder(orderID)

Identity

// Retreive tolen by authorization code
token, err := c.GrantNewAccessTokenFromAuthCode("<Authorization-Code>", "http://example.com/myapp/return.php")
// ... or by refresh token
token, err := c.GrantNewAccessTokenFromRefreshToken("<Refresh-Token>")

Retreive user information

userInfo, err := c.GetUserInfo("openid")

Create single payout to email

payout := paypalsdk.Payout{
    SenderBatchHeader: &paypalsdk.SenderBatchHeader{
        EmailSubject: "Subject will be displayed on PayPal",
    },
    Items: []paypalsdk.PayoutItem{
        paypalsdk.PayoutItem{
            RecipientType: "EMAIL",
            Receiver:      "single-email-payout@mail.com",
            Amount: &paypalsdk.AmountPayout{
                Value:    "15.11",
                Currency: "USD",
            },
            Note:         "Optional note",
            SenderItemID: "Optional Item ID",
        },
    },
}

payoutResp, err := c.CreateSinglePayout(payout)

How to Contribute

  • Fork a repository
  • Add/Fix something
  • Run ./before-commit.sh to check that tests passed and code is formatted well
  • Push to your repository
  • Create pull request

About

Golang client for PayPal REST API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 99.8%
  • Shell 0.2%