//訂單編號
$order_id = '20190119015948';
//測試環境
$status = 1;
//訂單金額
$amount = 888;
//付款方式 (信用卡)
$paytype ="CARD";
switch($status){
case 1: //測試
$token_url = 'sandbox-';
$accpwd = base64_encode("app_id:test_secret_code");
break;
case 2: //正式
$token_url = '';
$accpwd = base64_encode("app_id:secret_code");
break;
}
$headers = array('Content-Type:application/json','Authorization:Basic '.$accpwd);
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_URL,'https://'.$token_url.'api.pchomepay.com.tw/v1/token');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,null);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
$result=curl_exec($ch);
curl_close($ch);
$token = json_decode($result);
$pay_headers = array(
'Content-Type:application/json',
'pcpay-token:'.$token->token);
//傳給PChome資訊
//POST DATA
switch($paytype){
//信用卡
case 'CARD':
$go_url = 'https://'.$token_url.'api.pchomepay.com.tw/v1/payment';
$requestPayLoad='{
"order_id":"'.$order_id.'",
"pay_type":["CARD"],
"amount":'.$amount.',
//導回網址
"return_url":'',
"items":[{"name":品項名稱,"url":品項網址}],
"card_info":[{"installment":6}]
}';
break;
//虛擬帳號
case 'ATM':
$go_url = 'https://'.$token_url.'api.pchomepay.com.tw/v1/payment/atmva';
$requestPayLoad='{
//訂單編號
"order_id":"'.$order_id.'",
//訂單金額
"amount":'.$amount.',
//品項名稱
"item_name": "品項名稱",
//到期日
"expire_days":3
}';
break;
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,$pay_headers);
curl_setopt($ch,CURLOPT_URL,$go_url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,$requestPayLoad);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
$result=curl_exec($ch);
curl_close($ch);
//PChome回傳資訊
$response = json_decode($result);
switch($paytype){
//信用卡
case 'CARD':
//回傳格式
//{"order_id":"20190119015948","payment_url":"https:\/\/sandbox-secure.pchomepay.com.tw\/apipay\/ppwf?_pwfkey_=TDV2MkJTOWJZaEpGQTZqZlJXTnpseFJDRWtCS1VTMnc0QWg3amdBOWVPN1g2V2hYTkM3UEJobi10LHMwQlRZYx213"}
break;
//虛擬帳號
case 'ATM':
//回傳格式
//{"order_id":"20190119015723","virtual_account":"0702902241687648","bank_id":"011","expire_date":"20190122235959"}
//訂單編號
$order_id = $response->order_id;
//虛擬帳號
$virtual_account = $response->virtual_account;
//到期日
$expire_date = $response->expire_date;
break;
}
留言列表