Paypal Buttons Define Price With Serverside Integration

stevedevtech
Contributor
Contributor

Hi there, hopefully someone can help me. 

 

I've had the paypal smart payments buttons client side integration working on my site for a while now. 

 

However, I'm wanting to handle some of the processes on the server side now (PHP 7.1), basically so there is no chance someone on the client side could render the buttons at a different price than I want. 

 

I was looking at the sdk documentation (https://developer.paypal.com/docs/business/checkout/server-side-api-calls/create-order/) to create an order. I have the sdk installed with composer and I have a post request from the paypal buttons createOrder method reaching the CreateOrder class its createOrder function.

 

I set up some additional error logs, and when the server tries to execute the request, I get this error {"error":"invalid_client","error_description":"Client Authentication failed"}

 

I have my sandbox credentials in place properly. That's not an issue. 

 

First part of my client side js paypal buttons code

 

paypal.Buttons({
        createOrder: function(data, actions) {
            console.log(data, actions);
            return fetch('/createReservationOrder', {
              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
            });
        },

 

 

 

The paypal create order class reached via REST API Post at /createReservationOrder. Im currently passing the price as 10 (for $10) as a test amount. 

 

<?php

namespace Sample\CaptureIntentExamples;

require 'vendor/autoload.php';
//1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
use Sample\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;

class CreateOrder
{

// 2. Set up your server to receive a call from the client
  /**
   *This is the sample function to create an order. It uses the
   *JSON body returned by buildRequestBody() to create an order.
   */
  public static function createOrder($price, $debug=false)
  {
    $request = new OrdersCreateRequest();
    $request->prefer('return=representation');
    $request->body = self::buildRequestBody($price);
   // 3. Call PayPal to set up a transaction
    $client = PayPalClient::client();
    $response = $client->execute($request);

    // 4. Return a successful response to the client.
    return $response;
  }

  /**
     * Setting up the JSON request body for creating the order with minimum request body. The intent in the
     * request body should be "AUTHORIZE" for authorize intent flow.
     *
     */
    private static function buildRequestBody($price)
    {
        return array(
            'intent' => 'CAPTURE',
            'application_context' =>
                array(
                    'brand_name' => "Camp Avalon Reservation Suggested Donation",
                    'shipping_preference' => "NO_SHIPPING",
                    'return_url' => 'https://campavalon.org/reserve',
                    'cancel_url' => 'https://campavalon.org/reserve'
                ),
            'purchase_units' =>
                array(
                    0 =>
                        array(
                            'amount' =>
                                array(
                                    'currency_code' => 'USD',
                                    'value' => "$price"
                                )
                        )
                )
        );
    }
}


/**
 *This is the driver function that invokes the createOrder function to create
 *a sample order.
 */
if (!count(debug_backtrace()))
{
  CreateOrder::createOrder(true);
}

 

 

 

Any help is appreciated. Many thanks.  

Login to Me Too
1 REPLY 1

JackieDaytona
Contributor
Contributor

Check up on Stackoverflow - I think I saw your answer up there

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.