title: Paypal Checkout client integration - Problem with returning order id promise
body: <p>i have a problem integrating paypals payment gateway. I am using javascript for the client, python for the backend and the checkouts v2 api.</p>
  <p>Creating a order on the backend works without trouble, but while waiting for my servers response the createOrder function raises a error:</p>
  <pre><code>unhandled_error 
  Object { err: &quot;Expected an order id to be passed\nLe/&lt;/&lt;@https://www.sandbox.paypal.com/smart/buttons?style.layout=vertical&amp;style.color=blue&amp;style.shape=rect&amp;style.tagline=false&amp;components.0=buttons&amp;locale.country=NO&amp;locale.lang=no&amp;sdkMeta=eyJ1cmwiOiJodHRwczovL3d3dy5wYXlwYWwuY29tL3Nkay9qcz9jbGllbnQtaWQ9QWJmSjNNSG5oMkFIU1ZwdXl4eW5lLXBCbHdJZkNsLXpyVXc1dzFiX29TVUloZU01LXNMaDNfSWhuTnZkNUhYSW5wcXVFdm5MZG1LN0xOZ1gmZGlzYWJsZS1mdW5kaW5nPWNyZWRpdCxjYXJkIiwiYXR0cnMiOnt9fQ&amp;clientID=AbfJ3MHnh2AHSVpuyxyne-pBlwIfCl-zrUw5w1b_oSUIheM5-sLh3_IhnNvd5HXInpquEvnLdmK7LNgX&amp;sessionID=e2ea737589_mtc6mtu6mdi&amp;buttonSessionID=de4bfb3626_mtc6mjm6mtk&amp;env=sandbox&amp;fundingEligibility=eyJwYXlwYWwiOnsiZWxpZ2libGUiOnRydWV9LCJjYXJkIjp7ImVsaWdpYmxlIjpmYWxzZSwiYnJhbmRlZCI6dHJ1ZSwidmVuZG9ycyI6eyJ2aXNhIjp7ImVsaWdpYmxlIjp0cnVlfSwibWFzdGVyY2FyZCI6eyJlbGlnaWJsZSI6dHJ1ZX0sImFtZXgiOnsiZWxpZ2libGUiOnRydWV9LCJkaXNjb3ZlciI6eyJlbGlnaWJsZSI6ZmFsc2V9LCJoaXBlciI6eyJlbGlnaWJsZSI6ZmFsc2V9LCJlbG8iOnsiZWxpZ2libGUiOmZhbHNlfSwiamNiIjp7ImVsaWdpYmxlIjpmYWxzZX19…&quot;, timestamp: &quot;1593537805136&quot;, referer: &quot;www.sandbox.paypal.com&quot;, sessionID: &quot;e2ea737589_mtc6mtu6mdi&quot;, env: &quot;sandbox&quot;, buttonSessionID: &quot;de4bfb3626_mtc6mjm6mtk&quot; }
  Error: Expected an order id to be passed 
  Error: Expected an order id to be passed 
  12V21085461823829  // ticks in a few seconds later
  </code></pre>
  <p><a href="https://i.stack.imgur.com/SwT3y.png" rel="nofollow noreferrer">Console screenshot</a></p>
  <p>The problem seems to be that createOrder does not wait for the promise before raising the error, or that the promise is not given in the correct way. Something like that. Anyways here is the client side code:</p>
  <pre><code>paypal.Buttons({
      
      // button styling removed for clarity
  
      createOrder: function() {
          // purchase information 
          var data =  {
              'track_id': vm.selectedTrack.id,
              'lease_id': vm.selectedLease.id,
          }
          
          // post req to api with lease and track ids 
          // create payment on server side
          fetch('http://localhost:5000/api/paypal/create-purchase', {
              method: 'post',
              headers: {
                  'content-type': 'application/json'
              },
              body: JSON.stringify(data),
          }).then(function(res) {
              return res.json();
          }).then(function(data) {
              console.log(data.order_id)
              return data.order_id
          })
      }
  // conatiner element to render buttons in
  }).render('#paypal-button');
  </code></pre>
  <p>And the server side:</p>
  <pre><code>@app.route('/api/paypal/create-purchase', methods=['POST'])
  def paypal_create_purchase():
  
      # cart validation removed for clarity
  
      # create paypal purchase
  
      environment = SandboxEnvironment(client_id=app.config['PAYPAL_PUBLIC'], client_secret=app.config['PAYPAL_PRIVATE'])
      client = PayPalHttpClient(environment)
  
      paypal_request = OrdersCreateRequest()
      paypal_request.prefer('return=representation')
      paypal_request.request_body (
          {
              &quot;intent&quot;: &quot;CAPTURE&quot;,
              &quot;purchase_units&quot;: [
                  {
                      &quot;amount&quot;: {
                          &quot;currency_code&quot;: &quot;USD&quot;,
                          &quot;value&quot;: lease.price
                      }
                  }
              ]
          }
      )
      try:
          # Call API with your client and get a response for your call
          response = client.execute(paypal_request)
          order = response.result
          print(order.id)
  
      except IOError as ioe:
          print (ioe)
          if isinstance(ioe, HttpError):
              # Something went wrong server-side
              print(ioe.status_code)
  
      # note that it is the same key as on the client
      return jsonify(success=True,order_id=order.id)
      
  </code></pre>
  <p>I found this <a href="https://stackoverflow.com/questions/57197907/paypal-expected-an-order-id-to-be-passed">similar thread</a>, but i dont consider the origin of the error to be the same as in that thread (incorrect json key on client)</p>
  <p>Also see this relevant <a href="https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/#on-the-client" rel="nofollow noreferrer">page in the docs</a> which supplies this code:</p>
  <pre><code>createOrder: function() {
      return fetch('/my-server/create-paypal-transaction', {
          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
      });
  }
  </code></pre>