Skip to content

Commit

Permalink
Merge pull request #8 from sarmadka/main
Browse files Browse the repository at this point in the history
Added support for fetching promo codes.
  • Loading branch information
sarmadka authored Dec 15, 2024
2 parents ca458f3 + 76640a1 commit 42d089f
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Apis/checkout_session.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
key: String, items: Map[String, Int], customerId: String, successUrl: CharsPtr, verbose: Bool
): Possible[String] {
if items.getLength() == 0 {
return Possible[String].failure(Errors.invalidParams());
return Possible[String].failure(Errors.invalidParam(String("items")));
}
def parameters: String;
if customerId != String() {
Expand Down
33 changes: 33 additions & 0 deletions Apis/promotion_code.alusus
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@merge module Stripe {
func getPromotionCodes(key: String, code: String, verbose: Bool): Possible[Array[SrdRef[PromotionCode]]] {
return getRecords[PromotionCode, convertToPromotionCode](
key, String("https://api.stripe.com/v1/promotion_codes?code=") + code, -1, String(), verbose
);
}

func convertToPromotionCode(promotionCodeJson: Json): SrdRef[PromotionCode] {
return SrdRef[PromotionCode]().{
alloc()~init(
promotionCodeJson("id"),
promotionCodeJson("active"),
promotionCodeJson("code"),
promotionCodeJson("livemode"),
convertToCoupon(promotionCodeJson("coupon"))
)
};
}

func convertToCoupon(couponJson: Json): SrdRef[Coupon] {
return SrdRef[Coupon]().{
alloc()~init(
couponJson("id"),
couponJson("name"),
couponJson("duration"),
couponJson("duration_in_months"),
couponJson("amount_off"),
couponJson("percent_off"),
couponJson("currency")
)
};
}
}
2 changes: 1 addition & 1 deletion Apis/subscription.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
key: String, items: Map[String, Int], customerId: String, verbose: Bool
): Possible[String] {
if items.getLength() == 0 {
return Possible[String].failure(Errors.invalidParams());
return Possible[String].failure(Errors.invalidParam(String("items")));
}
def parameters: String = String.format("customer=%s", customerId.buf);
def i: Int;
Expand Down
21 changes: 17 additions & 4 deletions Errors.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,23 @@
return GenericError.new(NOT_FOUND, NOT_FOUND_MSG);
}

def INVALID_PARAMS: "stripe_invalid_params";
def INVALID_PARAMS_MSG: "Invalid request parameters";
func invalidParams(): SrdRef[GenericError] {
return GenericError.new(INVALID_PARAMS, INVALID_PARAMS_MSG);
def INVALID_PARAM: "stripe_invalid_param";
def INVALID_PARAM_MSG: "Invalid request parameter";
func invalidParam(paramName: String): SrdRef[InvalidParamError] {
return InvalidParamError.new(paramName);
}

class InvalidParamError {
@injection def error: GenericError;
def paramName: String;

func new(pn: String): SrdRef[InvalidParamError] {
return SrdRef[InvalidParamError].construct().{
code = INVALID_PARAM;
message = String.format("%s. Param: %s", INVALID_PARAM_MSG, pn.buf);
paramName = pn;
}
}
}
}
}
31 changes: 31 additions & 0 deletions Models/Coupon.alusus
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@merge module Stripe {
class Coupon {
def id: String;
def name: Nullable[String];
def duration: String;
def durationInMonths: Nullable[String];
def amountOff: Nullable[Int];
def percentOff: Nullable[Float];
def currency: Nullable[String];

handler this~init() {}

handler this~init(
id: String,
name: Nullable[String],
duration: String,
durationInMonths: Nullable[String],
amountOff: Nullable[Int],
percentOff: Nullable[Float],
currency: Nullable[String]
) {
this.id = id;
this.name = name;
this.duration = duration;
this.durationInMonths = durationInMonths;
this.amountOff = amountOff;
this.percentOff = percentOff;
this.currency = currency;
}
}
}
25 changes: 25 additions & 0 deletions Models/PromotionCode.alusus
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@merge module Stripe {
class PromotionCode {
def id: String;
def active: Bool;
def code: String;
def livemode: Bool;
def coupon: SrdRef[Coupon];

handler this~init() {}

handler this~init(
id: String,
active: Bool,
code: String,
livemode: Bool,
coupon: SrdRef[Coupon]
) {
this.id = id;
this.active = active;
this.code = code;
this.livemode = livemode;
this.coupon = coupon;
}
}
}
9 changes: 9 additions & 0 deletions Stripe.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import "Models/Customer";
import "Models/CheckoutSession";
import "Models/Subscription";
import "Models/PaymentMethod";
import "Models/Coupon";
import "Models/PromotionCode";
import "Utils/getRecord";
import "Utils/getRecords";
import "Utils/postRecord";
Expand All @@ -32,6 +34,7 @@ import "Apis/balance";
import "Apis/billing_portal_session";
import "Apis/subscription";
import "Apis/payment_method";
import "Apis/promotion_code";
import "Errors";

@merge module Stripe {
Expand Down Expand Up @@ -182,5 +185,11 @@ import "Errors";
handler this.getPaymentMethod(paymentMethodId: String): Possible[SrdRef[PaymentMethod]] {
return Stripe.getPaymentMethod(this.key, paymentMethodId, this.verbose);
}

// PromotionCode APIs

handler this.getPromotionCodes(code: String): Possible[Array[SrdRef[PromotionCode]]] {
return Stripe.getPromotionCodes(this.key, code, this.verbose);
}
}
}
3 changes: 2 additions & 1 deletion Utils/postRecord.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
if request.responseHttpStatus == 401 {
return Possible[String].failure(Errors.unauthenticated());
} else if request.responseHttpStatus == 400 {
return Possible[String].failure(Errors.invalidParams());
def json: Json = request.responseBody;
return Possible[String].failure(Errors.invalidParam(json("error")("param")));
} else if request.responseHttpStatus == 0 {
return Possible[String].failure(Errors.connection());
} else if request.responseHttpStatus != 200 {
Expand Down
140 changes: 140 additions & 0 deletions readme.ar.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,112 @@ class Subscription {
`معرف_الزبون` (`customerId`) معرف الزبون الخاص بعملية الاشتراك.


### قـسيمة (Coupon)

يحمل بيانات قسيمة ترويج واحدة.

```
صنف قـسيمة {
عرف المعرف: نـص؛
عرف الاسم: بـعدم[نـص]؛
عرف المدة: نـص؛
عرف المدة_بالأشهر: بـعدم[نـص]؛
عرف قيمة_التخفيض: بـعدم[صـحيح]؛
عرف نسبة_التخفيض: بـعدم[عـائم]؛
عرف العملة: بـعدم[نـص]؛
عملية هذا~هيئ()؛
عملية هذا~هيئ(
المعرف: نـص،
الاسم: بـعدم[نـص]،
المدة: نـص،
المدة_بالأشهر: بـعدم[نـص]،
قيمة_التخفيض: بـعدم[صـحيح]،
نسبة_التخفيض: بـعدم[عـائم]،
العملة: بـعدم[نـص]
}
```

<div dir=ltr>

```
class Coupon {
def id: String;
def name: Nullable[String];
def duration: String;
def durationInMonths: Nullable[String];
def amountOff: Nullable[Int];
def percentOff: Nullable[Float];
def currency: Nullable[String];
handler this~init();
handler this~init(
id: String,
name: Nullable[String],
duration: String,
durationInMonths: Nullable[String],
amountOff: Nullable[Int],
percentOff: Nullable[Float],
currency: Nullable[String]
);
}
```

</div>


### رمـز_ترويج (PromotionCode)

يحمل بيانات رمز ترويج واحد.

```
صنف قـسيمة {
عرف المعرف: نـص؛
عرف مفعل: ثـنائي؛
عرف الرمز: نـص؛
عرف النمط_الحي: ثـنائي؛
عرف القسيمة: سـندنا[قـسيمة]؛
عملية هذا~هيئ()؛
عملية هذا~هيئ(
المعرف: نـص،
مفعل: ثـنائي،
الرمز: نـص،
النمط_الحي: ثـنائي،
القسيمة: سـندنا[قـسيمة]
}
```

<div dir=ltr>

```
class PromotionCode {
def id: String;
def active: Bool;
def code: String;
def livemode: Bool;
def coupon: SrdRef[Coupon];
handler this~init() {}
handler this~init(
id: String,
active: Bool,
code: String,
livemode: Bool,
coupon: SrdRef[Coupon]
);
}
```

</div>


### وكـيل (Client)

يحتوي هذا الصنف كل وظائف التواصل مع سترايب. يُهيأ باستخدام مفتاح الواجهة البرمجية المُقدم من سترايب (API key):
Expand Down Expand Up @@ -1096,6 +1202,22 @@ handler this.getPaymentMethod(paymentMethodId: String): Possible[SrdRef[PaymentM

ترجع طريقة الدفع ذات المعرف المعطى.

#### هات_رموز_الترويج (getPromotionCodes)

```
عملية هذا.هات_رموز_الترويج(كود: نـص): لـا_مضمون[مـصفوفة[سـندنا[رمـز_ترويج]]]؛
```

<div dir=ltr>

```
handler this.getPromotionCodes(code: String): Possible[Array[SrdRef[PromotionCode]]];
```

</div>

ترجع رموز الترويج المطابقة للكود المعطى.


### أخـطاء (Errors)

Expand All @@ -1105,5 +1227,23 @@ handler this.getPaymentMethod(paymentMethodId: String): Possible[SrdRef[PaymentM
* `أخـطاء._اتصال_` (`Errors.CONNECTION`): تُرجع عند فشل الاتصال بخوادم سترايب.
* `أخـطاء._غير_متوقع_` (`Errors.UNEXPECTED`): تُرجع عند استلام رد غير متوقع من سترايب.
* `أخـطاء._غير_موجود_` (`Errors.NOT_FOUND`): تُرجع عند الفشل في العثور على القيد ذي المعرف المعطى.
* `أخـطاء._معطى_خاطئ_` (`Errors.INVALID_PARAM`): تُرجع عند فشل العملية بسبب معطى غير صالح.

```
صنف خـطأ_معطى_خاطئ {
عرف اسم_المعطى: نـص؛
}
```

<div dir=ltr>

```
class InvalidParamError {
def paramName: String;
}
```

</div>

</div>

Loading

0 comments on commit 42d089f

Please sign in to comment.