Created
May 31, 2017 09:27
-
-
Save rhaseven7h/9deb07316effee669faffe967579b673 to your computer and use it in GitHub Desktop.
Golang PayPal Quick And Dirty Shopping Cart Upload and Payment Execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"github.com/logpacker/PayPal-Go-SDK" | |
) | |
// All Constants // | |
const ( | |
PPAccount = "[email protected]" | |
PPClientID = "<The Client ID from PayPal Live or Sandbox>" | |
PPSecret = "<The Secret from Paypal Live or Sandbox>" | |
) | |
// A Hacky struct pretty printer for console | |
func pp(msg string, v interface{}) { | |
b, err := json.MarshalIndent(v, "", " ") | |
if err != nil { | |
log.Fatal(err) | |
} | |
sep := strings.Repeat("-", len(msg)) | |
fmt.Printf("\033[1;33m%s\n%s\n%s\n%s\n\n\033[0m", sep, msg, sep, string(b)) | |
} | |
func main() { | |
fmt.Println("Starting PayPal Tests") | |
fmt.Println("=====================") | |
fmt.Println() | |
c, err := paypalsdk.NewClient(PPClientID, PPSecret, paypalsdk.APIBaseSandBox) | |
if err != nil { | |
log.Fatal(err) | |
} | |
c.SetLog(os.Stderr) | |
accessToken, err := c.GetAccessToken() | |
if err != nil { | |
log.Fatal(err) | |
} | |
pp("accessToken", accessToken) | |
if len(os.Args) == 2 { | |
paymentID := strings.TrimSpace(os.Args[1]) | |
payment, err2 := c.GetPayment(paymentID) | |
if err2 != nil { | |
log.Fatal(err2) | |
} | |
pp("payment", payment) | |
return | |
} | |
if len(os.Args) == 3 { | |
paymentID := strings.TrimSpace(os.Args[1]) | |
payerID := strings.TrimSpace(os.Args[2]) | |
executeResponse, err2 := c.ExecuteApprovedPayment(paymentID, payerID) | |
if err2 != nil { | |
log.Fatal(err2) | |
} | |
pp("executeResponse", executeResponse) | |
return | |
} | |
customPayment := paypalsdk.Payment{ | |
Intent: "sale", | |
Payer: &paypalsdk.Payer{ | |
PaymentMethod: "paypal", | |
}, | |
Transactions: []paypalsdk.Transaction{ | |
paypalsdk.Transaction{ | |
Amount: &paypalsdk.Amount{ | |
Currency: "MXN", | |
Total: "120.00", | |
}, | |
Description: "Some description here", | |
Custom: "user_id:123", | |
SoftDescriptor: "DULC-MDJ", | |
ItemList: &paypalsdk.ItemList{ | |
Items: []paypalsdk.Item{ | |
paypalsdk.Item{ | |
SKU: "UPCCODE1000", | |
Name: "Item 1 Name", | |
Description: "Some desc for item 1", | |
Quantity: 10, | |
Price: "4.00", | |
Currency: "MXN", | |
}, | |
paypalsdk.Item{ | |
SKU: "UPCCODE1001", | |
Name: "Item 2 Name", | |
Description: "Some desc for item 2", | |
Quantity: 4, | |
Price: "20.00", | |
Currency: "MXN", | |
}, | |
}, | |
}, | |
}, | |
}, | |
RedirectURLs: &paypalsdk.RedirectURLs{ | |
CancelURL: "https://dulceriamdj.com:7143/paypal/cancel", | |
ReturnURL: "https://dulceriamdj.com:7143/paypal/approved", | |
}, | |
} | |
paymentResponse, err := c.CreatePayment(customPayment) | |
if err != nil { | |
log.Fatal(err) | |
} | |
pp("paymentResponse", paymentResponse) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment