Skip to content

Commit f418d70

Browse files
committed
Refund Apis Module is done.
1 parent 7327073 commit f418d70

File tree

2 files changed

+218
-3
lines changed

2 files changed

+218
-3
lines changed

lib/models/response/refund.dart

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// To parse this JSON data, do
2+
//
3+
// final refund = refundFromJson(jsonString);
4+
5+
import 'dart:convert';
6+
7+
Refund refundFromJson(String str) => Refund.fromJson(json.decode(str));
8+
9+
String refundToJson(Refund data) => json.encode(data.toJson());
10+
11+
class Refund {
12+
Refund({
13+
this.id,
14+
this.dateCreated,
15+
this.dateCreatedGmt,
16+
this.amount,
17+
this.reason,
18+
this.refundedBy,
19+
this.refundedPayment,
20+
this.metaData,
21+
this.lineItems,
22+
this.links,
23+
});
24+
25+
int id;
26+
DateTime dateCreated;
27+
DateTime dateCreatedGmt;
28+
String amount;
29+
String reason;
30+
int refundedBy;
31+
bool refundedPayment;
32+
List<dynamic> metaData;
33+
List<dynamic> lineItems;
34+
Links links;
35+
36+
factory Refund.fromJson(Map<String, dynamic> json) => Refund(
37+
id: json["id"],
38+
dateCreated: DateTime.parse(json["date_created"]),
39+
dateCreatedGmt: DateTime.parse(json["date_created_gmt"]),
40+
amount: json["amount"],
41+
reason: json["reason"],
42+
refundedBy: json["refunded_by"],
43+
refundedPayment: json["refunded_payment"],
44+
metaData: List<dynamic>.from(json["meta_data"].map((x) => x)),
45+
lineItems: List<dynamic>.from(json["line_items"].map((x) => x)),
46+
links: Links.fromJson(json["_links"]),
47+
);
48+
49+
Map<String, dynamic> toJson() => {
50+
"id": id,
51+
"date_created": dateCreated.toIso8601String(),
52+
"date_created_gmt": dateCreatedGmt.toIso8601String(),
53+
"amount": amount,
54+
"reason": reason,
55+
"refunded_by": refundedBy,
56+
"refunded_payment": refundedPayment,
57+
"meta_data": List<dynamic>.from(metaData.map((x) => x)),
58+
"line_items": List<dynamic>.from(lineItems.map((x) => x)),
59+
"_links": links.toJson(),
60+
};
61+
}
62+
63+
class Links {
64+
Links({
65+
this.self,
66+
this.collection,
67+
this.up,
68+
});
69+
70+
List<Collection> self;
71+
List<Collection> collection;
72+
List<Collection> up;
73+
74+
factory Links.fromJson(Map<String, dynamic> json) => Links(
75+
self: List<Collection>.from(
76+
json["self"].map((x) => Collection.fromJson(x))),
77+
collection: List<Collection>.from(
78+
json["collection"].map((x) => Collection.fromJson(x))),
79+
up: List<Collection>.from(
80+
json["up"].map((x) => Collection.fromJson(x))),
81+
);
82+
83+
Map<String, dynamic> toJson() => {
84+
"self": List<dynamic>.from(self.map((x) => x.toJson())),
85+
"collection": List<dynamic>.from(collection.map((x) => x.toJson())),
86+
"up": List<dynamic>.from(up.map((x) => x.toJson())),
87+
};
88+
}
89+
90+
class Collection {
91+
Collection({
92+
this.href,
93+
});
94+
95+
String href;
96+
97+
factory Collection.fromJson(Map<String, dynamic> json) => Collection(
98+
href: json["href"],
99+
);
100+
101+
Map<String, dynamic> toJson() => {
102+
"href": href,
103+
};
104+
}

lib/woosignal.dart

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ library woosignal;
1515
// IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1616
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1717

18+
import 'package:flutter/cupertino.dart';
19+
import 'package:woosignal/models/response/refund.dart';
1820
import 'package:woosignal/networking/api_provider.dart';
1921
import 'package:woosignal/helpers/shared_pref.dart';
2022
import 'package:woosignal/models/response/products.dart';
@@ -154,16 +156,15 @@ class WooSignal {
154156
}
155157

