简体   繁体   中英

How do I verify if the user paypal payment succeeded/failed and send message to Winform if succeded in C# .NET?

I have to implement a feature on my Winforms app that allow the user to do more stuff on the application. After they successfully made a payment in PayPal, so I have this code below that redirect the user to a PayPal payment page on browser:

private void PaypalPayment_Button(String TypeOf,String Pricing) 
{
    string url = "";

    string business = "nfrealyt@gmail.com";     
    string country = "MY";                  
    string currency = "USD";            

    url += "https://www.paypal.com/cgi-bin/webscr/" + 
        "?cmd=" + "_xclick" +
        "&amount=" + Pricing + 
        "&business=" + business + 
        "&item_name=" + TypeOf;
      

    System.Diagnostics.Process.Start(url);
    // https://www.paypal.com/paypalme/flowstoragepaypal //https://www.paypal.com/cgi-bin/webscr 
}

Everything's good now the main problem I'm dealing with is verifying whether if the user made the transaction or not, say if a transaction is made then execute certain code for example display a MessageBox on the Winforms application:

MessageBox.Show("Account Upgraded","Succeeded");

Is there a way to achieve this? I've done hundreds of googling but to no success.

bool paymentMade = false;

private void PaypalPayment_Button(String TypeOf,String Pricing) 
{
    string url = "";

    string business = "mygmail@gmail.com";     
    string country = "MY";                  
    string currency = "USD";            

    url += "https://www.paypal.com/cgi-bin/webscr/" + 
        "?cmd=" + "_xclick" +
        "&amount=" + Pricing + 
        "&business=" + business + 
        "&item_name=" + TypeOf;
      

    System.Diagnostics.Process.Start(url);
    paymentMade = true;
    if(paymentMade == true)
    {
        MessageBox.Show("Payment has made");
    }
}

I was expecting for a MessageBox message to be display on the WinForms as soon the user has successfully made the payment.

Change your integration to use the v2/checkout/orders API and make two routes (url paths) on your server, one for 'Create Order' and one for 'Capture Order'. You could use the (recently deprecated) Checkout.NET-SDK for the routes' API calls to PayPal, or your own HTTPS implementation of first getting an access token and then doing the call. Both of these routes should return/output only JSON data (no HTML or text). Inside the 2nd route, when the capture API is successful you should verify the amount was correct and store its resulting payment details in your database (particularly purchase_units[0].payments.captures[0].id , which is the PayPal transaction ID) and perform any necessary business logic (such as reserving product or sending an email) immediately before forwarding return JSON to the frontend caller. In the event of an error forward the JSON details of it as well, since the frontend must handle such cases.

Pair those 2 routes with this frontend approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server .

If you need to send any additional data from the client to the server, such having them fill the form out first in your case, add a body parameter to the fetch with a value that is a JSON string or object. The JS SDK also has various onInit / onClick functions you can use to verify the form is valid before creating the PayPal order.

If your intended flow is to instead collect form data after the PayPal transaction successfully captures, you can initiate your flow from the onApprove function, but in making it a separate two-step process there are additional complications you must design around, for instance what happens if the PayPal transaction is completed and then the user stops or their browser/tab is closed without entering additional form data you need/want to collect for the transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM