Skip to content
Merged
16 changes: 16 additions & 0 deletions src/main/java/co/omise/models/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Source extends Model {
private ChargeStatus chargeStatus;
private String currency;
private String email;
private String ip;
private FlowType flow;
@JsonProperty("installment_term")
private long installmentTerm;
Expand Down Expand Up @@ -89,10 +90,18 @@ public String getEmail() {
return this.email;
}

public String getIp() {
Comment thread
AnasNaouchi marked this conversation as resolved.
return this.ip;
}

public void setEmail(String email) {
this.email = email;
}

public void setIp(String ip) {
Comment thread
AnasNaouchi marked this conversation as resolved.
this.ip = ip;
}

public FlowType getFlow() {
return this.flow;
}
Expand Down Expand Up @@ -217,6 +226,8 @@ public static class CreateRequestBuilder extends RequestBuilder<Source> {
private String currency;
@JsonProperty
private String email;
@JsonProperty
private String ip;
@JsonProperty("installment_term")
private long installmentTerm;
@JsonProperty("mobile_number")
Expand Down Expand Up @@ -282,6 +293,11 @@ public CreateRequestBuilder email(String email) {
return this;
}

public CreateRequestBuilder ip(String ip) {
Comment thread
AnasNaouchi marked this conversation as resolved.
this.ip = ip;
return this;
}

public CreateRequestBuilder installmentTerm(long installmentTerm) {
this.installmentTerm = installmentTerm;
return this;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/co/omise/models/SourceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public enum SourceType {
@JsonProperty("atome")
Atome,
@JsonProperty("duitnow_obw")
DuitNowOBW;
DuitNowOBW,
@JsonProperty("wechat_pay")
WeChatPay;

@Override
public String toString() {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/co/omise/requests/Request.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package co.omise.requests;

import java.io.IOException;

import co.omise.Client;
import co.omise.models.OmiseObjectBase;
import okhttp3.HttpUrl;
import okhttp3.RequestBody;
import okio.Buffer;

/**
* Request is a base class, all extending classes would act as a holder class that encapsulate the information
Expand Down Expand Up @@ -70,6 +73,17 @@ public RequestBody getPayload() {
return payload;
}

/**
* Gets payload (request params).
*
* @return the payload as a readable string
*/
public String getPayloadToString() throws IOException {
Comment thread
AnasNaouchi marked this conversation as resolved.
final Buffer buffer = new Buffer();
payload.writeTo(buffer);
return buffer.readUtf8();
}

public ResponseType<T> getType() {
return type;
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/co/omise/live/LiveChargeRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,36 @@ public void testLiveChargeWithDuitNowOBW() throws IOException, OmiseException {
assertEquals("affin", charge.getSource().getBank());
assertEquals(FlowType.Redirect, charge.getSource().getFlow());
}
@Test
@Ignore("only hit the network when we need to.")
public void testLiveChargeWithWechatPay() throws IOException, OmiseException {

Request<Source> sourceRequest = new Source.CreateRequestBuilder()
.type(SourceType.WeChatPay)
.amount(15000)
.currency("THB")
.ip("127.0.0.1")
.build();

Source source = client.sendRequest(sourceRequest);

Request<Charge> createChargeRequest =
new Charge.CreateRequestBuilder()
.source(source.getId())
.amount(15000)
.currency("THB")
.returnUri("http://example.com/")
.build();

Charge charge = client.sendRequest(createChargeRequest);

System.out.println("created charge: " + charge.getId());

assertNotNull(charge.getId());
assertEquals(15000, charge.getAmount());
assertEquals("THB", charge.getCurrency());
assertEquals("127.0.0.1", charge.getSource().getIp());
assertEquals(SourceType.WeChatPay, charge.getSource().getType());
assertEquals(FlowType.AppRedirect, charge.getSource().getFlow());
}
}
1 change: 1 addition & 0 deletions src/test/java/co/omise/models/SourceTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ public void checkStringValue() {
assertEquals("mobile_banking_ktb", SourceType.MobileBankingKtb.toString());
assertEquals("atome", SourceType.Atome.toString());
assertEquals("duitnow_obw", SourceType.DuitNowOBW.toString());
assertEquals("wechat_pay", SourceType.WeChatPay.toString());
}
}
39 changes: 39 additions & 0 deletions src/test/java/co/omise/requests/SourceRequestTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package co.omise.requests;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import co.omise.models.OmiseException;
import co.omise.models.Source;
import co.omise.models.SourceType;
Expand Down Expand Up @@ -45,4 +48,40 @@ public void testCreateInstallment() throws IOException, OmiseException {
assertEquals("thb", source.getCurrency());
assertEquals("redirect", source.getFlow().toString());
}

@Test
public void testWeChatPaySourceIpMustBeNullWhenEmpty() throws IOException {
Request<Source> request = new TestSourceRequestBuilder()
.type(SourceType.WeChatPay)
.amount(500000)
.currency("thb")
.build();

// The key must exist in the request with a value of null
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(request.getPayloadToString());

assertTrue(rootNode.has("ip"));
assertNull(rootNode.get("ip").textValue());

}

@Test
public void testWeChatPaySourceIpMustHaveValueWhenPassed() throws IOException {
String ip = "127.0.0.1";
Request<Source> request = new TestSourceRequestBuilder()
.type(SourceType.WeChatPay)
.amount(500000)
.ip(ip)
.currency("thb")
.build();

// The key must exist in the request with the passed value
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(request.getPayloadToString());

assertTrue(rootNode.has("ip"));
assertEquals(rootNode.get("ip").textValue(), ip);

}
}