Created
May 28, 2021 00:46
-
-
Save ucheng/c7c7a9df4b5700098166361964d808a8 to your computer and use it in GitHub Desktop.
EDD x ECPay Payment Gateway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// registers the gateway | |
function pw_edd_register_gateway($gateways) { | |
$gateways['ecpay_credit'] = array( 'admin_label' => '綠界-信用卡', 'checkout_label' => __('綠界-信用卡', 'your_textdomain')); | |
return $gateways; | |
} | |
add_filter('edd_payment_gateways', 'pw_edd_register_gateway'); | |
// process the payment | |
add_action( 'edd_gateway_ecpay_credit', 'edd_process_ecpay_credit_purchase' ); | |
function edd_process_ecpay_credit_purchase( $purchase_data ) { | |
if( ! wp_verify_nonce( $purchase_data['gateway_nonce'], 'edd-gateway' ) ) { | |
wp_die( __( 'Nonce verification has failed', 'easy-digital-downloads' ), __( 'Error', 'easy-digital-downloads' ), array( 'response' => 403 ) ); | |
} | |
$purchase_summary = edd_get_purchase_summary($purchase_data); | |
/********************************** | |
* setup the payment details | |
**********************************/ | |
$payment_data = array( | |
'price' => $purchase_data['price'], | |
'date' => $purchase_data['date'], | |
'user_email' => $purchase_data['user_email'], | |
'purchase_key' => $purchase_data['purchase_key'], | |
'currency' => $edd_options['currency'], | |
'downloads' => $purchase_data['downloads'], | |
'cart_details' => $purchase_data['cart_details'], | |
'user_info' => $purchase_data['user_info'], | |
'status' => 'pending', | |
'gateway' => 'ecpay_credit' | |
); | |
// record the pending payment | |
$payment_id = edd_insert_payment($payment_data); | |
if ( ! $payment_id ) { | |
// Record the error | |
edd_record_gateway_error( __( 'Payment Error', 'easy-digital-downloads' ), sprintf( __( 'Payment creation failed before sending buyer to ECPay. Payment data: %s', 'easy-digital-downloads' ), json_encode( $payment_data ) ), $payment_id ); | |
// Problems? send back | |
edd_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['edd-gateway'] ); | |
} else { | |
// if errors are present, send the user back to the purchase page so they can be corrected | |
$date = date( 'Y/m/d H:i:s', current_time( 'timestamp' ) ); | |
$listener_url = trailingslashit( home_url() ).'?edd-listener=ecpay_credit'; | |
//prepare data | |
$data = array( | |
'MerchantID' => '2000132', | |
'MerchantTradeNo' => $payment_id.rand(100, 999), //商店訂單編號,後三碼為流水號,訂單編號重複 | |
'MerchantTradeDate' => $date, | |
'PaymentType' => 'aio', | |
'TotalAmount' => $payment_data['price'], | |
'TradeDesc' => 'your item desc', | |
'ItemName' => 'your item name', | |
'ReturnURL' => $listener_url,//背景通知的網址 | |
'OrderResultURL' => edd_get_success_page_uri(),//付款成功後導向這個網址,類似 woo get_return_url() | |
'ChoosePayment' => 'Credit', | |
'EncryptType' => 1 | |
); | |
$mac = GenerateCheckMacValue( $data ); | |
$data['CheckMacValue'] = $mac; | |
$ServiceURL = 'https://payment-stage.ecpay.com.tw/Cashier/AioCheckOut/V5'; | |
//生成表單,自動送出 | |
$szHtml = '<!DOCTYPE html>'; | |
$szHtml .= '<html>'; | |
$szHtml .= '<head>'; | |
$szHtml .= '<meta charset="utf-8">'; | |
$szHtml .= '</head>'; | |
$szHtml .= '<body>'; | |
$szHtml .= "<form id=\"__ecpayForm\" method=\"post\" target=\"_self\" action=\"{$ServiceURL}\">"; | |
foreach ($data as $keys => $value) { | |
$szHtml .= "<input type=\"hidden\" name=\"{$keys}\" value='{$value}' />"; | |
} | |
$szHtml .= '</form>'; | |
$szHtml .= '<script type="text/javascript">document.getElementById("__ecpayForm").submit();</script>'; | |
$szHtml .= '</body>'; | |
$szHtml .= '</html>'; | |
echo $szHtml ; | |
} | |
} | |
/** | |
* 產生檢查碼 | |
* 傳入 $arParameters 各參數 | |
* 傳出 $sMacValue 檢查碼 | |
*/ | |
function GenerateCheckMacValue($arParameters) | |
{ | |
$sMacValue = '' ; | |
if(isset($arParameters)) | |
{ | |
// 資料排序 | |
// php 5.3以下不支援 | |
// ksort($arParameters, SORT_NATURAL | SORT_FLAG_CASE); | |
uksort($arParameters, 'strcasecmp'); | |
// 開始組合字串 | |
$sMacValue = 'HashKey=5294y06JbISpM5x9' ; | |
foreach($arParameters as $key => $value) | |
{ | |
$sMacValue .= '&' . $key . '=' . $value ; | |
} | |
$sMacValue .= '&HashIV=v77hoKGq4kWxNNIS' ; | |
// URL Encode編碼 | |
$sMacValue = urlencode($sMacValue); | |
// 轉成小寫 | |
$sMacValue = strtolower($sMacValue); | |
// 取代為與 dotNet 相符的字元 | |
$sMacValue = str_replace('%2d', '-', $sMacValue); | |
$sMacValue = str_replace('%5f', '_', $sMacValue); | |
$sMacValue = str_replace('%2e', '.', $sMacValue); | |
$sMacValue = str_replace('%21', '!', $sMacValue); | |
$sMacValue = str_replace('%2a', '*', $sMacValue); | |
$sMacValue = str_replace('%28', '(', $sMacValue); | |
$sMacValue = str_replace('%29', ')', $sMacValue); | |
// MD5編碼 | |
$sMacValue = hash('sha256', $sMacValue, false); | |
$sMacValue = strtoupper($sMacValue); | |
} | |
return $sMacValue ; | |
} | |
add_action( 'init', 'process_ecpay_credit' ); | |
function process_ecpay_credit() { | |
if ( ! isset( $_GET['edd-listener'] ) || $_GET['edd-listener'] !== 'ecpay_credit' ) { | |
return; | |
} | |
$resp = $_REQUEST; | |
if (array_key_exists('RtnCode', $resp)) { | |
if ($resp['RtnCode'] == '1') { | |
$ecpay_payment_id = $resp['MerchantTradeNo']; | |
$payment_id = parse_ecpay_order_no_to_edd_payment_id($ecpay_payment_id); | |
edd_update_payment_status($payment_id, 'complete'); | |
edd_get_purchase_session(); | |
edd_empty_cart(); | |
} else { | |
//handle the error | |
} | |
} | |
} | |
function parse_ecpay_order_no_to_edd_payment_id( $ecpay_order_no ) { | |
$real_edd_payment_id = substr($ecpay_order_no, 0, -3); | |
return $real_edd_payment_id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment