square / connect-api-examples

Code samples demonstrating the functionality of the Square Connect API
https://developer.squareup.com/docs/sample-apps
389 stars 792 forks source link

PHP Payment Example Question #359

Closed bug-logic closed 1 year ago

bug-logic commented 1 year ago

Using the PHP Payment Example, there is no explanation on how to send additional values to the process-payment file. For example, assuming the Sandbox test returns a Payment Successful response and the form being used is as follows:

<form class="payment-form" id="fast-checkout">
  <input type="hidden"  name="amount" id="amount" value="10">
  <input type="hidden" name="address"  id="address" value="200 Maple Street">
    <div class="wrapper">
      <div id="card-container"></div>
      <button id="card-button" type="button">Pay with Card</button>
      <span id="payment-flow-message"></span>
    </div>
  </form>

What is the procedure for obtaining the values for amount and address?

$amount = intval($_POST['amount']);

Is clearly a non-starter.

bug-logic commented 1 year ago

If anyone shows up with the same question, here is the answer. Assuming you have some input fields, the values of which you need to pass along to process-payment.php, such as:

<input type="hidden" id="amount" value="40">
<input type="hidden" id="customer" value="Mr. Big">
<input type="hidden" id="whatever" value="Something else">

Open up the sq-payment-flow.js file At the TOP of the file, get the values you need. For example:

amount = document.getElementById('amount').value;
customer = document.getElementById('customer').value;
whatever = document.getElementById('whatever').value;

Now roll down the sq-payment-flow.js file and look for function(token) and put your values in there. For example:

window.createPayment = async function(token) {
  const dataJsonString = JSON.stringify({
    token,
    amount,
    customer,
    whatever
  });

Now crack open the provided process-payment.php file, and BELOW $data = json_decode($json); collect your variables in the $data return. For example:

$amount = $data->amount;
$customer = $data->customer;
$whatever = $data->whatever;

And that's that. Not sure why this is not clearly documented, but there ya go.