/
invoice.aspx.cs
135 lines (125 loc) · 5.71 KB
/
invoice.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using pay2go.Models;
using SpGatewayHelper.Helper;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Specialized;
public partial class invoive : System.Web.UI.Page
{
//https://ccore.spgateway.com/MPG/mpg_gateway
//測試平台https://cinv.pay2go.com/API/invoice_issue
// 正式平台https://inv.pay2go.com/API/invoice_issue
public string PostData;
public string Sha;
public string aes;
public string MerchantID;
public string action;
protected void Page_Load(object sender, EventArgs e)
{
OrderLib.OrderData o = new OrderLib.OrderData();
o = OrderLib.Get_ordData(Request.QueryString["ord_code"]);
action = "https://inv.pay2go.com/API/invoice_issue";//正式ID
action = "https://cinv.pay2go.com/API/invoice_issue";//測試ID
MerchantID = ""; //正式ID
MerchantID = "31117495";//測試ID
string _key = "";
_key = "8hWZNZjoHkNDTTgOXGYJI4Rc5k3OR7CO";
string _Vi = "";
_Vi = "oaAyJlX2BcpSuj8m";
DateTime gtm = new DateTime(1970, 1, 1);//宣告一個GTM時間出來
DateTime utc = DateTime.UtcNow.AddHours(8);//宣告一個目前的時間
int timeStamp = Convert.ToInt32(((TimeSpan)utc.Subtract(gtm)).TotalSeconds);
int TaxAmt = 0;
string ItemName = "";
string ItemCount = "";
string ItemUnit = "";
string ItemPrice = "";
string ItemAmt = "";
string ItemTaxType = "";
int i = 0;
foreach (var p in o.OrderDetail)
{
ItemName += i == 0 ? p.P_name : "|" + p.P_name;
ItemCount += i == 0 ? p.Num.ToString() : "|" + p.Num.ToString();
ItemUnit += i == 0 ? "個" : "|個";
ItemPrice += i == 0 ? p.Price.ToString() : "|" + p.Price.ToString();
TaxAmt += Convert.ToInt16(Math.Ceiling(p.Amount * 0.05));
ItemAmt += i == 0 ? p.Amount.ToString() : "|" + p.Amount.ToString();
ItemTaxType += i == 0 ? "1" : "|1";
i++;
}
var pay2goInfo = new pay2goInfo()
{
RespondType = "JSON",
TimeStamp = timeStamp.ToString(),
Version = "1.4",
BuyerName = o.Ordname,
BuyerAddress = o.Shipaddress,
BuyerEmail = o.Ordemail,
BuyerPhone = o.Ordphone,
MerchantOrderNo = timeStamp.ToString(),//o.Ord_code ,
Category = "B2C",
TaxType = "1",
TaxRate = 5,
Amt = o.TotalPrice - TaxAmt,
TaxAmt = TaxAmt,
TotalAmt = o.TotalPrice,
CarrierType = "",
CarrierNum = Server.HtmlDecode(""),
LoveCode = "",
PrintFlag = "Y",
ItemName = ItemName,
ItemCount = ItemCount,
ItemUnit = ItemUnit,
ItemPrice = ItemPrice,
ItemAmt = ItemAmt,
ItemTaxType = ItemTaxType,
Comment = "TEST,備註說明",
Status = "1",
CreateStatusTime = ""
};
var postData = MyHelper.ToDictionary(pay2goInfo);
var cryptoHelper = new CryptoHelper(_key, _Vi);
var aesString = cryptoHelper.GetAesString(postData);
aes = aesString;
Sha = cryptoHelper.GetSha256String(aesString);
string URI = action;
string myParameters = "MerchantID_=" + MerchantID + "&PostData_=" + aes;
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
JObject restoredObject = JsonConvert.DeserializeObject<JObject>(HtmlResult);
JObject result = JsonConvert.DeserializeObject<JObject>(restoredObject["Result"].ToString ());
Response.Write(HtmlResult);
string strsql = @"INSERT INTO InvoiceNumner(InvoiceTransNo, MerchantOrderNo, TotalAmt,
InvoiceNumber, RandomNum, CheckCode, CreateTime,
BarCode, QRcodeL, QRcodeR)
VALUES (@InvoiceTransNo, @MerchantOrderNo, @TotalAmt, @InvoiceNumber, @RandomNum, @CheckCode, @CreateTime,
@BarCode, @QRcodeL, @QRcodeR)";
NameValueCollection nvc = new NameValueCollection
{
{ "CheckCode", result["CheckCode"].ToString () },
{ "MerchantOrderNo", result["MerchantOrderNo"].ToString () },
{ "InvoiceNumber", result["InvoiceNumber"].ToString () },
{ "TotalAmt", result["TotalAmt"].ToString () },
{ "InvoiceTransNo", result["InvoiceTransNo"].ToString () },
{ "RandomNum", result["RandomNum"].ToString () },
{ "CreateTime", result["CreateTime"].ToString () },
{ "BarCode", result["BarCode"].ToString () },
{ "QRcodeL", result["QRcodeL"].ToString () },
{ "QRcodeR", result["QRcodeR"].ToString ()}
};
if (result != null) { DbControl.Data_add (strsql, nvc);}
//Dictionary<string, string> results = ((IDictionary<string, JToken>)(JObject)parsed["moretests"]).ToDictionary(pair => pair.Key, pair => (string)pair.Value);
Response.Write(restoredObject["Status"]);
Response.Write(restoredObject["Message"]);
}
}
}