Ecpay 綠界發票 php 程式碼筆記

Mario tsai
17 min readOct 27, 2023

綠界的開發票功能 發票操作

https://developers.ecpay.com.tw/?p=7809

開出發票

=>重要回傳值 InvoiceNo InvoiceDate

    public function issue_b_to_c_invoice()
{
log_message_debug('Ecpayinvoice AlbertBaseController issue_b_to_c_invoice-> ');

$postData = $this->get_posts(
array(
'RelateNumber',
'CustomerIdentifier',
'CustomerName',
'CustomerAddr',
'CustomerPhone',
'CustomerEmail',
'ClearanceMark',
'Donation',
'LoveCode',
'CarrierType',
'CarrierNum',
'SalesAmount',
'InvoiceRemark',
'Items',
'InvoiceRemark',
'vat'
)
);
// 可以依照自己需求帶入下欄
$MerchantID = env('ECPAY_Merchant_ID');
$ec_hash_id = env('ECPAY_INVOICE_HASH_ID');
$ec_hash_iv = env('ECPAY_INVOICE_HASH_IV');
$factory = new Factory([
'hashKey' => $ec_hash_id,
'hashIv' => $ec_hash_iv,
]);
$postService = $factory->create('PostWithAesJsonResponseService');

$itemCount = 3;
$itemPrice = 10;
$itemAmount = ($itemPrice * $itemCount);
$saleAmount = $itemAmount;
$taxAmount = round(($saleAmount * 0.05), 0);
$data = [
'MerchantID' => $MerchantID,
'RelateNumber' => 'maylife_test_' . time(), //特店自訂編號 需為唯一值不可重複使用
"CustomerID" => "1",//通關方式註記
'CustomerIdentifier' => '23165448', //買方統編
'CustomerName' => '23165448', //買方統編
"CustomerAddr" => "1",//通關方式註記
"CustomerPhone" => "1",//通關方式註記
// "CustomerEmail" => "1",
"ClearanceMark" => "1",//通關方式註記
"Print" => "1",//通關方式註記 0:不列印 1:要列印
"Donation" => "0",//捐贈註記 0:不捐贈 1:要捐贈 當統一編號[CustomerIdentifier]有值時,此參數請帶0
"LoveCode" => "",//捐贈碼//捐贈碼 當捐贈註記[Donation]=1(要捐贈)時,為必填。
"CarrierType" => "", // 空字串:無載具 1:綠界電子發票載具 2:自然人憑證號碼 3:手機條碼載具
"CarrierNum" => "",// 當[CarrierType]=”” 時,請帶空字串。 當[CarrierType]=1 請帶空字串,系統會自動帶入值, CarrierType]=2 請帶固定長度為16且格式為2碼大寫英文字母加上14碼數字 CarrierType]=3 請帶固定長度為8碼字元,第1碼為【/】; 其餘7碼則由數字【0-9】、大寫英文【A-Z】與特殊符號【+】【-】【.】這39個字元組成的編號
'InvType' => '07',
'TaxType' => '1', // 當字軌類別[InvType]為07時,則此欄位請填入1、2、3或9 字軌類別[InvType]為08時,則此欄位請填入3或4 => 1:應稅。 2:零稅率。 3:免稅。 4:應稅(特種稅率
'SalesAmount' => $saleAmount, //帶整數,不可有小數點。 ItemAmount 所有商品的ItemAmount加總後四捨五入=SalesAmount(含稅)
"InvoiceRemark" => "發票備註",
'vat' => '1', //商品單價是否含稅 1:含稅 0:未稅
'Items' => [
[
'ItemSeq' => 1,
'ItemName' => '測試商品01',
'ItemCount' => $itemCount,
'ItemWord' => '件',
'ItemPrice' => $itemPrice,
'ItemTaxType' => '1',
'ItemAmount' => $itemAmount,
'ItemRemark' => 'aaa',
],
],

];
$input = [
'MerchantID' => $MerchantID,
'RqHeader' => [
'Timestamp' => time(),
],
'Data' => $data,
];
$url = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/Issue';
$response = $postService->post($input, $url);
record_log(
$this->session->get('login_user')->lu_login,
'issue_b_to_b_invoice',
'insert',
"" . json_encode($response),
'',
$this->request->getIPAddress()
);
//{
// "MerchantID": "3002607",
// "RpHeader": {
// "Timestamp": 1234564848
// },
// "TransCode": 1,
// "TransMsg": "Success",
// "Data": {
// "RtnCode": 1,
// "RtnMsg": "開立發票成功",
// "InvoiceNo": "UV11100012",
// "InvoiceDate": "2019-09-17 17:17:31",
// "RandomNumber": "6866"
// }
//}
$res['res'] = $response;
$res['success'] = TRUE;
return $this->respond($res);
}

