wordpress外掛開發 簡易綠界API串接結合自己開發的外掛(進階版+不須另外建立頁面)
閱讀本篇前建議先閱讀
重點概念
1.新建一個class(要被外掛主檔引入)
2.在該支程式__construct()內做網址判斷
這邊假設我希望綠界回傳給我的網址是{Mywebsite_base_Url}/ecpayverify
我的判斷就會如下:
[php]if(strpos($_SERVER['REQUEST_URI'], '/ecpayverify/')===0||strpos($_SERVER['REQUEST_URI'], '/ecpayverify/')>0){
}[/php]
3.將要處理的邏輯寫在裡面,處理完後記得要"exit();"避免回傳給綠界資訊時產生多於的程式碼,以下附上範例
[php]<?php
if(!class_exists('onlineScheduleTrans')):
class onlineScheduleTrans{
public static $_instance=NULL;
function __construct(){
global $wpdb;
add_action('wp_ajax_nopriv_buyService', array($this, 'buyService'));
add_action('wp_ajax_buyService', array($this, 'buyService'));
if(strpos($_SERVER['REQUEST_URI'], '/ecpayverify/')===0||strpos($_SERVER['REQUEST_URI'], '/ecpayverify/')>0){
global $wpdb;
require_once onlineSchedule_DIR.'/SDK/ECPay.Payment.Integration.php';
define( 'ECPay_MerchantID', '3145753' );
define( 'ECPay_HashKey', 'kUMKO4xa8YHtgc1B' );
define( 'ECPay_HashIV', 'CSZqL9EXECkOMlUt' );
// 重新整理回傳參數。
$arParameters = $_POST;
foreach ($arParameters as $keys => $value) {
if ($keys != 'CheckMacValue') {
if ($keys == 'PaymentType') {
$value = str_replace('_CVS', '', $value);
$value = str_replace('_BARCODE', '', $value);
$value = str_replace('_CreditCard', '', $value);
}
if ($keys == 'PeriodType') {
$value = str_replace('Y', 'Year', $value);
$value = str_replace('M', 'Month', $value);
$value = str_replace('D', 'Day', $value);
}
$arFeedback[$keys] = $value;
}
}
// 計算出 CheckMacValue
$CheckMacValue = ECPay_CheckMacValue::generate( $arParameters, ECPay_HashKey, ECPay_HashIV );
// 必須要支付成功並且驗證碼正確
if ( $_POST['RtnCode'] =='1' && $CheckMacValue == $_POST['CheckMacValue'] ){
// 要處理的程式放在這裡,例如將線上服務啟用、更新訂單資料庫付款資訊等
$MerchantTradeNo=$_POST["MerchantTradeNo"];
$RtnCode=$_POST["RtnCode"];
$TradeAmt=$_POST["TradeAmt"];
$CheckMacValue=$_POST["CheckMacValue"];
//if success do something
}
else{
//if fail do something
}
// 接收到資訊回應綠界
echo '1|OK';
exit();
}
}
public static function instance(){
if(is_null(self::$_instance))self::$_instance=new self();
return self::$_instance;
}
}
endif;[/php]