18 Mar

การเชื่อต่อ Payment Gateway 2C2P โดยใช้ PHP


บทความการเขียน php เชื่อมต่อ Payment Gateway ขอ 2C2P เพื่อ ทำระบบชำระเงินผ่านบัตรเครดิต

เริ่มขั้นตอนหลังจากเราไปขอสมัครใช้บริการของ 2C2P ทางเราจะได้รับ ข้อมูล
1. user และ password เพื่อ login เข้าไป Dashboard เพื่อเอา SecretKey from 2C2P
2. Merchant ID ของเรา
3. วิธีการติดตั้งและใช้งาน

หลังจากได้ข้อมูล SecretKey และ Merchant ID มาแล้ว เราจะทำการสร้างแบบฟอร์ม เพื่อทำการเชื่อต่อ (ผมจะใส่ Comment ใน Code เพื่อ อธิบายการทำงาน)

<?php 
 
  //ข้อมูลทั่วไปของ  Transaction
	$order_id = "00000001";
  $payment_description  =  " Product Itme 0001 x 10 "; // รายละเอียดของสินค้า
  $amount  = str_pad(5000, 10, '0', STR_PAD_LEFT)."00";  // จำนวนเงิน ต้องททำเป็นเลข 10 หลัก
  $currency = "764";  // สกุลเงิน ไทย ถ้าอยากเปลี่ยนสกุลเงินดูได้ใน Doc ของ 2C2P
  $result_url_1 = "https://www.test/result.php";     // url สำหรับ redirect กลับมาที่เว็บเราเพื่อเช็คการชำระเงิน
  
  //Merchant's account information
	$merchant_id = "7xxxxxxxxxxxx";			// Get MerchantID when 
	$secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxD";	//Get SecretKey from 2C2P PGW Dashboard
	  
	//Request information
	$version = "8.5";
	
  // version demo or implement
  $payment_url = "https://demo2.2c2p.com/2C2PFrontEnd/RedirectV3/payment";  // version สำหรับทำสอบชำระเงิน  โดยปรกติต้องทดสอบถึงจะแจ้งทาง 2C2P เพื่อเปิดใช้จริง
	//$payment_url = "https://t.2c2p.com/RedirectV3/payment"; // version ใช้จริง
  
  //Construct signature string  hash ข้อมูลทั้งหมด
	$params = $version.$merchant_id.$payment_description.$order_id.$currency.$amount.$result_url_1;
	$hash_value = hash_hmac('sha256',$params, $secret_key,false);	//Compute hash value
	

      // สร้างแบบฟอร์มที่จะส่งค่า
      $formdata = '  

	   <form id="payform" method="post" action="'.$payment_url.'">
		<input type="hidden" name="version" value="'.$version.'"/>
		<input type="hidden" name="merchant_id" value="'.$merchant_id.'"/>
		<input type="hidden" name="currency" value="'.$currency.'"/>
		<input type="hidden" name="result_url_1" value="'.$result_url_1.'"/>
		<input type="hidden" name="hash_value" value="'.$hash_value.'"/>
    PRODUCT INFO : <input type="text" name="payment_description" value="'.$payment_description.'"  readonly/><br/>
		ORDER NO : <input type="text" name="order_id" value="'.$order_id.'"  readonly/><br/>
		AMOUNT: <input type="text" name="amount" value="'.$amount.'" readonly/><br/>
		<input type="submit" id="submitx" name="submit" value="Confirm" />
	  </form>  
     


	';  
  
    echo  $formdata;

?>

จะได้ออกมาเป็นหน้าจอประมาณนี้ครับ

เมื่อ Click Submit ก็จะไปในหน้าชำระเงินของ 2C2P ดังรูป

หลังจากมีการชำระเงินแล้วระบบ 2C2P จะ redirect กลับมาที่เว็บเราที่ใส่ไว้ในฟอร์ม ซื่องผมใช้ชื่อ result.php
2C2P จะส่งค่า Status ต่างๆกลับมาให้เราในรูปแบบ POST

เราใช้คำสั่ง  print_r($_POST); //ในไฟล์ result.php เพื่อ ดูว่า Status เป็นอย่างไร

ค่าที่ได้จะออกมาประมาณรูป

ซึ่งค่าที่บอกว่าผ่านไม่่ผ่านคือ payment_status จะต้องเท่ากับ 000 จึงจะเท่ากับชำระเงินสำเร็จ
ส่วนค่าที่ Return กลับมาอื่นๆก็แล้วแต่การปรับใช้ให้เหมาะสมกับเว็บของตัวเองครัับ

Leave a Reply

Your email address will not be published. Required fields are marked *