質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Square

Squareは独自のカードリーダーを使ってスマートフォンやタブレットにクレジットカード決済機能を提供します。レジアプリはiOS/AndroidアプリやモバイルWebサイトとAPI連携できます。その他、Webサイト向けの決済APIや商品、顧客管理APIも提供しています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

3026閲覧

Squareを使用した決済。メモや商品名を決済に登録したい

mikeko0901

総合スコア227

Square

Squareは独自のカードリーダーを使ってスマートフォンやタブレットにクレジットカード決済機能を提供します。レジアプリはiOS/AndroidアプリやモバイルWebサイトとAPI連携できます。その他、Webサイト向けの決済APIや商品、顧客管理APIも提供しています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2020/10/16 08:13

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(); }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

できなさそうなので、閉じます

投稿2020/10/17 09:07

mikeko0901

総合スコア227

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問