延遲開出發票

=>重要帶入值Tsr 重要回傳值 OrderNumber

public function delay_issue_b_to_c_invoice()
{
$postData = $this->get_posts(
array(
'RelateNumber',
'CustomerIdentifier',
'CustomerName',
'CustomerAddr',
'CustomerPhone',
'CustomerEmail',
'ClearanceMark',
'Donation',
'LoveCode',
'CarrierType',
'CarrierNum',
'SalesAmount',
'InvoiceRemark',
'Items',
'InvoiceRemark',
'vat',
'DelayFlag',
'DelayDay',
'Tsr',
'PayType',
'PayAct',
'NotifyURL',
)
);


$MerchantID = env('ECPAY_Merchant_ID');
$ec_hash_id = env('ECPAY_INVOICE_HASH_ID');
$ec_hash_iv = env('ECPAY_INVOICE_HASH_IV');
$factory = new Factory([
'hashKey' => $ec_hash_id,
'hashIv' => $ec_hash_iv,
]);
$postService = $factory->create('PostWithAesJsonResponseService');

$itemCount = 3;
$itemPrice = 10;
$itemAmount = ($itemPrice * $itemCount);
$saleAmount = $itemAmount;
$taxAmount = round(($saleAmount * 0.05), 0);
$data = [
'MerchantID' => $MerchantID,
'RelateNumber' => 'maylife_test_' . time(), //特店自訂編號 需為唯一值不可重複使用
"CustomerID" => "1123212",//通關方式註記
'CustomerIdentifier' => '23165448', //買方統編
'CustomerName' => '23165448', //買方統編
"CustomerAddr" => "1",//通關方式註記
"CustomerPhone" => "1",//通關方式註記
// "CustomerEmail" => "1",
"ClearanceMark" => "1",//通關方式註記
"Print" => "1",//通關方式註記 0:不列印 1:要列印
"Donation" => "0",//捐贈註記 0:不捐贈 1:要捐贈 當統一編號[CustomerIdentifier]有值時,此參數請帶0
"LoveCode" => "",//捐贈碼//捐贈碼 當捐贈註記[Donation]=1(要捐贈)時,為必填。
"CarrierType" => "", // 空字串:無載具 1:綠界電子發票載具 2:自然人憑證號碼 3:手機條碼載具
"CarrierNum" => "",// 當[CarrierType]=”” 時,請帶空字串。 當[CarrierType]=1 請帶空字串,系統會自動帶入值, CarrierType]=2 請帶固定長度為16且格式為2碼大寫英文字母加上14碼數字 CarrierType]=3 請帶固定長度為8碼字元,第1碼為【/】; 其餘7碼則由數字【0-9】、大寫英文【A-Z】與特殊符號【+】【-】【.】這39個字元組成的編號
'InvType' => '07',
'TaxType' => '1', // 當字軌類別[InvType]為07時,則此欄位請填入1、2、3或9 字軌類別[InvType]為08時,則此欄位請填入3或4 => 1:應稅。 2:零稅率。 3:免稅。 4:應稅(特種稅率
'SalesAmount' => $saleAmount, //帶整數,不可有小數點。 ItemAmount 所有商品的ItemAmount加總後四捨五入=SalesAmount(含稅)
"InvoiceRemark" => "發票備註",
'vat' => '1', //商品單價是否含稅 1:含稅 0:未稅
"DelayFlag" => "1", //1:延遲開立 DelayFlag=1(延遲) DelayDay=7(天數) 此為7天後自動開立, DelayFlag = 2(觸發) DelayDay=2(天數) 此為被觸發後過2天才會開立,若此張發票都沒有被觸發,將不會被開立
"DelayDay" => 5,
"Tsr" => 'maylife_test_' . time(), //用來呼叫付款完成觸發或延遲開立發票API的依據。 均為唯一值不可重覆使用。
"PayType" => "2",
"PayAct" => "ECPAY",
"NotifyURL" => "test@ecpay.com.tw", //使用測試環境時,不提供NotifyURL開立通知 到開立成功結果通知後,請正確回應 1|OK 給綠界。
'Items' => [
[
'ItemSeq' => 1,
'ItemName' => '測試商品01',
'ItemCount' => $itemCount,
'ItemWord' => '件',
'ItemPrice' => $itemPrice,
'ItemTaxType' => '1',
'ItemAmount' => $itemAmount,
'ItemRemark' => 'aaa',
],
],

];
$input = [
'MerchantID' => $MerchantID,
'RqHeader' => [
'Timestamp' => time(),
],
'Data' => $data,
];
$url = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/DelayIssue';
$response = $postService->post($input, $url);
record_log(
$this->session->get('login_user')->lu_login,
'delay_issue_b_to_b_invoice',
'insert',
"" . json_encode($response),
'',
$this->request->getIPAddress()
);
//{
// "MerchantID": "3002607",
// "RpHeader": {
// "Timestamp": 1234564848
// },
// "TransCode": 1,
// "TransMsg": "Success",
//
// "Data": {
// "RtnCode": 1,
// "RtnMsg": "開立發票成功",
// "OrderNumber": "1231000000",
// }
//}
$res['res'] = $response;
$res['success'] = TRUE;
return $this->respond($res);
}

