LoginSignup
19
18

More than 5 years have passed since last update.

PHPでLINE Payを実装してみた(サンプルコードあり)

Posted at

自社サイトがPHPだったので、LinePayをPHPで導入してみました。
必要最低限のAPIしか叩いてません。サンプルコードは以下の通り。

linepay.php
<?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']}";
  }
}

?>

line.png

詳しくは、「LINE PayのAPIをPHPで実装してみた」へどうぞ!

19
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18