skip to Main Content

Dears all,
I’m starting with payPal,
and I’ve tried to implement the standard sample provided with SDK (c#, FW 4.6.1)

here below my server-side method

    public async static Task<PayPalHttp.HttpResponse> CreateOrder()
    {
        OrdersCreateRequest oRequest = new OrdersCreateRequest();
        oRequest.Prefer("return=representation");
        //System.Net.ServicePointManager.Expect100Continue = true;
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
        oRequest.RequestBody(BuildRequestBodyWithMinimumFields());
        //3. Call PayPal to set up a transaction
        PayPalHttp.HttpClient oClient = PayPalClient.client();
        oClient.SetConnectTimeout(new TimeSpan(0, 0, 0, 10, 0));
        var oResponse = await oClient.Execute(oRequest);
        var result = oResponse.Result<Order>();

        return oResponse;
}

while here jquery call

paypal.Buttons({
    style: {
        shape: 'rect',
        color: 'blue',
        layout: 'vertical',
        label: 'pay',

    },
    createOrder: function () {
        return fetch('/shop/paypal_test.aspx/CreateOrder', {
            method: 'post',
            headers: {
                'content-type': 'application/json'
            }
        }).then(function (res) {
            return res.json();
        }).then(function (data) {
            return data.orderID; // Use the same key name for order ID on the client and server
        });
    }
}).render('#paypal-button-container');

The issue is that the response to oClient.Execute never gets back.

PayPalClient has been built exactly as SDK sample.

Looking at PayPal API Calls Log, the API is called correctly and it is marked with a green flag.

Have you some ideas?

thank you in advance

2

Answers


  1. Chosen as BEST ANSWER

    Dears, I've finally found a solution.

    As said before, after API call, on developer.paypal web site the related call has been marked with a green flag, that means that the call has been made correctly.

    Then, I've investigated on server code, and how the tasks are managed.

    The asynchrounous call is embedded in a private method and it's called by a sync method that "waits" for the response.

    In this way all works correctly

    Here the snippet code that allows the routine to work and not to be stopped to "client.Execute(oRequest)".

    [WebMethod(EnableSession = true)]
    public static PayPalCheckoutSdk.Orders.Order SetupTransaction()
    {
        data.tessutigenovataddei.com.Order oOrder = null;
        //save order in database.
        oOrder = CreateOrderWithPaypal();
    
        OrdersCreateRequest oRequest = new OrdersCreateRequest();
        oRequest.Prefer("return=representation");
    
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
        oRequest.RequestBody(BuildRequestBodyWithMinimumFields(oOrder));
    
        PayPalHttp.HttpClient oClient = PayPalClient.client();
        oClient.SetConnectTimeout(new TimeSpan(0, 0, 0, 10, 0));
        var oResponseTask = SetupTransactionAsync(oClient, oRequest);
    
        oResponseTask.Wait();
    
        return oResponseTask.Result.Result<PayPalCheckoutSdk.Orders.Order>();
    }
    
    private async static Task<PayPalHttp.HttpResponse> SetupTransactionAsync(PayPalHttp.HttpClient client, OrdersCreateRequest request)
    {
        SynchronizationContext.SetSynchronizationContext(null);
        PayPalHttp.HttpResponse oResponse = await client.Execute(request);
    
        return oResponse;
    }
    

    thank you for the support


  2. What do you mean "never gets back" ? You say there is a "green flag", does this mean there was an HTTP 2xx success response? Was there an id in the response body?

    If you are forwarding the response body to your front end, the front end should not be looking for the data.orderID key, since there will be no such key unless you provide it. Here is a better sample to use for your front end, which reads data.id : https://developer.paypal.com/demo/checkout/#/pattern/server

    Note that that sample also includes a fetch in the onApprove function. You need to implement such a route that will capture your order, otherwise there will be no transaction created.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search