Giter Site home page Giter Site logo

Execute payment about paypal HOT 3 CLOSED

plutov avatar plutov commented on July 24, 2024
Execute payment

from paypal.

Comments (3)

plutov avatar plutov commented on July 24, 2024

I have an example in README, did you try it, and what kind of error do you have, also please post your code in the issue.

// paymentID is returned via return_url
paymentID := "PAY-17S8410768582940NKEE66EQ"
// payerID is returned via return_url
payerID := "7E7MGXCWTTKK2"
executeResult, err := c.ExecuteApprovedPayment(paymentID, payerID)

from paypal.

kingwill101 avatar kingwill101 commented on July 24, 2024

@plutov sorry for the late response

so this is the endpoint im using to start the transaction

func paypalDonationPost(c *gin.Context) {

// Create a client instance
    client, err := paypalsdk.NewClient(appConfig.Paypal.ClientID, appConfig.Paypal.Secret, paypalsdk.APIBaseSandBox)

    if err != nil {
        log.Println(err.Error())
        getSiteInfo(c).Session.AddFlash("Unable to process donation at this time")
        c.Redirect(302, "/donate")
    }

    // When you will have authorization_code you can get an access_token
    _, err = client.GetAccessToken()
    if err != nil {
        log.Println("access token", err.Error())
        getSiteInfo(c).Session.AddFlash("Unable to process donation at this time")
        c.Redirect(302, "/donate")
    }
    //client.SetAccessToken(accessToken.Token)
    log.Println(d)
    // Now we can create a paypal payment
    redirectURI := fmt.Sprint("https://example.com/donate/confirm/?transactionID=", d.ID)
    cancelURI := fmt.Sprint("https://example.com/donate/cancel/?transactionID=", d.ID)
p := paypalsdk.Payment{
        Intent: "sale",
        Payer: &paypalsdk.Payer{
            PaymentMethod: "paypal",
        },
        Transactions: []paypalsdk.Transaction{{
            ItemList: &paypalsdk.ItemList{
                Items: []paypalsdk.Item{
                    {
                        Name:     "Donation to ",
                        SKU:      "Donation to",
                        Currency: "USD",
                        Price:    amount,
                        Quantity: 1,
                    },
                },
            },

            Amount: &paypalsdk.Amount{
                Total:    amount,
                Currency: "USD",
            },
            Description: "My Payment",
            Custom:      "Some custom text and all",
        }},
        RedirectURLs: &paypalsdk.RedirectURLs{
            ReturnURL: redirectURI,
            CancelURL: cancelURI,
        },
    }
    res, err := client.CreatePayment(p)
    if err != nil {
        fmt.Fprint(c.Writer, err.Error())
    } else {
        log.Print(res.Links[0])
        log.Print(res.Links[1])
        c.Redirect(302, res.Links[1].Href)
    }
}

and after the user goes through the payment process and accepts paypal routes back to this endpoint


func PaymentSuccess(c *gin.Context) {

    uID, _ := strconv.Atoi(c.Param("transactionID"))

    // paymentID is returned via return_url
    paymentID := c.Param("paymentId")
    // payerID is returned via return_url
    payerID := c.Param("PayerID")

    client, err := paypalsdk.NewClient(appConfig.Paypal.ClientID, appConfig.Paypal.Secret, paypalsdk.APIBaseSandBox)

    log.Println(uID, paymentID, payerID)

    if err != nil {
        c.Writer.Write([]byte(fmt.Sprintf("failed to get new client")))
        return

    }

    _, err = client.GetAccessToken()

    if err != nil {
        c.Writer.Write([]byte(fmt.Sprint("failed to get new token ", err.Error())))
        return

    }

    executeResult, er := client.ExecuteApprovedPayment(paymentID, payerID)

    if er != nil {
        c.Writer.Write([]byte(fmt.Sprint("failed to execute payment  - ", er.Error(), "   Result -->", executeResult)))
        return
    }

    html := "<html><body><h1>Thank you! Paymen accepted!</h1></body></html>"
    c.Writer.Write([]byte(fmt.Sprint(html, uID, executeResult)))
    return

}

result after trying to approve payment

failed to execute payment  - POST https://api.sandbox.paypal.com/v1/payments/payment//execute: 500    Result -->&{ []  []}

from paypal.

plutov avatar plutov commented on July 24, 2024

As I can see from this log and error, paymentID is empty, because URL is missing it - POST https://api.sandbox.paypal.com/v1/payments/payment//execute

from paypal.

Related Issues (20)

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.