觸發發票

=>重要帶入值 Tsr

public function trigger_b_to_c_invoice()
{
// $postData = $this->get_posts(
// array(
// 'Tsr',
// )
// );

$MerchantID = env('ECPAY_Merchant_ID');
$ec_hash_id = env('ECPAY_INVOICE_HASH_ID');
$ec_hash_iv = env('ECPAY_INVOICE_HASH_IV');
$factory = new Factory([
'hashKey' => $ec_hash_id,
'hashIv' => $ec_hash_iv,
]);
$postService = $factory->create('PostWithAesJsonResponseService');

$data = [
'MerchantID' => $MerchantID,
'Tsr' => 'maylife_test_1698400760',
"PayType"=> "2"
];
$input = [
'MerchantID' => $MerchantID,
'RqHeader' => [
'Timestamp' => time(),
],
'Data' => $data,
];
$url = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/TriggerIssue';
$response = $postService->post($input, $url);
//{
// "MerchantID": "3002607",
// "RpHeader": {
// "Timestamp": 1234564848
// },
// "TransCode": 1,
// "TransMsg": "Success",
// "Data": {
// "RtnCode": 1,
// "RtnMsg": "延後開立成功",
// "Tsr": "201909170001"
// }
//}
$res['res'] = $response;
$res['success'] = TRUE;
return $this->respond($res);
}

折讓發票

public function allowance_b_to_b_invoice()
{
$postData = $this->get_posts(
array(
'InvoiceNo',
'InvoiceDate',
'CustomerName',
'NotifyMail',
'NotifyPhone',
'items'
)
);
$MerchantID = env('ECPAY_Merchant_ID');
$ec_hash_id = env('ECPAY_INVOICE_HASH_ID');
$ec_hash_iv = env('ECPAY_INVOICE_HASH_IV');
$factory = new Factory([
'hashKey' => $ec_hash_id,
'hashIv' => $ec_hash_iv,
]);
$postService = $factory->create('PostWithAesJsonResponseService');


$itemCount = 3;
$itemPrice = 10;
$itemAmount = ($itemPrice * $itemCount);
$saleAmount = $itemAmount;
$taxAmount = round(($saleAmount * 0.05), 0);
$data = [
'MerchantID' => $MerchantID,
'InvoiceNo' => 'IU70231312', //特店自訂編號 需為唯一值不可重複使用
"InvoiceDate" => "2023-10-27",//通關方式註記
'AllowanceNotify' => 'S', // 開立折讓後,寄送將相關發票折讓資訊通知消費者 S:簡訊 E:電子郵件 A 皆通知時 N:皆不通知
'CustomerName' => '23165448', //買方統編
"NotifyPhone" => "0982222222",//通關方式註記
"NotifyMail" => "xxxx@gmail.com",//通關方式註記 折讓單總金額(含稅
'AllowanceAmount' => 30 ,//折讓單總金額(含稅 int
'Items' => [
[
'ItemSeq' => 1,
'ItemName' => '測試商品01',
'ItemCount' => $itemCount,
'ItemWord' => '件',
'ItemPrice' => $itemPrice,
'ItemTaxType' => '1',
'ItemAmount' => $itemAmount,
'ItemRemark' => 'aaa',
],
],

];
$input = [
'MerchantID' => $MerchantID,
'RqHeader' => [
'Timestamp' => time(),
],
'Data' => $data,
];
$url = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/Allowance';
$response = $postService->post($input, $url);
//{
// "MerchantID": "3002607",
// "RpHeader": {
// "Timestamp": 1234564848
// },
// "TransCode": 1,
// "TransMsg": "Success",
// "Data": {
// "RtnCode": 1,
// "RtnMsg": "開立發票成功",
// "InvoiceNo": "UV11100012",
// "InvoiceDate": "2019-09-17 17:17:31",
// "RandomNumber": "6866"
// }
//}
$res['res'] = $response;
$res['success'] = TRUE;
return $this->respond($res);
}

--

--

Mario tsai

Major in physical education. Football referee. And high interest in looking the world through the numbers.