<?php
/**
* 經銷商串接-特約商店申請
*/
final class AgentApplicant
{
/**
* 經銷商商務代號
* @var string
*/
public $agentUid = "A1234567891002";
/**
* 經銷商金鑰或認證碼
* @var string
*/
public $agentKey = "nq8KL8qNNCW12A3TBfAp4lU0y3PED8IQ";
/**
* 串接交易位置
* @var string
*/
public $url = "https://pay.usecase.cc/api/agent";
/**
* 取得串接欄位資料
* @return array
*/
public function getRawData()
{
$rawData = array();
$rawData['agent_uid'] = $this->agentUid;
$rawData['type'] = 0;
// 基本資料
$detail = [];
$detail['project_id'] = "1";
$detail['paymodes'] = [1, 9];
$detail['is_agent_einvoice'] = 1;
$detail['background_return_url'] = "https://query.usecase.cc/receive.php";
$detail['allot_bank_country'] = 1;
$detail['allot_bank_name'] = 23;
$detail['allot_bank_ext'] = 1872;
$detail['allot_bank_idname'] = "金城武";
$detail['allot_bank_id'] = "12330001144";
$detail['allot_bank_currency'] = "NTD";
$detail['service_mail'] = "[email protected]";
$detail['service_phone_type'] = 1;
$detail['service_phone_code'] = "886";
$detail['service_phone'] = "02-88888888";
// 範例用檔案
$sampleFile['size'] = 273;;
$sampleFile['type'] = "image/png";;
$sampleFile['data'] = "iVBORw0KGgoAAAANSUhEUgAAADMAAAASCAIAAAB5M3HuAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACmSURBVEhLY/hPe3Dv+1O/ey2LX+96//sTVIgIQD+XQRDx7qO3y4h338C4DILwu28gXQZBuNzHANRGa3To4wU012AiTPcxoKkYWITsvsHlMgiCuG8wugyCBqnLCh5NH3QuA7rpzOfrgyudwd0EAYOi1EBzEwQMcEkb/6Bvx/uTUHWoYMBcBnHT978/oYowwAC4jKCbIICuLiPSTRBAJ5eR5CYQ+P8fAPhjvViKbEtMAAAAAElFTkSuQmCC";
// 個人
$detail['zh_tw_name'] = "君の名は";
$detail['en_us_name'] = "Your Name";
$detail['applicant_name'] = "金城武";
$detail['applicant_id_type'] = 1;
$detail['applicant_id'] = "M123456789";
$detail['applicant_birthday'] = "20000131";
$detail['applicant_zip_code'] = '432';
$detail['applicant_address'] = "大肚里沙田路二段199號";
$detail['applicant_phone_code'] = "886";
$detail['applicant_phone'] = "04-26888888";
$detail['applicant_cellphone_code'] = '886';
$detail['applicant_cellphone'] = "0924-999999";
$detail['applicant_email'] = "[email protected]";
$detail['mcc_code'] = "1711";
$detail['real_zip_code'] = "407";
$detail['real_address'] = "市政北二路236號26樓之1";
$principal_id_front_img['name'] = "身分證正面.png";
$principal_id_front_img['size'] = $sampleFile['size'];
$principal_id_front_img['type'] = $sampleFile['type'];
$principal_id_front_img['data'] = $sampleFile['data'];
$principal_id_obverse_img['name'] = "身分證反面.png";
$principal_id_obverse_img['size'] = $sampleFile['size'];
$principal_id_obverse_img['type'] = $sampleFile['type'];
$principal_id_obverse_img['data'] = $sampleFile['data'];
$principal_driverlicense_healthidcard_img['name'] = "申請人第二證件.png";
$principal_driverlicense_healthidcard_img['size'] = $sampleFile['size'];
$principal_driverlicense_healthidcard_img['type'] = $sampleFile['type'];
$principal_driverlicense_healthidcard_img['data'] = $sampleFile['data'];
$business_photos_img_1 = $sampleFile;
$business_photos_img_1['name'] = "營業照片第一張.png";
$business_photos_img_2 = $sampleFile;
$business_photos_img_2['name'] = "營業照片第二張.png";
$business_photos_img_3 = $sampleFile;
$business_photos_img_3['name'] = "營業照片第三張.png";
$business_photos_img_4 = $sampleFile;
$business_photos_img_4['name'] = "營業照片第四張.png";
$business_photos_4_img[] = $business_photos_img_1;
$business_photos_4_img[] = $business_photos_img_2;
$business_photos_4_img[] = $business_photos_img_3;
$business_photos_4_img[] = $business_photos_img_4;
$bank_passbook_img['name'] = '銀行存摺封面影本.png';
$bank_passbook_img['size'] = $sampleFile['size'];
$bank_passbook_img['type'] = $sampleFile['type'];
$bank_passbook_img['data'] = $sampleFile['data'];
$detail['files'] = ['principal_id_front_img' => $principal_id_front_img,
'principal_id_obverse_img' => $principal_id_obverse_img,
'principal_driverlicense_healthidcard_img' => $principal_driverlicense_healthidcard_img,
'business_photos_4_img' => $business_photos_4_img,
'bank_passbook_img' => $bank_passbook_img];
/*
// 公司
$detail['org_type_uid'] = 2;
$detail['tong_bian'] = "28915188";
$detail['id_name'] = "高鉅科技股份有限公司";
$detail['setup_date'] = "20081001";
$detail['mcc_code'] = "1711";
$detail['capital_currency'] = "NTD";
$detail['capital'] = 50000000;
$detail['zh_tw_name'] = "MYPAY-小野喵";
$detail['en_us_name'] = "MYPAY - Cat";
$detail['zip_code'] = "407";
$detail['address'] = "市政北二路236號26樓之1";
$detail['real_zip_code'] = "407";
$detail['real_address'] = "市政北二路236號26樓之1";
$detail['envoice_zip_code'] = "407";
$detail['envoice_address'] = "市政北二路236號26樓之1";
$detail['principal_name'] = "金城武";
$detail['principal_id_type'] = 1;
$detail['principal_id'] = "M123456789";
$detail['principal_birthday'] = "20000101";
$detail['principal_phone_code'] = "886";
$detail['principal_phone'] = "04-26888888";
$detail['principal_cellphone_code'] = '886';
$detail['principal_cellphone'] = "0924-999999";
$detail['principal_zip_code'] = "407";
$detail['principal_address'] = "市政北二路236號26樓之1";
$detail['principal_email'] = "[email protected]";
$detail['salesman_name'] = "金城武";
$detail['salesman_phone_code'] = "886";
$detail['salesman_phone'] = "02-8888888";
$detail['salesman_cellphone_code'] = "886";
$detail['salesman_cellphone'] = "0920-999999";
$detail['salesman_mail'] = "[email protected]";
$detail['accountant_name'] = "金城武";
$detail['accountant_phone_code'] = "886";
$detail['accountant_phone'] = "02-8888888";
$detail['accountant_fax_code'] = "886";
$detail['accountant_fax'] = "02-8888887";
$detail['accountant_cellphone_code'] = "886";
$detail['accountant_cellphone'] = "0920-999999";
$detail['accountant_mail'] = "[email protected]";
$detail['technician_name'] = "金城武";
$detail['technician_phone_code'] = "886";
$detail['technician_phone'] = "02-8888888";
$detail['technician_cellphone_code'] = "886";
$detail['technician_cellphone'] = "0920-999999";
$detail['technician_mail'] = "[email protected]";
$principal_id_front_img['name'] = "身分證正面.png";
$principal_id_front_img['size'] = $sampleFile['size'];
$principal_id_front_img['type'] = $sampleFile['type'];
$principal_id_front_img['data'] = $sampleFile['data'];
$principal_id_obverse_img['name'] = "身分證反面.png";
$principal_id_obverse_img['size'] = $sampleFile['size'];
$principal_id_obverse_img['type'] = $sampleFile['type'];
$principal_id_obverse_img['data'] = $sampleFile['data'];
$principal_driverlicense_healthidcard_img['name'] = "負責人第二證件.png";
$principal_driverlicense_healthidcard_img['size'] = $sampleFile['size'];
$principal_driverlicense_healthidcard_img['type'] = $sampleFile['type'];
$principal_driverlicense_healthidcard_img['data'] = $sampleFile['data'];
$approvalletter_img['name'] = "變更事項登記表.png";
$approvalletter_img['size'] = $sampleFile['size'];
$approvalletter_img['type'] = $sampleFile['type'];
$approvalletter_img['data'] = $sampleFile['data'];
$report_401_405_img = $sampleFile;
$report_401_405_img['name'] = "401報表-108-1.png";
$business_photos_img_1 = $sampleFile;
$business_photos_img_1['name'] = "營業照片第一張.png";
$business_photos_img_2 = $sampleFile;
$business_photos_img_2['name'] = "營業照片第二張.png";
$business_photos_img_3 = $sampleFile;
$business_photos_img_3['name'] = "營業照片第三張.png";
$business_photos_img_4 = $sampleFile;
$business_photos_img_4['name'] = "營業照片第四張.png";
$business_photos_4_img[] = $business_photos_img_1;
$business_photos_4_img[] = $business_photos_img_2;
$business_photos_4_img[] = $business_photos_img_3;
$business_photos_4_img[] = $business_photos_img_4;
$bank_passbook_img['name'] = '銀行存摺封面影本.png';
$bank_passbook_img['size'] = $sampleFile['size'];
$bank_passbook_img['type'] = $sampleFile['type'];
$bank_passbook_img['data'] = $sampleFile['data'];
$detail['files'] = ['principal_id_front_img' => $principal_id_front_img,
'principal_id_obverse_img' => $principal_id_obverse_img,
'principal_driverlicense_healthidcard_img' => $principal_driverlicense_healthidcard_img,
'approvalletter_img' => $approvalletter_img,
'report_401_405_img' => $report_401_405_img,
'business_photos_4_img' => $business_photos_4_img,
'bank_passbook_img' => $bank_passbook_img];
*/
$rawData['detail'] = $detail;
return $rawData;
}
/**
* 取得服務位置
* @return array
*/
public function getService()
{
return array(
'service_name' => 'store',
'cmd' => 'api/authorizedstore'
);
}
/**
* AES 256 加密
* @param array $fields
* @param string $key
* @return string
*/
public function encrypt($fields, $key)
{
$data = json_encode($fields);
$size = openssl_cipher_iv_length('AES-256-CBC');
$iv = openssl_random_pseudo_bytes($size);
$data = openssl_encrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
$data = base64_encode($iv . $data);
return $data;
}
/**
* 資料 POST 到主機
* @param array $postData
* @return mixed
*/
public function post($postData = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* 取得送出欄位資料
* @return array
*/
public function getPostData ()
{
$postData = array();
$postData['agent_uid'] = $this->agentUid;
$postData['service'] = $this->encrypt($this->getService(), $this->agentKey);
$postData['encry_data'] = $this->encrypt($this->getRawData(), $this->agentKey);
return $postData;
}
/**
* 執行
*/
public function run()
{
$json = $this->post($this->getPostData());
echo $json;
}
}
$AgentApplicant = new AgentApplicant();
$AgentApplicant->run();
?>
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Dynamic;
/// <summary>
/// 建議使用.net framework4.5以上版本
/// 1.請透過NuGet安裝Newtonsoft.Json,若.net framework小於4.5請安裝Newtonsoft.Json 7.0以下版本
/// 2.若貴司.net framework小於4 可能無法使用dynamic,若是自行組陣列
/// 3.若貴司.net framework小於3.5 可能無法使用AES類別,若是參閱微軟網站使用較舊方式進行加密
/// 4.若貴司.net framework小於3.5 可能沒有Linq可使用,故data只能組字串,Newtonsoft.Json也可能無法使用
/// </summary>
namespace MyPay {
/// <summary>
/// 經銷商串接-特約商店申請
/// </summary>
public class AgentApplicant {
/// <summary>
/// 經銷商商務代號
/// </summary>
public string agentUid = "A1234567891002";
/// <summary>
/// 經銷商金鑰或認證碼
/// </summary>
public string agentKey = "nq8KL8qNNCW12A3TBfAp4lU0y3PED8IQ";
/// <summary>
/// 串接交易位置
/// </summary>
public string url = "https://pay.usecase.cc/api/agent";
/// <summary>
/// 執行
/// </summary>
static void Main() {
AgentApplicant simulator = new AgentApplicant();
//僅限走https的Tls 1.2以上版本
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
//發送至遠端
var result = simulator.Post(simulator.GetPostData());
System.Console.WriteLine(result);
}
/// <summary>
/// 取得串接欄位資料
/// </summary>
private DataRequest GetRawData() {
// 範例用檔案
DataFile sampleFile = new DataFile();
sampleFile.size = 273;;
sampleFile.type = "image/png";;
sampleFile.data = "iVBORw0KGgoAAAANSUhEUgAAADMAAAASCAIAAAB5M3HuAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACmSURBVEhLY/hPe3Dv+1O/ey2LX+96//sTVIgIQD+XQRDx7qO3y4h338C4DILwu28gXQZBuNzHANRGa3To4wU012AiTPcxoKkYWITsvsHlMgiCuG8wugyCBqnLCh5NH3QuA7rpzOfrgyudwd0EAYOi1EBzEwQMcEkb/6Bvx/uTUHWoYMBcBnHT978/oYowwAC4jKCbIICuLiPSTRBAJ5eR5CYQ+P8fAPhjvViKbEtMAAAAAElFTkSuQmCC";
// 基本資料
dynamic detail = new ExpandoObject();
detail.project_id = "1";
ArrayList paymodes = new ArrayList();
paymodes.Add(1);
paymodes.Add(9);
detail.paymodes = paymodes;
detail.is_agent_einvoice = 1;
detail.background_return_url = "https://query.usecase.cc/receive.php";
detail.allot_bank_country = 1;
detail.allot_bank_name = 23;
detail.allot_bank_ext = 1872;
detail.allot_bank_idname = "金城武";
detail.allot_bank_id = "12330001144";
detail.allot_bank_currency = "NTD";
detail.service_mail = "[email protected]";
detail.service_phone_type = 1;
detail.service_phone_code = "886";
detail.service_phone = "02-88888888";
// 個人
detail.zh_tw_name = "君の名は";
detail.en_us_name = "Your Name";
detail.applicant_name = "金城武";
detail.applicant_id_type = 1;
detail.applicant_id = "M123456789";
detail.applicant_birthday = "20000131";
detail.applicant_zip_code = "432";
detail.applicant_address = "大肚里沙田路二段199號";
detail.applicant_phone_code = "886";
detail.applicant_phone = "04-26888888";
detail.applicant_cellphone_code = "886";
detail.applicant_cellphone = "0924-999999";
detail.applicant_email = "[email protected]";
detail.mcc_code = "1711";
detail.real_zip_code = "407";
detail.real_address = "市政北二路236號26樓之1";
DataFile principal_id_front_img = new DataFile();
principal_id_front_img.name = "身分證正面.png";
principal_id_front_img.size = sampleFile.size;
principal_id_front_img.type = sampleFile.type;
principal_id_front_img.data = sampleFile.data;
DataFile principal_id_obverse_img = new DataFile();
principal_id_obverse_img.name = "身分證反面.png";
principal_id_obverse_img.size = sampleFile.size;
principal_id_obverse_img.type = sampleFile.type;
principal_id_obverse_img.data = sampleFile.data;
DataFile principal_driverlicense_healthidcard_img = new DataFile();
principal_driverlicense_healthidcard_img.name = "申請人第二證件.png";
principal_driverlicense_healthidcard_img.size = sampleFile.size;
principal_driverlicense_healthidcard_img.type = sampleFile.type;
principal_driverlicense_healthidcard_img.data = sampleFile.data;
ArrayList business_photos_4_img = new ArrayList();
DataFile business_photos_img_1 = new DataFile();
business_photos_img_1.name = "營業照片第一張.png";
business_photos_img_1.size = sampleFile.size;
business_photos_img_1.type = sampleFile.type;
business_photos_img_1.data = sampleFile.data;
business_photos_4_img.Add(business_photos_img_1);
DataFile business_photos_img_2 = new DataFile();
business_photos_img_2.name = "營業照片第一張.png";
business_photos_img_2.size = sampleFile.size;
business_photos_img_2.type = sampleFile.type;
business_photos_img_2.data = sampleFile.data;
business_photos_4_img.Add(business_photos_img_2);
DataFile business_photos_img_3 = new DataFile();
business_photos_img_3.name = "營業照片第一張.png";
business_photos_img_3.size = sampleFile.size;
business_photos_img_3.type = sampleFile.type;
business_photos_img_3.data = sampleFile.data;
business_photos_4_img.Add(business_photos_img_3);
DataFile business_photos_img_4 = new DataFile();
business_photos_img_4.name = "營業照片第一張.png";
business_photos_img_4.size = sampleFile.size;
business_photos_img_4.type = sampleFile.type;
business_photos_img_4.data = sampleFile.data;
business_photos_4_img.Add(business_photos_img_4);
DataFile bank_passbook_img = new DataFile();
bank_passbook_img.name = "銀行存摺封面影本.png";
bank_passbook_img.size = sampleFile.size;
bank_passbook_img.type = sampleFile.type;
bank_passbook_img.data = sampleFile.data;
dynamic files = new ExpandoObject();
files.principal_id_front_img = principal_id_front_img;
files.principal_id_obverse_img = principal_id_obverse_img;
files.principal_driverlicense_healthidcard_img = principal_driverlicense_healthidcard_img;
files.business_photos_4_img = business_photos_4_img;
files.bank_passbook_img = bank_passbook_img;
detail.files = files;
/*
// 公司
detail.org_type_uid = 2;
detail.tong_bian = "28915188";
detail.id_name = "高鉅科技股份有限公司";
detail.setup_date = "20081001";
detail.mcc_code = "1711";
detail.capital_currency = "NTD";
detail.capital = 50000000;
detail.zh_tw_name = "MYPAY-小野喵";
detail.en_us_name = "MYPAY - Cat";
detail.zip_code = "407";
detail.address = "市政北二路236號26樓之1";
detail.real_zip_code = "407";
detail.real_address = "市政北二路236號26樓之1";
detail.envoice_zip_code = "407";
detail.envoice_address = "市政北二路236號26樓之1";
detail.principal_name = "金城武";
detail.principal_id_type = 1;
detail.principal_id = "M123456789";
detail.principal_birthday = "20000101";
detail.principal_phone_code = "886";
detail.principal_phone = "04-26888888";
detail.principal_cellphone_code = "886";
detail.principal_cellphone = "0924-999999";
detail.principal_zip_code = "407";
detail.principal_address = "市政北二路236號26樓之1";
detail.principal_email = "[email protected]";
detail.salesman_name = "金城武";
detail.salesman_phone_code = "886";
detail.salesman_phone = "02-8888888";
detail.salesman_cellphone_code = "886";
detail.salesman_cellphone = "0920-999999";
detail.salesman_mail = "[email protected]";
detail.accountant_name = "金城武";
detail.accountant_phone_code = "886";
detail.accountant_phone = "02-8888888";
detail.accountant_fax_code = "886";
detail.accountant_fax = "02-8888887";
detail.accountant_cellphone_code = "886";
detail.accountant_cellphone = "0920-999999";
detail.accountant_mail = "[email protected]";
detail.technician_name = "金城武";
detail.technician_phone_code = "886";
detail.technician_phone = "02-8888888";
detail.technician_cellphone_code = "886";
detail.technician_cellphone = "0920-999999";
detail.technician_mail = "[email protected]";
File principal_id_front_img = new File();
principal_id_front_img.name = "身分證正面.png";
principal_id_front_img.size = sampleFile.size;
principal_id_front_img.type = sampleFile.type;
principal_id_front_img.data = sampleFile.data;
File principal_id_obverse_img = new File();
principal_id_obverse_img.name = "身分證反面.png";
principal_id_obverse_img.size = sampleFile.size;
principal_id_obverse_img.type = sampleFile.type;
principal_id_obverse_img.data = sampleFile.data;
File principal_driverlicense_healthidcard_img = new File();
principal_driverlicense_healthidcard_img.name = "申請人第二證件.png";
principal_driverlicense_healthidcard_img.size = sampleFile.size;
principal_driverlicense_healthidcard_img.type = sampleFile.type;
principal_driverlicense_healthidcard_img.data = sampleFile.data;
File approvalletter_img = new File();
approvalletter_img.name = "變更事項登記表.png";
approvalletter_img.size = sampleFile.size;
approvalletter_img.type = sampleFile.type;
approvalletter_img.data = sampleFile.data;
File report_401_405_img = new File();
report_401_405_img.name = "401報表-108-1.png";
report_401_405_img.size = sampleFile.size;
report_401_405_img.type = sampleFile.type;
report_401_405_img.data = sampleFile.data;
ArrayList business_photos_4_img = new ArrayList();
File business_photos_img_1 = new File();
business_photos_img_1.name = "營業照片第一張.png";
business_photos_img_1.size = sampleFile.size;
business_photos_img_1.type = sampleFile.type;
business_photos_img_1.data = sampleFile.data;
business_photos_4_img.Add(business_photos_img_1);
File business_photos_img_2 = new File();
business_photos_img_2.name = "營業照片第一張.png";
business_photos_img_2.size = sampleFile.size;
business_photos_img_2.type = sampleFile.type;
business_photos_img_2.data = sampleFile.data;
business_photos_4_img.Add(business_photos_img_2);
File business_photos_img_3 = new File();
business_photos_img_3.name = "營業照片第一張.png";
business_photos_img_3.size = sampleFile.size;
business_photos_img_3.type = sampleFile.type;
business_photos_img_3.data = sampleFile.data;
business_photos_4_img.Add(business_photos_img_3);
File business_photos_img_4 = new File();
business_photos_img_4.name = "營業照片第一張.png";
business_photos_img_4.size = sampleFile.size;
business_photos_img_4.type = sampleFile.type;
business_photos_img_4.data = sampleFile.data;
business_photos_4_img.Add(business_photos_img_4);
File bank_passbook_img = new File();
bank_passbook_img.name = "銀行存摺封面影本.png";
bank_passbook_img.size = sampleFile.size;
bank_passbook_img.type = sampleFile.type;
bank_passbook_img.data = sampleFile.data;
dynamic files = new ExpandoObject();
files.principal_id_front_img = principal_id_front_img;
files.principal_id_obverse_img = principal_id_obverse_img;
files.principal_driverlicense_healthidcard_img = principal_driverlicense_healthidcard_img;
files.approvalletter_img = approvalletter_img;
files.report_401_405_img = report_401_405_img;
files.business_photos_4_img = business_photos_4_img;
files.bank_passbook_img = bank_passbook_img;
detail.files = files;
*/
DataRequest rawData = new DataRequest();
rawData.agent_uid = this.agentUid;
rawData.type = 0;
rawData.detail = detail;
return rawData;
}
/// <summary>
/// 取得服務位置
/// </summary>
private ServiceRequest GetService() {
ServiceRequest rawData = new ServiceRequest();
rawData.service_name = "store";
rawData.cmd = "api/authorizedstore";
return rawData;
}
/// <summary>
/// 取得送出欄位資料
/// </summary>
private NameValueCollection GetPostData() {
string data_json = JsonConvert.SerializeObject(GetRawData(), Formatting.None);
string svr_json = JsonConvert.SerializeObject(GetService(), Formatting.None);; //依API種類調整
//產生AES向量
var IV = GetBytesIV();
//進行加密
var data_encode = Encrypt(data_json, this.agentKey, IV);
var svr_encode = Encrypt(svr_json, this.agentKey, IV);
//請注意使用的 Http Post 套件是否會自動加上UrlEncode,本Post範例為原始方式,故須加上UrlEncode
//若自行使用的套件會自動補上UrlEncode,則請忽略下面的UrlEncode,避免做了兩次UrlEncode
string data_toUrlEncode = HttpUtility.UrlEncode(data_encode);
string svr_toUrlEncode = HttpUtility.UrlEncode(svr_encode);
NameValueCollection postData = new NameValueCollection();
postData["agent_uid"] = this.agentUid;
postData["service"] = svr_toUrlEncode;
postData["encry_data"] = data_toUrlEncode;
return postData;
}
/// <summary>
/// AES 256 加密
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <param name="byteIV"></param>
/// <returns></returns>
private string Encrypt(string data, string key, byte[] byteIV) {
var byteKey = System.Text.Encoding.UTF8.GetBytes(key);
var enBytes = AES_Encrypt(data, byteKey, byteIV);
return Convert.ToBase64String(BytesAdd(byteIV, enBytes));
}
/// <summary>
/// AES 256 加密處理
/// </summary>
/// <param name="original"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <returns></returns>
private byte[] AES_Encrypt(string original, byte[] key, byte[] iv) {
try {
var data = Encoding.UTF8.GetBytes(original);
var cipher = Aes.Create().CreateEncryptor(key, iv);
var de = cipher.TransformFinalBlock(data, 0, data.Length);
return de;
} catch {
return null;
}
}
/// <summary>
/// 轉換Bytes
/// </summary>
/// <param name="a"></param>
/// <param name="arryB"></param>
/// <returns></returns>
private byte[] BytesAdd(byte[] a, params byte[][] arryB) {
List < byte > c = new List < byte > ();
c.AddRange(a);
arryB.ToList().ForEach(b => {
c.AddRange(b);
});
return c.ToArray();
}
/// <summary>
/// 產生AES的IV
/// </summary>
/// <returns></returns>
private static byte[] GetBytesIV() {
var aes = System.Security.Cryptography.AesCryptoServiceProvider.Create();
aes.KeySize = 256;
aes.GenerateIV();
return aes.IV;
}
/// <summary>
/// 資料 POST 到主機
/// </summary>
/// <param name="pars"></param>
/// <returns></returns>
private string Post(NameValueCollection pars) {
string result = string.Empty;
string param = string.Empty;
if (pars.Count > 0) {
pars.AllKeys.ToList().ForEach(key => {
param += key + "=" + pars[key] + "&";
});
if (param[param.Length - 1] == '&') {
param = param.Remove(param.Length - 1);
}
}
byte[] bs = Encoding.UTF8.GetBytes(param);
try {
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create(this.url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
using(Stream reqStream = req.GetRequestStream()) {
reqStream.Write(bs, 0, bs.Length);
}
using(WebResponse wr = req.GetResponse()) {
Encoding myEncoding = Encoding.GetEncoding("UTF-8");
using(StreamReader myStreamReader = new StreamReader(wr.GetResponseStream(), myEncoding)) {
result = myStreamReader.ReadToEnd();
}
}
req = null;
} catch (WebException ex) {
throw new WebException(ex.Message + "params : " + param, ex, ex.Status, ex.Response);
}
return result;
}
}
/// <summary>
/// 串接內容資料請求欄位
/// </summary>
public class DataRequest {
public string agent_uid { get; set; }
public int type { get; set; }
public ExpandoObject detail { get; set; }
}
/// <summary>
/// 夾檔檔案資訊
/// </summary>
public class DataFile {
public string name { get; set; }
public int size { get; set; }
public string type { get; set; }
public string data { get; set; }
}
/// <summary>
/// 串接服務請求欄位
/// </summary>
public class ServiceRequest {
public string service_name { get; set; }
public string cmd { get; set; }
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URLEncoder;
import java.util.*;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import static java.nio.charset.StandardCharsets.UTF_8;
import org.spongycastle.crypto.engines.AESEngine;
import org.spongycastle.crypto.modes.CBCBlockCipher;
import org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.spongycastle.crypto.params.KeyParameter;
import org.spongycastle.crypto.params.ParametersWithIV;
import java.security.SecureRandom;
/**
* 經銷商串接-特約商店申請
* 1. jackson-core 下載 https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
* 2. jackson-databind 下載 https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
* 3. jackson-annotations 下載 https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations
* 4. Spongy Castle 下載 https://mvnrepository.com/artifact/com.madgag.spongycastle/core
*/
public class AgentApplicant {
/**
* 經銷商商務代號
*/
String agentUid = "A1234567891002";
/**
* 經銷商金鑰或認證碼
*/
String agentKey = "nq8KL8qNNCW12A3TBfAp4lU0y3PED8IQ";
/**
* 串接交易位置
*/
String url = "https://pay.usecase.cc/api/agent";
/**
* 執行
* @param args
*/
public static void main(String[] args) {
AgentApplicant simulator = new AgentApplicant();
String json = simulator.post(simulator.getPostData());
System.out.print(json);
}
@SuppressWarnings(value = { "unchecked", "deprecation" })
/**
* 取得串接欄位資料
* @return 串接原始資料
*/
public Map getRawData() {
// 範例用檔案
Map<Object, Object> sampleFile = new HashMap<Object, Object>();
sampleFile.put("size", 273);
sampleFile.put("type", "image/png");
sampleFile.put("data", "iVBORw0KGgoAAAANSUhEUgAAADMAAAASCAIAAAB5M3HuAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACmSURBVEhLY/hPe3Dv+1O/ey2LX+96//sTVIgIQD+XQRDx7qO3y4h338C4DILwu28gXQZBuNzHANRGa3To4wU012AiTPcxoKkYWITsvsHlMgiCuG8wugyCBqnLCh5NH3QuA7rpzOfrgyudwd0EAYOi1EBzEwQMcEkb/6Bvx/uTUHWoYMBcBnHT978/oYowwAC4jKCbIICuLiPSTRBAJ5eR5CYQ+P8fAPhjvViKbEtMAAAAAElFTkSuQmCC");
// 基本資料
Map<Object, Object> detail = new HashMap<Object, Object>();
detail.put("project_id", "1");
ArrayList paymodes = new ArrayList();
paymodes.add(1);
paymodes.add(9);
detail.put("paymodes", paymodes);
detail.put("is_agent_einvoice", 1);
detail.put("background_return_url", "https://query.usecase.cc/receive.php");
detail.put("allot_bank_country", 1);
detail.put("allot_bank_name", 23);
detail.put("allot_bank_ext", 1872);
detail.put("allot_bank_idname", "金城武");
detail.put("allot_bank_id", "12330001144");
detail.put("allot_bank_currency", "NTD");
detail.put("service_mail", "[email protected]");
detail.put("service_phone_type", 1);
detail.put("service_phone_code", "886");
detail.put("service_phone", "02-88888888");
// 個人
detail.put("zh_tw_name", "君の名は");
detail.put("en_us_name", "Your Name");
detail.put("applicant_name", "金城武");
detail.put("applicant_id_type", 1);
detail.put("applicant_id", "M123456789");
detail.put("applicant_birthday", "20000131");
detail.put("applicant_zip_code", "432");
detail.put("applicant_address", "大肚里沙田路二段199號");
detail.put("applicant_phone_code", "886");
detail.put("applicant_phone", "04-26888888");
detail.put("applicant_cellphone_code", "886");
detail.put("applicant_cellphone", "0924-999999");
detail.put("applicant_email", "[email protected]");
detail.put("mcc_code", "1711");
detail.put("real_zip_code", "407");
detail.put("real_address", "市政北二路236號26樓之1");
Map<Object, Object> principal_id_front_img = new HashMap<Object, Object>();
principal_id_front_img.put("name", "身分證正面.png");
principal_id_front_img.put("size", sampleFile.get("size"));
principal_id_front_img.put("type", sampleFile.get("type"));
principal_id_front_img.put("data", sampleFile.get("data"));
Map<Object, Object> principal_id_obverse_img = new HashMap<Object, Object>();
principal_id_obverse_img.put("name", "身分證反面.png");
principal_id_obverse_img.put("size", sampleFile.get("size"));
principal_id_obverse_img.put("type", sampleFile.get("type"));
principal_id_obverse_img.put("data", sampleFile.get("data"));
Map<Object, Object> principal_driverlicense_healthidcard_img = new HashMap<Object, Object>();
principal_driverlicense_healthidcard_img.put("name", "申請人第二證件.png");
principal_driverlicense_healthidcard_img.put("size", sampleFile.get("size"));
principal_driverlicense_healthidcard_img.put("type", sampleFile.get("type"));
principal_driverlicense_healthidcard_img.put("data", sampleFile.get("data"));
ArrayList business_photos_4_img = new ArrayList();
Map<Object, Object> business_photos_4_img_1 = new HashMap<Object, Object>();
business_photos_4_img_1.put("name", "營業照片第一張.png");
business_photos_4_img_1.put("size", sampleFile.get("size"));
business_photos_4_img_1.put("type", sampleFile.get("type"));
business_photos_4_img_1.put("data", sampleFile.get("data"));
business_photos_4_img.add(business_photos_4_img_1);
Map<Object, Object> business_photos_4_img_2 = new HashMap<Object, Object>();
business_photos_4_img_2.put("name", "營業照片第二張.png");
business_photos_4_img_2.put("size", sampleFile.get("size"));
business_photos_4_img_2.put("type", sampleFile.get("type"));
business_photos_4_img_2.put("data", sampleFile.get("data"));
business_photos_4_img.add(business_photos_4_img_2);
Map<Object, Object> business_photos_4_img_3 = new HashMap<Object, Object>();
business_photos_4_img_3.put("name", "營業照片第三張.png");
business_photos_4_img_3.put("size", sampleFile.get("size"));
business_photos_4_img_3.put("type", sampleFile.get("type"));
business_photos_4_img_3.put("data", sampleFile.get("data"));
business_photos_4_img.add(business_photos_4_img_3);
Map<Object, Object> business_photos_4_img_4 = new HashMap<Object, Object>();
business_photos_4_img_4.put("name", "營業照片第四張.png");
business_photos_4_img_4.put("size", sampleFile.get("size"));
business_photos_4_img_4.put("type", sampleFile.get("type"));
business_photos_4_img_4.put("data", sampleFile.get("data"));
business_photos_4_img.add(business_photos_4_img_4);
Map<Object, Object> bank_passbook_img = new HashMap<Object, Object>();
bank_passbook_img.put("name", "銀行存摺封面影本.png");
bank_passbook_img.put("size", sampleFile.get("size"));
bank_passbook_img.put("type", sampleFile.get("type"));
bank_passbook_img.put("data", sampleFile.get("data"));
Map<Object, Object> files = new HashMap<Object, Object>();
files.put("principal_id_front_img", principal_id_front_img);
files.put("principal_id_obverse_img", principal_id_obverse_img);
files.put("principal_driverlicense_healthidcard_img", principal_driverlicense_healthidcard_img);
files.put("business_photos_4_img", business_photos_4_img);
files.put("bank_passbook_img", bank_passbook_img);
detail.put("files", files);
/*
// 公司
detail.put("org_type_uid", 2);
detail.put("tong_bian", "28915188");
detail.put("id_name", "高鉅科技股份有限公司");
detail.put("setup_date", "20081001");
detail.put("mcc_code", "1711");
detail.put("capital_currency", "NTD");
detail.put("capital", 50000000);
detail.put("zh_tw_name", "MYPAY-小野喵");
detail.put("en_us_name", "MYPAY - Cat");
detail.put("zip_code", "407");
detail.put("address", "市政北二路236號26樓之1");
detail.put("real_zip_code", "407");
detail.put("real_address", "市政北二路236號26樓之1");
detail.put("envoice_zip_code", "407");
detail.put("envoice_address", "市政北二路236號26樓之1");
detail.put("principal_name", "金城武");
detail.put("principal_id_type", 1);
detail.put("principal_id", "M123456789");
detail.put("principal_birthday", "20000101");
detail.put("principal_phone_code", "886");
detail.put("principal_phone", "04-26888888");
detail.put("principal_cellphone_code", "886");
detail.put("principal_cellphone", "0924-999999");
detail.put("principal_zip_code", "407");
detail.put("principal_address", "市政北二路236號26樓之1");
detail.put("principal_email", "[email protected]");
detail.put("salesman_name", "金城武");
detail.put("salesman_phone_code", "886");
detail.put("salesman_phone", "02-8888888");
detail.put("salesman_cellphone_code", "886");
detail.put("salesman_cellphone", "0920-999999");
detail.put("salesman_mail", "[email protected]");
detail.put("accountant_name", "金城武");
detail.put("accountant_phone_code", "886");
detail.put("accountant_phone", "02-8888888");
detail.put("accountant_fax_code", "886");
detail.put("accountant_fax", "02-8888887");
detail.put("accountant_cellphone_code", "886");
detail.put("accountant_cellphone", "0920-999999");
detail.put("accountant_mail", "[email protected]");
detail.put("technician_name", "金城武");
detail.put("technician_phone_code", "886");
detail.put("technician_phone", "02-8888888");
detail.put("technician_cellphone_code", "886");
detail.put("technician_cellphone", "0920-999999");
detail.put("technician_mail", "[email protected]");
Map<Object, Object> principal_id_front_img = new HashMap<Object, Object>();
principal_id_front_img.put("name", "身分證正面.png");
principal_id_front_img.put("size", sampleFile.get("size"));
principal_id_front_img.put("type", sampleFile.get("type"));
principal_id_front_img.put("data", sampleFile.get("data"));
Map<Object, Object> principal_id_obverse_img = new HashMap<Object, Object>();
principal_id_obverse_img.put("name", "身分證反面.png");
principal_id_obverse_img.put("size", sampleFile.get("size"));
principal_id_obverse_img.put("type", sampleFile.get("type"));
principal_id_obverse_img.put("data", sampleFile.get("data"));
Map<Object, Object> principal_driverlicense_healthidcard_img = new HashMap<Object, Object>();
principal_driverlicense_healthidcard_img.put("name", "申請人第二證件.png");
principal_driverlicense_healthidcard_img.put("size", sampleFile.get("size"));
principal_driverlicense_healthidcard_img.put("type", sampleFile.get("type"));
principal_driverlicense_healthidcard_img.put("data", sampleFile.get("data"));
Map<Object, Object> approvalletter_img = new HashMap<Object, Object>();
approvalletter_img.put("name", "變更事項登記表.png");
approvalletter_img.put("size", sampleFile.get("size"));
approvalletter_img.put("type", sampleFile.get("type"));
approvalletter_img.put("data", sampleFile.get("data"));
Map<Object, Object> report_401_405_img = new HashMap<Object, Object>();
report_401_405_img.put("name", "401報表-108-1.png");
report_401_405_img.put("size", sampleFile.get("size"));
report_401_405_img.put("type", sampleFile.get("type"));
report_401_405_img.put("data", sampleFile.get("data"));
ArrayList business_photos_4_img = new ArrayList();
Map<Object, Object> business_photos_4_img_1 = new HashMap<Object, Object>();
business_photos_4_img_1.put("name", "營業照片第一張.png");
business_photos_4_img_1.put("size", sampleFile.get("size"));
business_photos_4_img_1.put("type", sampleFile.get("type"));
business_photos_4_img_1.put("data", sampleFile.get("data"));
business_photos_4_img.add(business_photos_4_img_1);
Map<Object, Object> business_photos_4_img_2 = new HashMap<Object, Object>();
business_photos_4_img_2.put("name", "營業照片第二張.png");
business_photos_4_img_2.put("size", sampleFile.get("size"));
business_photos_4_img_2.put("type", sampleFile.get("type"));
business_photos_4_img_2.put("data", sampleFile.get("data"));
business_photos_4_img.add(business_photos_4_img_2);
Map<Object, Object> business_photos_4_img_3 = new HashMap<Object, Object>();
business_photos_4_img_3.put("name", "營業照片第三張.png");
business_photos_4_img_3.put("size", sampleFile.get("size"));
business_photos_4_img_3.put("type", sampleFile.get("type"));
business_photos_4_img_3.put("data", sampleFile.get("data"));
business_photos_4_img.add(business_photos_4_img_3);
Map<Object, Object> business_photos_4_img_4 = new HashMap<Object, Object>();
business_photos_4_img_4.put("name", "營業照片第四張.png");
business_photos_4_img_4.put("size", sampleFile.get("size"));
business_photos_4_img_4.put("type", sampleFile.get("type"));
business_photos_4_img_4.put("data", sampleFile.get("data"));
business_photos_4_img.add(business_photos_4_img_4);
Map<Object, Object> bank_passbook_img = new HashMap<Object, Object>();
bank_passbook_img.put("name", "銀行存摺封面影本.png");
bank_passbook_img.put("size", sampleFile.get("size"));
bank_passbook_img.put("type", sampleFile.get("type"));
bank_passbook_img.put("data", sampleFile.get("data"));
Map<Object, Object> files = new HashMap<Object, Object>();
files.put("principal_id_front_img", principal_id_front_img);
files.put("principal_id_obverse_img", principal_id_obverse_img);
files.put("principal_driverlicense_healthidcard_img", principal_driverlicense_healthidcard_img);
files.put("approvalletter_img", approvalletter_img);
files.put("report_401_405_img", report_401_405_img);
files.put("business_photos_4_img", business_photos_4_img);
files.put("bank_passbook_img", bank_passbook_img);
detail.put("files", files);
*/
Map<Object, Object> rawData = new HashMap<Object, Object>();
rawData.put("agent_uid", this.agentUid);
rawData.put("type", 0);
rawData.put("detail", detail);
return rawData;
}
/**
* 取得服務位置
* @return 串接服務資料
*/
public Map getService() {
Map<Object, Object> rawData = new HashMap<Object, Object>();
rawData.put("service_name", "store");
rawData.put("cmd", "api/authorizedstore");
return rawData;
}
/**
* AES 256 加密
* @param rawData 原始資料
* @param AesKey AES256金鑰字串
* @return 轉換成Base64資料
*/
public String encrypt(Map rawData, String AesKey) {
try {
ObjectMapper objMapper = new ObjectMapper();
byte[] data = objMapper.writeValueAsString(rawData).getBytes(UTF_8);
byte[] key = AesKey.getBytes(UTF_8);
// 16 bytes is the IV size for AES256
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new AESEngine()));
// Random iv
SecureRandom rng = new SecureRandom();
byte[] ivBytes = new byte[16];
rng.nextBytes(ivBytes);
cipher.init(true, new ParametersWithIV(new KeyParameter(key),
ivBytes));
byte[] outBuf = new byte[cipher.getOutputSize(data.length)];
int processed = cipher
.processBytes(data, 0, data.length, outBuf, 0);
processed += cipher.doFinal(outBuf, processed);
byte[] outBuf2 = new byte[processed + 16]; // Make room for iv
System.arraycopy(ivBytes, 0, outBuf2, 0, 16); // Add iv
System.arraycopy(outBuf, 0, outBuf2, 16, processed);
Base64.Encoder encoder = Base64.getEncoder();
String base64 = encoder.encodeToString(outBuf2);
return base64;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 資料 POST 到主機
* @param qstr 串接資料
* @return 服務回傳JSON資訊
*/
public String post(String qstr) {
String result = "";
try {
// 資料
byte[] qstr_bytes = qstr.getBytes(StandardCharsets.UTF_8);
URL iurl = new URL(this.url);
SSLContext sc = SSLContext.getInstance("TLSv1.2"); // $NON-NLS-1$
sc.init(null, null, new java.security.SecureRandom());
HttpsURLConnection con = (HttpsURLConnection) iurl.openConnection();
con.setSSLSocketFactory(sc.getSocketFactory());
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length",
String.valueOf(qstr_bytes.length));
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setDoOutput(true);
con.setDoInput(true);
con.getOutputStream()
.write(qstr.getBytes(Charset.forName("UTF-8")));
con.getOutputStream().flush();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream(), "UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine + "\r\n");
}
try {
result = response.toString();
} finally {
in.close();
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return result;
}
/**
* 取得送出欄位資料
* @return POST完整資料
*/
public String getPostData() {
String postData = "";
try {
// Base64需要使用UrlEncode做傳輸
String data_toUrlEncode = URLEncoder.encode(
this.encrypt(this.getRawData(), this.agentKey), "UTF-8");
String svr_toUrlEncode = URLEncoder.encode(
this.encrypt(this.getService(), this.agentKey), "UTF-8");
postData = "agent_uid=" + this.agentUid + "&service="
+ svr_toUrlEncode + "&encry_data=" + data_toUrlEncode;
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return postData;
}
}
const crypto = require('crypto');
const httpRequest = require('https');
/**
* 經銷商串接-特約商店申請
*/
function AgentApplicant() {
// 經銷商商務代號
this.agentUid = "A1234567891002";
// 經銷商金鑰或認證碼
this.agentKey = "nq8KL8qNNCW12A3TBfAp4lU0y3PED8IQ";
// 串接交易位置
this.url = "https://pay.usecase.cc/api/agent";
};
/**
* 取得串接欄位資料
*/
AgentApplicant.prototype.getRawData = function () {
// 範例用檔案
sampleFile = {
size: 273,
type: "image/png",
data: "iVBORw0KGgoAAAANSUhEUgAAADMAAAASCAIAAAB5M3HuAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACmSURBVEhLY/hPe3Dv+1O/ey2LX+96//sTVIgIQD+XQRDx7qO3y4h338C4DILwu28gXQZBuNzHANRGa3To4wU012AiTPcxoKkYWITsvsHlMgiCuG8wugyCBqnLCh5NH3QuA7rpzOfrgyudwd0EAYOi1EBzEwQMcEkb/6Bvx/uTUHWoYMBcBnHT978/oYowwAC4jKCbIICuLiPSTRBAJ5eR5CYQ+P8fAPhjvViKbEtMAAAAAElFTkSuQmCC"
}
// 基本資料
detail = {
project_id: "1",
paymodes: [1, 9],
is_agent_einvoice: 1,
background_return_url: "https://query.usecase.cc/receive.php",
allot_bank_country: 1,
allot_bank_name: 23,
allot_bank_ext: 1872,
allot_bank_idname: "金城武",
allot_bank_id: "12330001144",
allot_bank_currency: "NTD",
service_mail: "[email protected]",
service_phone_type: 1,
service_phone_code: "886",
service_phone: "02-88888888"
}
// 個人
detail.zh_tw_name = "君の名は";
detail.en_us_name = "Your Name";
detail.applicant_name = "金城武";
detail.applicant_id_type = 1;
detail.applicant_id = "M123456789";
detail.applicant_birthday = "20000131";
detail.applicant_zip_code = '432';
detail.applicant_address = "大肚里沙田路二段199號";
detail.applicant_phone_code = "886";
detail.applicant_phone = "04-26888888";
detail.applicant_cellphone_code = '886';
detail.applicant_cellphone = "0924-999999";
detail.applicant_email = "[email protected]";
detail.mcc_code = "1711";
detail.real_zip_code = "407";
detail.real_address = "市政北二路236號26樓之1";
detail.files = {
principal_id_front_img: {
name: "身分證正面.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
},
principal_id_obverse_img: {
name: "身分證反面.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
},
principal_driverlicense_healthidcard_img: {
name: "申請人第二證件.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
},
business_photos_4_img: [
{
name: "營業照片第一張.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
},
{
name: "營業照片第二張.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
},
{
name: "營業照片第三張.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
},
{
name: "營業照片第四張.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
}
],
bank_passbook_img: {
name: "銀行存摺封面影本.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
}
};
/*
// 公司
detail.org_type_uid = 2;
detail.tong_bian = "28915188";
detail.id_name = "高鉅科技股份有限公司";
detail.setup_date = "20081001";
detail.mcc_code = "1711";
detail.capital_currency = "NTD";
detail.capital = 50000000;
detail.zh_tw_name = "MYPAY-小野喵";
detail.en_us_name = "MYPAY - Cat";
detail.zip_code = "407";
detail.address = "市政北二路236號26樓之1";
detail.real_zip_code = "407";
detail.real_address = "市政北二路236號26樓之1";
detail.envoice_zip_code = "407";
detail.envoice_address = "市政北二路236號26樓之1";
detail.principal_name = "金城武";
detail.principal_id_type = 1;
detail.principal_id = "M123456789";
detail.principal_birthday = "20000101";
detail.principal_phone_code = "886";
detail.principal_phone = "04-26888888";
detail.principal_cellphone_code = '886';
detail.principal_cellphone = "0924-999999";
detail.principal_zip_code = "407";
detail.principal_address = "市政北二路236號26樓之1";
detail.principal_email = "[email protected]";
detail.salesman_name = "金城武";
detail.salesman_phone_code = "886";
detail.salesman_phone = "02-8888888";
detail.salesman_cellphone_code = "886";
detail.salesman_cellphone = "0920-999999";
detail.salesman_mail = "[email protected]";
detail.accountant_name = "金城武";
detail.accountant_phone_code = "886";
detail.accountant_phone = "02-8888888";
detail.accountant_fax_code = "886";
detail.accountant_fax = "02-8888887";
detail.accountant_cellphone_code = "886";
detail.accountant_cellphone = "0920-999999";
detail.accountant_mail = "[email protected]";
detail.technician_name = "金城武";
detail.technician_phone_code = "886";
detail.technician_phone = "02-8888888";
detail.technician_cellphone_code = "886";
detail.technician_cellphone = "0920-999999";
detail.technician_mail = "[email protected]";
detail.files = {
principal_id_front_img: {
name: "身分證正面.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
},
principal_id_obverse_img: {
name: "身分證反面.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
},
principal_driverlicense_healthidcard_img: {
name: "負責人第二證件.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
},
approvalletter_img: {
name: "變更事項登記表.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
},
report_401_405_img: {
name: "401報表-108-1.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
},
business_photos_4_img: [{
name: "營業照片第一張.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
}, {
name: "營業照片第二張.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
}, {
name: "營業照片第三張.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
}, {
name: "營業照片第四張.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
}],
bank_passbook_img: {
name: "營業照片第四張.png",
size: sampleFile.size,
type: sampleFile.type,
data: sampleFile.data
}
};*/
return {
agent_uid: this.agentUid,
type: 0,
detail: detail
};
};
/**
* 取得服務位置
*/
AgentApplicant.prototype.getService = function () {
return {
service_name: "store",
cmd: "api/authorizedstore"
};
};
/**
* AES 256 加密
*/
AgentApplicant.prototype.encrypt = function (fields, key) {
let eData = JSON.stringify(fields);
const blockSize = 16;
const iv = crypto.randomBytes(blockSize);
const encryptor = crypto.createCipheriv('aes-256-cbc', key, iv);
let tmpCipher = encryptor.update(Buffer.from(eData));
let finalCipher = encryptor.final();
const tempData = Buffer.concat([tmpCipher, finalCipher], tmpCipher.length + finalCipher.length);
let data = Buffer.concat([iv, tempData], iv.length + tempData.length).toString('base64');
return data;
};
/**
* 資料 POST 到主機
*/
AgentApplicant.prototype.post = function (postData) {
return new Promise((res, rej) => {
let options = {
method: "POST",
headers: {
"Content-Type": "application/json"
},
rejectUnauthorized: false
};
let send_process = httpRequest.request(this.url, options, (api_res) => {
let res_data = "";
api_res.on('data', (tmp_data) => {
res_data += tmp_data;
});
api_res.on('end', () => {
res(res_data);
});
});
send_process.write(JSON.stringify(postData));
send_process.end();
});
};
/**
* 取得送出欄位資料
*/
AgentApplicant.prototype.getPostData = function () {
return {
"agent_uid": this.agentUid,
"service": this.encrypt(this.getService(), this.agentKey),
"encry_data": this.encrypt(this.getRawData(), this.agentKey)
};
};
/**
* 執行
*/
AgentApplicant.prototype.run = async function () {
json = await this.post(this.getPostData())
console.log(json);
};
AgentApplicant = new AgentApplicant();
AgentApplicant.run();
import json
import base64
import requests
from Crypto.Cipher import AES
from Crypto.Util import Padding
from Crypto.Random import get_random_bytes
"""經銷商串接-特約商店申請
"""
class AgentApplicant:
# 經銷商商務代號
agentUid = "A1234567891002";
# 經銷商金鑰或認證碼
agentKey = b"nq8KL8qNNCW12A3TBfAp4lU0y3PED8IQ";
# 串接交易位置
url = "https://pay.usecase.cc/api/agent"
def getRawData(self):
"""取得串接欄位資料
Returns:
{dict}: 欄位資料
"""
## 範例用檔案
sampleFile = {
'size': 273,
'type': "image/png",
'data': "iVBORw0KGgoAAAANSUhEUgAAADMAAAASCAIAAAB5M3HuAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACmSURBVEhLY/hPe3Dv+1O/ey2LX+96//sTVIgIQD+XQRDx7qO3y4h338C4DILwu28gXQZBuNzHANRGa3To4wU012AiTPcxoKkYWITsvsHlMgiCuG8wugyCBqnLCh5NH3QuA7rpzOfrgyudwd0EAYOi1EBzEwQMcEkb/6Bvx/uTUHWoYMBcBnHT978/oYowwAC4jKCbIICuLiPSTRBAJ5eR5CYQ+P8fAPhjvViKbEtMAAAAAElFTkSuQmCC"
}
## 基本資料
detail = {
'project_id': "1",
'paymodes': [1, 9],
'is_agent_einvoice': 1,
'background_return_url': "https://query.usecase.cc/receive.php",
'allot_bank_country': 1,
'allot_bank_name': 23,
'allot_bank_ext': 1872,
'allot_bank_idname': "金城武",
'allot_bank_id': "12330001144",
'allot_bank_currency': "NTD",
'service_mail': "[email protected]",
'service_phone_type': 1,
'service_phone_code': "886",
'service_phone': "02-88888888"
}
# 個人
personal = {
'zh_tw_name': "君の名は",
'en_us_name': "Your Name",
'applicant_name': "金城武",
'applicant_id_type': 1,
'applicant_id': "M123456789",
'applicant_birthday': "20000131",
'applicant_zip_code': '432',
'applicant_address': "大肚里沙田路二段199號",
'applicant_phone_code': "886",
'applicant_phone': "04-26888888",
'applicant_cellphone_code': '886',
'applicant_cellphone': "0924-999999",
'applicant_email': "[email protected]",
'mcc_code': "1711",
'real_zip_code': "407",
'real_address': "市政北二路236號26樓之1",
'files': {
'principal_id_front_img': {
'name': "身分證正面.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
},
'principal_id_obverse_img': {
'name': "身分證反面.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
},
'principal_driverlicense_healthidcard_img': {
'name': "申請人第二證件.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
},
'business_photos_4_img': [
{
'name': "營業照片第一張.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
},
{
'name': "營業照片第二張.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
},
{
'name': "營業照片第三張.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
},
{
'name': "營業照片第四張.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
}
],
'bank_passbook_img': {
'name': "銀行存摺封面影本.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
}
}
}
detail.update(personal)
"""
# 公司
company = {
'org_type_uid': 2,
'tong_bian': "28915188",
'id_name': "高鉅科技股份有限公司",
'setup_date': "20081001",
'mcc_code': "1711",
'capital_currency': "NTD",
'capital': 50000000,
'zh_tw_name': "MYPAY-小野喵",
'en_us_name': "MYPAY - Cat",
'zip_code': "407",
'address': "市政北二路236號26樓之1",
'real_zip_code': "407",
'real_address': "市政北二路236號26樓之1",
'envoice_zip_code': "407",
'envoice_address': "市政北二路236號26樓之1",
'principal_name': "金城武",
'principal_id_type': 1,
'principal_id': "M123456789",
'principal_birthday': "20000101",
'principal_phone_code': "886",
'principal_phone': "04-26888888",
'principal_cellphone_code': '886',
'principal_cellphone': "0924-999999",
'principal_zip_code': "407",
'principal_address': "市政北二路236號26樓之1",
'principal_email': "[email protected]",
'salesman_name': "金城武",
'salesman_phone_code': "886",
'salesman_phone': "02-8888888",
'salesman_cellphone_code': "886",
'salesman_cellphone': "0920-999999",
'salesman_mail': "[email protected]",
'accountant_name': "金城武",
'accountant_phone_code': "886",
'accountant_phone': "02-8888888",
'accountant_fax_code': "886",
'accountant_fax': "02-8888887",
'accountant_cellphone_code': "886",
'accountant_cellphone': "0920-999999",
'accountant_mail': "[email protected]",
'technician_name': "金城武",
'technician_phone_code': "886",
'technician_phone': "02-8888888",
'technician_cellphone_code': "886",
'technician_cellphone': "0920-999999",
'technician_mail': "[email protected]",
'files': {
'principal_id_front_img': {
'name': "身分證正面.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
},
'principal_id_obverse_img': {
'name': "身分證反面.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
},
'principal_driverlicense_healthidcard_img': {
'name': "負責人第二證件.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
},
'approvalletter_img': {
'name': "變更事項登記表.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
},
'report_401_405_img': {
'name': "401報表-108-1.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
},
'business_photos_4_img': [{
'name': "營業照片第一張.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
}, {
'name': "營業照片第二張.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
}, {
'name': "營業照片第三張.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
}, {
'name': "營業照片第四張.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
}],
'bank_passbook_img': {
'name': "營業照片第四張.png",
'size': sampleFile.get('size'),
'type': sampleFile.get('type'),
'data': sampleFile.get('data')
}
}
}
detail.update(company)
"""
return {
'agent_uid': self.agentUid,
'type': 0,
'detail': detail
}
def getService(self):
"""取得服務位置
Returns:
{dict}: 服務位置資料
"""
return {
'service_name': 'store',
'cmd': 'api/authorizedstore'
}
def encrypt(self, fields, key):
"""AES 256 加密
Args:
fields {dict}: 欄位資料
key {bytes}: AES金鑰
Returns:
{string}: 加密資料
"""
data = json.dumps(fields, separators=(',', ':'))
data = Padding.pad(data.encode('utf-8'), AES.block_size)
iv = get_random_bytes(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
data = cipher.encrypt(data)
data = base64.b64encode(iv + data)
return data
def post(self, postData):
"""資料 POST 到主機
Args:
postData {dict}: 欄位資料
Returns:
{string}: JSON資料
"""
result = requests.post(self.url, postData)
return result.text
def getPostData(self):
"""取得送出欄位資料
Returns:
{dict}: 欄位資料
"""
postData = {
'agent_uid': self.agentUid,
'service': self.encrypt(self.getService(), self.agentKey),
'encry_data': self.encrypt(self.getRawData(), self.agentKey)
}
return postData
def run(self):
"""執行
"""
json = self.post(self.getPostData())
print(json)
AgentApplicant = AgentApplicant()
AgentApplicant.run()