fork download
  1. <?php
  2.  
  3. function encrypt($key, $data) {
  4. return base64_encode(hash_hmac('sha256', $data, $key, true));
  5. }
  6.  
  7. function createLinePayOrder($amount, $currency, $orderId) {
  8. $channelSecret = 'a917ab6a2367b536f8e5a6e2977e06f4';
  9. $channelId = 'YOUR_CHANNEL_ID'; // Replace with your actual channel ID
  10. $requestUri = '/v3/payments/request';
  11. $nonce = uniqid('', true);
  12.  
  13. $form = [
  14. 'amount' => $amount,
  15. 'currency' => $currency,
  16. 'orderId' => $orderId,
  17. 'packages' => [
  18. [
  19. 'id' => 'package_id', // Replace with your package ID
  20. 'name' => 'shop_name', // Replace with your shop name
  21. 'amount' => $amount,
  22. 'products' => [
  23. [
  24. 'id' => 'product_id', // Replace with your product ID
  25. 'name' => 'product_name', // Replace with your product name
  26. 'quantity' => 10,
  27. 'price' => 10
  28. ]
  29. ]
  30. ]
  31. ],
  32. 'redirectUrls' => [
  33. 'confirmUrl' => 'https://y...content-available-to-author-only...n.com/confirm', // Replace with your confirmation URL
  34. ]
  35. ];
  36.  
  37. // Convert form data to JSON
  38. $jsonForm = json_encode($form);
  39.  
  40. // Create the signature
  41. $signature = encrypt($channelSecret, $channelSecret . $requestUri . $jsonForm . $nonce);
  42.  
  43. // Prepare the request headers
  44. $headers = [
  45. 'Content-Type: application/json',
  46. 'X-LINE-ChannelId: ' . $channelId,
  47. 'X-LINE-TransactionId: ' . $nonce,
  48. 'X-LINE-Signature: ' . $signature
  49. ];
  50.  
  51. // Send the POST request to Line Pay API
  52. $ch = curl_init();
  53. curl_setopt($ch, CURLOPT_URL, 'https://s...content-available-to-author-only...e.me' . $requestUri);
  54. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  55. curl_setopt($ch, CURLOPT_POST, true);
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonForm);
  57. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  58.  
  59. // Execute the request
  60. $response = curl_exec($ch);
  61.  
  62. // Check for errors
  63. if ($response === false) {
  64. echo 'Curl error: ' . curl_error($ch);
  65. return;
  66. }
  67.  
  68. // Close cURL session
  69. curl_close($ch);
  70.  
  71. // Decode the JSON response
  72. $responseData = json_decode($response, true);
  73.  
  74. return $responseData;
  75. }
  76.  
  77. // Example usage:
  78. $orderDetails = createLinePayOrder(100, 'JPY', 'merchant_order_id');
  79. echo 'Response: <pre>' . print_r($orderDetails, true) . '</pre>';
  80.  
  81. ?>
  82.  
Success #stdin #stdout 0.03s 26188KB
stdin
Standard input is empty
stdout
Curl error: Could not resolve host: sandbox-api-pay.line.meResponse: <pre></pre>