【问题标题】:Sample code in php to integrate square-connect payment gatewayphp中集成square-connect支付网关的示例代码
【发布时间】:2016-07-30 16:00:05
【问题描述】:

我是集成 square-connect 支付网关的新手。我可以在 php 中找到示例代码、集成 API 等吗?

提前致谢。

【问题讨论】:

    标签: square-connect


    【解决方案1】:

    有多种语言的代码示例available here

    --da3mon

    【讨论】:

    【解决方案2】:
    <?php
    require 'vendor/autoload.php';
    
    $access_token = 'sandbox-sq0atb-4K2efNBH8i9ltb5SoefQeA';
    # setup authorization
    \SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($access_token);
    # create an instance of the Location API
    $locations_api = new \SquareConnect\Api\LocationsApi();
    $locationid = '';
    try {
      $locations = $locations_api->listLocations();
      $dr = $locations->getLocations();
      $locationid = $dr[0]['id'];
    } catch (\SquareConnect\ApiException $e) {
      echo "Caught exception!<br/>";
      print_r("<strong>Response body:</strong><br/>");
      echo "<pre>"; var_dump($e->getResponseBody()); echo "</pre>";
      echo "<br/><strong>Response headers:</strong><br/>";
      echo "<pre>"; var_dump($e->getResponseHeaders()); echo "</pre>";
      exit(1);
    }
    ?>
    <html>
    <head>
      <title>My Payment Form</title>
      <!-- link to the SqPaymentForm library -->
      <script type="text/javascript" src="https://js.squareup.com/v2/paymentform"></script>
      <script type="text/javascript">
        window.applicationId =
          <?php
            echo "\"";
            echo ($_ENV["USE_PROD"] == 'true')  ?  $_ENV["PROD_APP_ID"]
                                                :  $_ENV["SANDBOX_APP_ID"];
            echo "\"";
          ?>;
        window.locationId =
        <?php
          echo "\"";
          echo ($_ENV["USE_PROD"] == 'true')  ?  $_ENV["PROD_LOCATION_ID"]
                                              :  $_ENV["SANDBOX_LOCATION_ID"];
          echo "\"";
        ?>;
      </script>
    
      <!-- link to the local SqPaymentForm initialization -->
      <script type="text/javascript" src="https://cdn.jsdelivr.net/gh/square/connect-api-examples/templates/web-ui/payment-form/custom/sq-payment-form.js"></script>
      <!-- link to the custom styles for SqPaymentForm -->
      <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/square/connect-api-examples/templates/web-ui/payment-form/custom/sq-payment-form.css">
    </head>
    <body>
      <!-- Begin Payment Form -->
      <div class="sq-payment-form">
        <!--
          Square's JS will automatically hide these buttons if they are unsupported
          by the current device.
        -->
        <div id="sq-walletbox">
          <button id="sq-google-pay" class="button-google-pay"></button>
          <button id="sq-apple-pay" class="sq-apple-pay"></button>
          <button id="sq-masterpass" class="sq-masterpass"></button>
          <div class="sq-wallet-divider">
            <span class="sq-wallet-divider__text">Or</span>
          </div>
        </div>
        <div id="sq-ccbox">
          <!--
            You should replace the action attribute of the form with the path of
            the URL you want to POST the nonce to (for example, "/process-card").
            You need to then make a "Charge" request to Square's transaction API with
            this nonce to securely charge the customer.
            Learn more about how to setup the server component of the payment form here:
            https://docs.connect.squareup.com/payments/transactions/processing-payment-rest
          -->
          <form id="nonce-form" action="process-card.php" method="post">
            <div class="sq-field">
              <label class="sq-label">Card Number</label>
              <div id="sq-card-number">
              <input type="text" name="card_number"></div>
            </div>
            <div class="sq-field-wrapper">
              <div class="sq-field sq-field--in-wrapper">
                <label class="sq-label">CVV</label>
                <div id="sq-cvv">
                <input type="text" name="cvv"></div>
              </div>
              <div class="sq-field sq-field--in-wrapper">
                <label class="sq-label">Expiration</label>
                <div id="sq-expiration-date">
                <input type="text" name="expiration"></div>
              </div>
              <div class="sq-field sq-field--in-wrapper">
                <label class="sq-label">Postal</label>
                <div id="sq-postal-code">
                <input type="text" name="postal"></div>
              </div>
            </div>
            <div class="sq-field">
              <button id="sq-creditcard" class="sq-button" onclick="requestCardNonce(event)">
                Pay $1.00 Now
              </button>
            </div>
            <!--
              After a nonce is generated it will be assigned to this hidden input field.
            -->
            <div id="error"></div>
            <input type="hidden" id="card-nonce" name="nonce">
          </form>
        </div>
      </div>
      <!-- End Payment Form -->
    
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 2018-09-11
      • 2015-11-12
      • 1970-01-01
      • 2011-05-05
      相关资源
      最近更新 更多