Skip to content

Instantly share code, notes, and snippets.

@donma
Created October 18, 2024 08:45
Show Gist options
  • Save donma/24ad5072cb210dee476db0c36ff93189 to your computer and use it in GitHub Desktop.
Save donma/24ad5072cb210dee476db0c36ff93189 to your computer and use it in GitHub Desktop.
private static string TestPaymentUrl
{
get
{
return "https://payment-api.testing.oen.tw/";
}
}
private static string ProductPaymentUrl
{
get
{
return "https://payment-api.oen.tw/";
}
}
public string MerchantId { get; set; }
public string AuthToken { get; set; }
/// <summary>
// 使用信用卡付款
/// </summary>
/// <param name="orderId"></param>
/// <param name="amount"></param>
/// <param name="cardNo"></param>
/// <param name="cardExp"></param>
/// <param name="cardCVV"></param>
/// <param name="cardUserName"></param>
/// <param name="prodId"></param>
/// <param name="prodName"></param>
/// <param name="prodQvt"></param>
/// <param name="prodUnit"></param>
/// <param name="userName"></param>
/// <param name="userEmail"></param>
/// <param name="testMode"></param>
/// <returns></returns>
public PayByCreditCardResponse PayByCreditCard(string orderId, int amount,
string cardNo, string cardExp, string cardCVV, string cardUserName,
string prodId, string prodName, int prodQvt, string prodUnit,
string userName = "", string userEmail = "",
bool testMode = true)
{
var transationUrl = TestPaymentUrl;
if (!testMode)
{
transationUrl = ProductPaymentUrl;
}
try
{
var client = new RestClient(transationUrl);
// 創建 POST 請求
var request = new RestRequest("transactions", Method.Post);
// 設置 JSON 數據
var transData = new
{
merchantId = MerchantId,
amount = amount,
cardNum = cardNo,
cardExp = cardExp,
cardCVV = cardCVV,
cardName = cardUserName,
orderId = orderId,
productDetails = new
[] {
new {
productionCode=prodId,
description= prodName,
quantity =prodQvt,
unit=prodUnit,
unitPrice= amount,
}
}
,
userName = userName,
userEmail = userEmail
};
// 添加 JSON Body 到請求中
request.AddJsonBody(transData);
request.AddHeader("Authorization", "Bearer " + AuthToken);
request.AddHeader("Content-Type", "application/json");
// 發送請求並接收回應
var response = client.ExecuteAsync(request).Result;
return JsonConvert.DeserializeObject<PayByCreditCardResponse>(response.Content);
}
catch (Exception ex)
{
throw ex;
}
}
public class PayByCreditCardData
{
public string id { get; set; }
public string transactionId { get; set; }
public string authCode { get; set; }
}
public class PayByCreditCardResponse
{
public string code { get; set; }
public PayByCreditCardData data { get; set; }
public string message { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment