Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
hot fix Paypal
Browse files Browse the repository at this point in the history
  • Loading branch information
k-nero committed Nov 6, 2023
1 parent cb65056 commit a9b065b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
83 changes: 77 additions & 6 deletions src/SWD-Laundry-Backend.Core/ValueObject/PaypalApiObjectModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class PaypalOrder

public readonly struct PaypalOrderResponse
{
public string create_time { get; init; }
public DateTime? create_time { get; init; }
public string id { get; init; }
public string update_time { get; init; }
public DateTime? update_time { get; init; }
public string processing_instruction { get; init; }
public PaypalLink[] links { get; init; }
public string intent { get; init; }
Expand All @@ -28,26 +28,51 @@ public readonly struct PaypalOrderResponse
public readonly struct PaypalOrderCaptureResponse
{
public string id { get; init; }
public PaymentSource? payment_source { get; init; }
public string status { get; init; }
public string create_time { get; init; }
public string update_time { get; init; }
public DateTime? create_time { get; init; }
public DateTime? update_time { get; init; }
public PaypalLink[] links { get; init; }
public PaypalPayer payer { get; init; }
public PaypalPayer? payer { get; init; }
public PaypalPurchaseUnit[] purchase_units { get; init; }
}

public readonly struct ShippingDetail
{
public PayerName name { get; init; }
public string type { get; init; }
public PayerAdress address { get; init; }

}

public readonly struct PaymentSource
{
public PaymentSourcePaypal? paypal { get; init; }
}

public readonly struct PaypalPayer
{
public string email_address { get; init; }
public string payer_id { get; init; }
public string address { get; init; }
public PayerAdress address { get; init; }
public PayerName name { get; init; }
}

public readonly struct PayerAdress
{
public string address_line_1 { get; init; }
public string address_line_2 { get; init; }
public string admin_area_2 { get; init; }
public string admin_area_1 { get; init; }
public string postal_code { get; init; }
public string country_code { get; init; }
}

public readonly struct PayerName
{
public string given_name { get; init; }
public string surname { get; init; }
public string? full_name { get; init; }
}

public readonly struct PaypalLink
Expand Down Expand Up @@ -78,6 +103,9 @@ public readonly struct PaymentSourcePaypal
public string? billing_agreement_id { get; init; }
public string? vault_id { get; init; }
public string? email_address { get; init; }
public string? account_id { get; init; }
public string? account_status { get; init; }
public PayerAdress? address { get; init; }

}

Expand All @@ -102,6 +130,49 @@ public readonly struct PaypalPurchaseUnit
public string? soft_descriptor { get; init; }
public PaypalItem[]? items { get; init; }
public PaypalAmountRequest amount { get; init; }
public ShippingDetail? shipping { get; init; }
public Payment? payments { get; init; }
}

public readonly struct Payment
{
public object[]? authorizations { get; init; }
public object[]? refunds { get; init; }
public Capture[]? captures { get; init; }

}

public readonly struct Capture
{
public string status { get; init; }
public object? status_details { get; init; }
public string id { get; init; }
public string? invoice_id { get; init; }
public string? custom { get; init; }
public bool? final_capture { get; init; }
public string? disbursement_mode { get; init; }
public PaypalLink[]? links { get; init; }
public Amount? amount { get; init; }
public DateTime? create_time { get; init; }
public DateTime? update_time { get; init; }
public PaypalSellerProtection? seller_protection { get; init; }
public PaypalSellerReceivableBreakdown? seller_receivable_breakdown { get; init; }
public object? processor_response { get; init; }
}

public readonly struct PaypalSellerReceivableBreakdown
{
public Amount? gross_amount { get; init; }
public Amount? paypal_fee { get; init; }
public Amount? net_amount { get; init; }
public Amount? total_refunded_amount { get; init; }
public Amount? total_charged_back_amount { get; init; }
}

public readonly struct PaypalSellerProtection
{
public string? status { get; init; }
public string[]? dispute_categories { get; init; }
}

public readonly struct PaypalAmountRequest
Expand Down
22 changes: 16 additions & 6 deletions src/SWD-Laundry-Backend.Service/Services/PaypalService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public PaypalService(ITransactionService transactionService, IWalletService wall
client.DefaultRequestHeaders.Add("Accept-Language", "en_US");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token.Value.access_token);
var response = await client.PostAsync(paypal_api_url, new StringContent("", Encoding.UTF8, "application/json"), cancellationToken);

var responseString = await response.Content.ReadAsStringAsync(cancellationToken);
Transaction? transaction;
if (response.IsSuccessStatusCode)
{
var responseString = await response.Content.ReadAsStringAsync(cancellationToken);

var result = JsonConvert.DeserializeObject<PaypalOrderCaptureResponse>(responseString);

var transactionId = result.purchase_units[0].reference_id;
Expand Down Expand Up @@ -71,7 +71,7 @@ public PaypalService(ITransactionService transactionService, IWalletService wall
}
}

return null;
throw new Exception(responseString);
}

public async Task<PaypalOrderResponse> CreatePaypalOrderAsync(TransactionModel model, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -99,7 +99,7 @@ public async Task<PaypalOrderResponse> CreatePaypalOrderAsync(TransactionModel m
{
amount = new PaypalAmountRequest()
{
currency_code = "VND",
currency_code = "USD",
value = model.Amount.ToString()
},
reference_id = transId,
Expand All @@ -114,7 +114,12 @@ public async Task<PaypalOrderResponse> CreatePaypalOrderAsync(TransactionModel m
var result = JsonConvert.DeserializeObject<PaypalOrderResponse>(responseString);
if (result.id == null)
{
throw new Exception("There is something wrong, cannot create order. Paypal URL: " + paypal_api_url);
var rs = JsonConvert.DeserializeObject<object>(responseString);
if (rs != null)
{
throw new Exception(rs.ToString());
}
throw new Exception("Unknow error, cannot create order. Paypal URL: " + paypal_api_url);
}
return result;
}
Expand Down Expand Up @@ -142,7 +147,12 @@ public async Task<PaypalOrderResponse> CreatePaypalOrderAsync(TransactionModel m
var result = JsonConvert.DeserializeObject<PaypalAccessTokenResponse>(responseString);
if(result.access_token == null)
{
throw new Exception("There is something wrong, cannot get access token. ClientId " + client_id + " - ClientSecret: " + client_secret + " - Paypal URL: " + paypal_api_url);
var rs = JsonConvert.DeserializeObject<object>(responseString);
if (rs != null)
{
throw new Exception(rs.ToString());
}
throw new Exception("Unknow error, cannot get access token");
}
return result;
}
Expand Down

0 comments on commit a9b065b

Please sign in to comment.