简体   繁体   中英

PayPal Smart Buttons Server Side Integration is failing on live mode

I am working on an application in php where I have to integrate PayPal Smart Buttons to my application. Everything is working fine in sandbox mode. But when I turn it to Live or Production Environment. It gives JSON error.

PayPal Smart Buttons returns me JSON error

This answer helped me alot while setting up my environment for sandbox and everything else, this is working really fine. but only for sandbox mode!

I checked with account on paypal, the transactions are created there, but no response is being sent to server for further processing. It indicates the API is setup successfully and keys are good. But only issue is I m unable to capture response and send it back to server here is my code

create-paypal-transaction.php

public static function createOrder($debug=false)
  {
    $request = new OrdersCreateRequest();
    $request->prefer('return=representation');
    $request->body = self::buildRequestBody();
   // 3. Call PayPal to set up a transaction
    $client = PayPalClient::client();
    $response = $client->execute($request);

    // 4. Return a successful response to the client.
    $json_obj= array('id'=>$response->result->id);
    $jsonstring = json_encode($json_obj);
    echo $jsonstring;
  }

on client side

createOrder: function() {
      return fetch('payments/create-paypal-transaction.php', {
        method: 'post',
        headers: {
          'content-type': 'application/json'
        },
        body: JSON.stringify($('#form_add').serializeObject())
      }).then(function(res) {
        console.log(res);
        return res.json();
      }).then(function(data) {
        console.log(data);
        return data.id;
      });

I used console.log() for debugging server response but it is always

{
...
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
...
}

also the console shows below errors

create_order_error 

click_initiate_payment_reject

Uncaught SyntaxError: Unexpected token < in JSON at position 0

ANY HELP WOULD BE APPRECIATED:)

Im getting the same issue, i've read that you need to redirect them to the approval url like such

    public static function createOrder($debug=false)
  {
    $request = new OrdersCreateRequest();
    $request->prefer('return=representation');
    $request->body = self::buildRequestBody();

    // 3. Call PayPal to set up a transaction
    $client = PayPalClient::client();
    $response = $client->execute($request);
    $res_links = $response->result->links;
    $approve_index = array_search('approve',array_column($res_links,'rel'));
    redirect($res_links[$approve_index]->href);

    // 4. Return a successful response to the client.
    $json_obj= array('id'=>$response->result->id);
    $jsonstring = json_encode($json_obj);
    echo $jsonstring;
   }

This ends up giving a cors error and when bypassing the cors checking in chrome(which is terrible) it simply closes the checkout popup and the response for the approval url is the html for the page.

In the network tab in your dev console, check the response for the create-paypal-transaction.php file. It'l give you the actual error the json > error is unrelated. You can also visit the file directly from your browser and see what errors you get there.

You can message me on discord if youd like im also working on getting this working. REDRUM#9269

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