Skip to content

Commit

Permalink
Add wechat_pay source (#81)
Browse files Browse the repository at this point in the history
## Description

Add `wechat_pay` source. More info could be found
[here](https://docs.opn.ooo/wechat-pay-online)

*NOTE:*

* It is important to mention that when creating a `wechat_pay` source,
it is advised to pass the `ip` parameter. This `ip` parameter should be
the ip from the customer's device and not the server's ip.

---------

Co-authored-by: Daniel Fowler <[email protected]>
  • Loading branch information
AnasNaouchi and danfowler authored Feb 14, 2024
1 parent 8ce97ed commit b651484
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Omise.Examples/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ protected async Task<PaymentSource> RetrieveSourceRabbitLinepay()
});
}

// Creates a new PaymentSource called RetrieveSourceWeChatPay, as sources can be created client-side (as well as server-side).
protected async Task<PaymentSource> RetrieveSourceWeChatPay()
{
return await Client.Sources.Create(new CreatePaymentSourceRequest
{
Amount = 2000,
Currency = "thb",
Ip = "127.0.0.1",
Type = OffsiteTypes.WeChatPay,
Flow = FlowTypes.Redirect
});
}

public async Task<PaymentSource> RetrieveSourceTrueMoney()
{
return await Client.Sources.Create(new CreatePaymentSourceRequest
Expand Down
19 changes: 19 additions & 0 deletions Omise.Examples/Examples/Charges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,25 @@ public async Task Create__Create_With_Source_RabbitLinepay()
Console.WriteLine($"created charge: {charge.Id}");
Console.WriteLine($"Redirect customer to: {charge.AuthorizeURI}");
}

#region WeChat Pay
public async Task Create__Create_With_Source_WeChatPay()
{
var source = await RetrieveSourceWeChatPay();
var charge = await Client.Charges.Create(new CreateChargeRequest()
{
Amount = 2000,
Currency = "thb",
Offsite = OffsiteTypes.WeChatPay,
Flow = FlowTypes.Redirect,
Source = source
});

Console.WriteLine($"created charge: {charge.Id}");
Console.WriteLine($"Redirect customer to: {charge.AuthorizeURI}");
}
#endregion

#endregion

#region Wallet Alipay
Expand Down
28 changes: 28 additions & 0 deletions Omise.Examples/Examples/PaymentSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ protected string RetrieveRabbitLinepaySourceId()
}
#endregion

#region WeChat Pay
public async Task Create__Create_WeChatPay()
{
var source = await Client.Sources.Create(new CreatePaymentSourceRequest
{
Amount = 2000,
Currency = "thb",
Ip = "127.0.0.1",
Type = OffsiteTypes.WeChatPay,
Flow = FlowTypes.Redirect
});

Console.WriteLine($"created source: {source.Id}");
}

public async Task Retrieve__Retrieve_WeChatPay()
{
var sourceId = RetrieveWeChatPaySourceId();
var source = await Client.Sources.Get(sourceId);
Console.WriteLine($"source flow is {source.Flow.ToString()}");
}

protected string RetrieveWeChatPaySourceId()
{
return RetrieveSourceWeChatPay().Result.Id;
}
#endregion

#region BillPayment
public async Task Create__Create_BillPayment()
{
Expand Down
6 changes: 6 additions & 0 deletions Omise/Models/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,8 @@ public partial class PaymentSource : ModelBase {
public string StoreName { get; set; }
[JsonProperty("terminal_id")]
public string TerminalId { get; set; }
[JsonProperty("ip")]
public string Ip { get; set; }

public override bool Equals(object obj) {
if (obj == null) return false;
Expand All @@ -985,6 +987,7 @@ public override bool Equals(object obj) {
object.Equals(this.StoreId, another.StoreId) &&
object.Equals(this.StoreName, another.StoreName) &&
object.Equals(this.TerminalId, another.TerminalId) &&
object.Equals(this.Ip, another.Ip) &&
true;
}

Expand Down Expand Up @@ -1033,6 +1036,9 @@ public override int GetHashCode() {
if (TerminalId != default(string)) {
hash = hash * 23 + TerminalId.GetHashCode();
}
if (Ip != default(string)) {
hash = hash * 23 + Ip.GetHashCode();
}

return hash;
}
Expand Down
2 changes: 2 additions & 0 deletions Omise/Models/OffsiteTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public enum OffsiteTypes
MobileBankingOCBCPAO,
[EnumMember(Value = "rabbit_linepay")]
RabbitLinepay,
[EnumMember(Value = "wechat_pay")]
WeChatPay,
[EnumMember(Value = "paynow")]
Paynow,
[EnumMember(Value = "points_citi")]
Expand Down
1 change: 1 addition & 0 deletions Omise/Models/PaymentSourceRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public class CreatePaymentSourceRequest : Request

[JsonProperty("phone_number")]
public string PhoneNumber { get; set; }
public string Ip { get; set; }
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ credit card data through your server directly. **Please read our [Security Best
Practices](https://docs.opn.ooo/security-best-practices) guideline before deploying
production code using this package.**

**Note:** It is important to mention that when creating a `wechat_pay` source, it is advised to pass the `ip` parameter. This `ip` parameter should be the ip from the customer's device and not the server's ip.

### Creating a Charge with a Token

```c#
Expand Down

0 comments on commit b651484

Please sign in to comment.