ver.2サポートサイト

HOMEAPIサンプルプログラムPHP外部決済システム連携

外部決済システム連携

PHPを使用して、外部決済代行システムへクレジットカード情報を連携するサンプルプログラムです。

SPIRAL ver.2のアカウント内にクレジットカード情報を保存せず、外部決済代行システムが提供するAPIへ情報を送信し、決済を実行するモデルです。

外部決済代行システムへの連携ページでは、外部決済代行システムAPIの仕様に従ってください。

登録フォームの完了ページのソース

<h1>申し込み完了画面</h1>
<p>申込み番号は<span th:text="${record[1]}">申込管理ID</span>です。</p>
<p>引き続き、支払い入力画面へ進んでください。</p>
<form th:action="${pages['p02759'].path}" method="POST">
  <input type="hidden" name="orderid" th:value="${record[1]}">
  <button type="submit" name="btnsubmit">支払い入力へ進む</button>
</form>

カード番号入力ページのbody

<h1>支払い入力画面</h1>
<p>クレジットカード番号を入力してください。</p>
<p>※クレジットカード番号はSSL通信で暗号化され、カード決済代行会社へ認証のために送信されます。</p>
<p>※当社ではクレジットカード番号を保管しません。</p>
<form th:action="${pages['p02287'].path}" method="POST">
  カード番号:
  <input type="number" name="cardnumber" value=""><br>
  有効期限:20<input type="number" name="expire" value=""><br>
  数字4桁。2023年12月 → 2312
<br>
名義人:
<input type="text" name="holdername" value="">
<br>
  セキュリティコード:
  <input type="number" name="seccode" value=""><br>
  <input type="hidden" name="orderid" th:value="${postParams['orderid']}">
  <button type="submit" name="btnsubmit">入力する</button>
</form>

外部決済代行システムへの連携ページのPHP

<?php

//カード番号入力ページから送信されてきた値を変数にセット
$orderid = $SPIRAL->getParam("orderid");			//決済代行システムへ送信する一意の値
$cardnumber = $SPIRAL->getParam("cardnumber");	//カード番号
$expire = $SPIRAL->getParam("expire");			//有効期限
$holdername = $SPIRAL->getParam("holdername");	//名義人
$seccode = $SPIRAL->getParam("seccode");			//セキュリティコード
<code class="php syntaxhl">//オーダーIDを元に、支払い金額をDBから取得
$base_url = "https://api.spiral-platform.com/v1/apps";

$header = array(
"Authorization:Bearer xxxxxxxxxxxxx",
"Content-Type:application/json",
); 

//エンドポイント
$url =  $base_url . "/9999/dbs/1/records?where=@orderid='" . $orderid . "'&fields=amount";

//メソッド
$method = "GET";

$curl = curl_init();

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL , $url);
curl_setopt($curl, CURLOPT_HTTPHEADER , $header);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST , $method);
$response = curl_exec($curl);
if (curl_errno($curl)) echo curl_error($curl);
curl_close($curl);

//json形式を配列に変換
$data = json_decode($response , true);

$amount = $data["items"][0]["amount"];//金額

//決済代行システムのAPI仕様に従う
$url = "https://";	//決済代行システムの取引登録APIのエンドポイントURL
$header = array("Content-Type: application/json;charset=UTF-8");
$body = [
    'apiKey'       => 'xxxxxx',
    'apiPass'     => 'xxxxxx',
    'orderID'      => $orderid,
    'amount'       => $amount
];
//配列をjson形式に変換
$json_body = json_encode($body);

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL , $url);
curl_setopt($curl, CURLOPT_HTTPHEADER , $header);
curl_setopt($curl, CURLOPT_POSTFIELDS , $json_body);
curl_setopt($curl, CURLOPT_POST , true);
$response = curl_exec( $curl );
$curlinfo = curl_getinfo( $curl );
curl_close( $curl );

// レスポンスチェック
if( $curlinfo[ 'http_code' ] != 200 ){
    // エラー
    return false;
}

// レスポンスのエラーチェック
$data = json_decode($response , true);
if( array_key_exists( 'errCode', $data ) ){
    // エラー
    return false;
}

//決済代行システムのAPIから返却された値を取得
$accessID = $data["accessID"];
$accessPass = $data["accessPass"];
$orderID = $data["orderID"];

$tranurl = "https://";	//決済代行システムの決済実行APIのエンドポイントURL
$param = [
    'accessID'        => $accessID,
    'accessPass'      => $accessPass,
    'orderID'         => $orderID,
    'method'          => '1',
    'cardNo'          => $cardnumber,
    'expire'          => $expire,
    'holderName'          => $holdername,
    'securityCode'          => $seccode,
];

//配列をjson形式に変換
$json_param = json_encode($param);

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL , $tranurl);
curl_setopt($curl, CURLOPT_HTTPHEADER , $header);
curl_setopt($curl, CURLOPT_POSTFIELDS , $json_param);
curl_setopt($curl, CURLOPT_POST , true);
$response = curl_exec( $curl );
$curlinfo = curl_getinfo( $curl );
curl_close( $curl );

// レスポンスチェック
if( $curlinfo[ 'http_code' ] != 200 ){
    // エラー
    return false;
}

// レスポンスのエラーチェック
$data = json_decode($response , true);
if( array_key_exists( 'errCode', $data ) ){
    // エラー
foreach($data as $d){
  $msg = "エラーが発生しました。:" . $da["errCode"];
}
    return false;
}else{
  $msg = "お支払いが完了しました。";
  return true;
}
$SPIRAL->setTHValue("output", $msg);
?>

外部決済代行システムへの連携ページのbody

<div th:if="${cp.result.isSuccess}">
    <p th:text="${cp.result.value['output']}"></p>
</div>

<div th:if="${!cp.result.isSuccess}">
    <p th:text="${cp.result.errorMessage}">error message</p>
</div>