phpでSquareを用いた決済の実装を行っています。
まずはサンプルを試しています。ダウンロードしたサンプルはこちら。
https://github.com/square/connect-api-examples/tree/master/connect-examples/v2/php_payment
テストで本番決済できましたが、
Squareの管理画面のお取引に表示される商品が「任意の金額」となっております。
こちらにphpで吐き出した注文IDや商品名などを表示(登録)させたいのですが、
方法がわかりません…
ご存じの方いらっしゃいましたら、ご教示いただけますと幸いです。
以下、コードです。(gitのサンプルほぼそのままですが)
index.php(決済フォームのあるページ)
<?php require_once ("../../vendor/autoload.php"); use Square\Environment; // dotenv is used to read from the '.env' file created for credentials //$dotenv = Dotenv\Dotenv::create(__DIR__); //$dotenv->load(); // Pulled from the .env file and upper cased e.g. sandbox, production. $upper_case_environment = strtoupper("production"); ?> <html> <head> <title>My Payment Form</title> <!-- link to the SqPaymentForm library --> <script type="text/javascript" src= <?php echo "\""; echo ("production" === Environment::PRODUCTION) ? "https://js.squareup.com/v2/paymentform" : "https://js.squareupsandbox.com/v2/paymentform"; echo "\""; ?> ></script> <script type="text/javascript"> window.applicationId ="アプリケーションIDをもってきます"; window.locationId ="ロケーションIDをもってきます"; </script> <!-- link to the local SqPaymentForm initialization --> <script type="text/javascript" src="js/sq-payment-form.js"></script> <!-- link to the custom styles for SqPaymentForm --> <link rel="stylesheet" type="text/css" href="css/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-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 Payments API with this nonce to securely charge the customer. Learn more about how to setup the server component of the payment form here: https://developer.squareup.com/docs/payments-api/overview --> <form id="nonce-form" novalidate action="./process-card.php" method="post"> <input type="hidden" value="12121212" name="order_id"> <input type="hidden" value="100" name="total_price"> <div class="sq-field"> <label class="sq-label">カード番号</label> <div id="sq-card-number"></div> </div> <div class="sq-field-wrapper"> <div class="sq-field sq-field--in-wrapper"> <label class="sq-label">セキュリティコード</label> <div id="sq-cvv"></div> </div> <div class="sq-field sq-field--in-wrapper"> <label class="sq-label">有効期限</label> <div id="sq-expiration-date"></div> </div> <div class="sq-field sq-field--in-wrapper"> <label class="sq-label">郵便番号</label> <div id="sq-postal-code"></div> </div> </div> <div class="sq-field"> <button id="sq-creditcard" class="sq-button" onclick="onGetCardNonce(event)"> 100円を支払う </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>
process-card.php(決済の処理を行うページ)
<?php // Note this line needs to change if you don't use Composer: // require('square-php-sdk/autoload.php'); require_once ("../../vendor/autoload.php"); //use Dotenv\Dotenv; use Square\Models\Money; use Square\Models\CreatePaymentRequest; use Square\Exceptions\ApiException; use Square\SquareClient; //追加 前ページからIDと金額を取得。※現在は前ページはべた書き $order_id = $_POST["order_id"]; $total_price = $_POST["total_price"]; // Pulled from the .env file and upper cased e.g. SANDBOX, PRODUCTION. $upper_case_environment = strtoupper("production"); // The access token to use in all Connect API requests. // Set your environment as *sandbox* if you're just testing things out. $access_token = "アクセストークンをもってくる"; // Initialize the Square client. $client = new SquareClient([ 'accessToken' => $access_token, 'environment' => "production" ]); // Helps ensure this code has been reached via form submission if ($_SERVER['REQUEST_METHOD'] != 'POST') { error_log('Received a non-POST request'); echo 'Request not allowed'; http_response_code(405); return; } // Fail if the card form didn't send a value for `nonce` to the server $nonce = $_POST['nonce']; if (is_null($nonce)) { echo 'Invalid card data'; http_response_code(422); return; } $payments_api = $client->getPaymentsApi(); // To learn more about splitting payments with additional recipients, // see the Payments API documentation on our [developer site] // (https://developer.squareup.com/docs/payments-api/overview). $money = new Money(); // Monetary amounts are specified in the smallest unit of the applicable currency. // This amount is in cents. It's also hard-coded for $1.00, which isn't very useful. $money->setAmount($total_price); $money->setCurrency('JPY'); //$money->setNote('test'); // Every payment you process with the SDK must have a unique idempotency key. // If you're unsure whether a particular payment succeeded, you can reattempt // it with the same idempotency key without worrying about double charging // the buyer. $create_payment_request = new CreatePaymentRequest($nonce, uniqid(), $money); // The SDK throws an exception if a Connect endpoint responds with anything besides // a 200-level HTTP code. This block catches any exceptions that occur from the request. try { $response = $payments_api->createPayment($create_payment_request); // If there was an error with the request we will // print them to the browser screen here if ($response->isError()) { echo 'Api response has Errors'; $errors = $response->getErrors(); echo '<ul>'; foreach ($errors as $error) { echo '<li>❌ ' . $error->getDetail() . '</li>'; } echo '</ul>'; exit(); } echo '<pre>'; //決済IDの取得 $resp_arr = $response->getBody(); $resp_dec = json_decode($resp_arr, true); $pmtid = $resp_dec["payment"] ["id"]; print($pmtid); echo '</pre>'; } catch (ApiException $e) { echo 'Caught exception!<br/>'; echo('<strong>Response body:</strong><br/>'); echo '<pre>'; var_dump($e->getResponseBody()); echo '</pre>'; echo '<br/><strong>Context:</strong><br/>'; echo '<pre>'; var_dump($e->getContext()); echo '</pre>'; exit(); }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。