読者です 読者をやめる 読者になる 読者になる

ノラプログラマーの技術メモ

ネットで調べても出てこなかった情報を載せていきたい技術系ブログ。

LINE PayをPHPで実装してみた

LinePayをPHPで実装したサンプルコード。最低限のAPIしか呼んでません。

<?php

if(!$_GET['transactionId']) {
  // ----------------------------------- 決済reserve
  $header = array(
    'Content-Type: application/json; charset=UTF-8',
    'X-LINE-ChannelId: 1466261954',
    'X-LINE-ChannelSecret: ed0e387bf251243d52c7c6a9a215cc26',
  );
  $postData = array(
    'productName' => "Test Item",
    'amount' => 800,
    'currency' => "JPY",
    'confirmUrl' => "https://hogehoge.com/linepay.php",
    'orderId' => "00001",
  );

  $ch = curl_init("https://api-pay.line.me/v2/payments/request");
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSLVERSION, 1);
  curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
  $rs = json_decode(curl_exec($ch), true);
  curl_close($ch);

  if($rs['returnCode'] == "0000") {
    header("Location: {$rs['info']['paymentUrl']['web']}");
  }
  else {
    echo "エラー番号:{$rs['returnCode']}";
  }
}
else {
  // ----------------------------------- 決済confirm
  $header = array(
    'Content-Type: application/json; charset=UTF-8',
    'X-LINE-ChannelId: 1466261954',
    'X-LINE-ChannelSecret: ed0e387bf251243d52c7c6a9a215cc26',
  );
  $postData = array(
    'amount' => 800,
    'currency' => "JPY",
  );

  $ch = curl_init("https://api-pay.line.me/v2/payments/{$_GET['transactionId']}/confirm");
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSLVERSION, 1);
  curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
  $rs = json_decode(curl_exec($ch), true);
  curl_close($ch);

  if($rs['returnCode'] == "0000") {
    echo "決済成功:{$rs['returnCode']}";
  }
  else {
    echo "エラー番号:{$rs['returnCode']}";
  }
}

?>

詳しくはコチラのサイトへどーぞ。
 ↓↓↓
kawaidesu.com