156158
/// https://woosignal.com/docs/api/1.0/products#retrive-a-product-api-call
157-
Future<Product> retrieveProduct(
158-
{int id}) async {
159+
Future<Product> retrieveProduct({int id}) async {
159160
Map<String, dynamic> payload = {};
160161

161162
_printLog("Parameters: " + payload.toString());
162163
payload = _standardPayload("get", payload, "products/${id.toString()}");
163164

164165
Product product;
165166
await _apiProvider.post("/request", payload).then((json) {
166-
product = Product.fromJson(json);
167+
product = Product.fromJson(json);
167168
});
168169
_printLog(product.toString());
169170
return product;
@@ -709,4 +710,114 @@ class WooSignal {
709710
_printLog(payloadRsp.toString());
710711
return payloadRsp;
711712
}
713+
714+
// https://woocommerce.github.io/woocommerce-rest-api-docs/?php#refunds
715+
//Refunds
716+
// Create a refund
717+
// This API helps you to create a new refund for an order.
718+
Future<Refund> createRefund({
719+
@required String amount,
720+
@required int orderId,
721+
}) async {
722+
Map<String, dynamic> payload = {};
723+
if (amount != null) payload['amount'] = amount;
724+
725+
_printLog(payload.toString());
726+
payload = _standardPayload(
727+
"post", payload, "orders/${orderId.toString()}/refunds");
728+
Refund refund;
729+
await _apiProvider.post("/request", payload).then((json) {
730+
refund = Refund.fromJson(json);
731+
});
732+
_printLog(refund.toString());
733+
return refund;
734+
}
735+
736+
// Retrieve a refund
737+
// This API lets you retrieve and view a specific refund from an order.
738+
Future<Refund> retrieveRefund({
739+
@required int orderId,
740+
@required int refundId,
741+
String dp,
742+
}) async {
743+
Map<String, dynamic> payload = {};
744+
if (dp != null) payload["dp"] = dp;
745+
_printLog("Parameters: " + payload.toString());
746+
payload = _standardPayload("get", payload,
747+
"orders/${orderId.toString()}/refunds/${refundId.toString()}");
748+
749+
Refund refund;
750+
await _apiProvider.post("/request", payload).then((json) {
751+
refund = Refund.fromJson(json);
752+
});
753+
_printLog(refund.toString());
754+
return refund;
755+
}
756+
757+
// List all refunds
758+
// This API helps you to view all the refunds from an order.
759+
Future<List<Refund>> getRefunds({
760+
String context,
761+
int page,
762+
int perPage,
763+
String search,
764+
String after,
765+
String before,
766+
List<int> exclude,
767+
List<int> include,
768+
int offset,
769+
String order,
770+
String orderby,
771+
List<int> parent,
772+
List<int> parentExclude,
773+
int dp,
774+
@required int orderId,
775+
}) async {
776+
Map<String, dynamic> payload = {};
777+
if (page != null) payload["page"] = page;
778+
if (perPage != null) payload["per_page"] = perPage;
779+
if (search != null) payload["search"] = search;
780+
if (after != null) payload["after"] = after;
781+
if (before != null) payload["before"] = before;
782+
if (exclude != null) payload["exclude"] = exclude;
783+
if (include != null) payload["include"] = include;
784+
if (offset != null) payload["include"] = offset;
785+
if (order != null) payload["order"] = order;
786+
if (orderby != null) payload["orderby"] = orderby;
787+
if (parent != null) payload["code"] = parent;
788+
if (parentExclude != null) payload["code"] = parentExclude;
789+
if (dp != null) payload["code"] = dp;
790+
791+
_printLog("Parameters: " + payload.toString());
792+
payload = _standardPayload(
793+
"get", payload, "orders/${orderId.toString()}/refunds");
794+
795+
List<Refund> refunds = [];
796+
await _apiProvider.post("/request", payload).then((json) {
797+
refunds = (json as List).map((i) => Refund.fromJson(i)).toList();
798+
});
799+
_printLog(refunds.toString());
800+
return refunds;
801+
}
802+
803+
// Delete a refund
804+
// This API helps you delete an order refund.
805+
Future<Refund> deleteRefund({
806+
@required int orderId,
807+
@required int refundId,
808+
@required Map<String, dynamic> data,
809+
}) async {
810+
Map<String, dynamic> payload = data;
811+
812+
_printLog(payload.toString());
813+
payload = _standardPayload("delete", payload,
814+
"orders/${orderId.toString()}/refunds/${refundId.toString()}");
815+
816+
Refund refund;
817+
await _apiProvider.post("/request", payload).then((json) {
818+
refund = Refund.fromJson(json);
819+
});
820+
_printLog(refund.toString());
821+
return refund;
822+
}
712823
}

0 commit comments

Comments
 (0)