Skip to content

Commit

Permalink
Merge pull request #7 from sarmadka/main
Browse files Browse the repository at this point in the history
Added cancelSubscription method.
  • Loading branch information
sarmadka authored Oct 15, 2023
2 parents 29df2cd + a9fdcc8 commit 38c806e
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 40 deletions.
7 changes: 2 additions & 5 deletions Apis/billing_portal_session.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
}

func createBillingPortalSession(
key: String, customerId : String ,returnUrl: CharsPtr, verbose: Bool
key: String, customerId: CharsPtr, returnUrl: CharsPtr, verbose: Bool
): Possible[String] {
if items.getLength() == 0 {
return Possible[String].failure(Errors.invalidParams());
}
def parameters: String = String.format(
"customer=%s&return_url=%s"،
customerId.buf,
Net.uriEncode(customerId).buf,
Net.uriEncode(returnUrl).buf
);
return createBillingPortalSession(key, parameters, verbose);
Expand Down
33 changes: 11 additions & 22 deletions Apis/customer.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,22 @@

func doesCustomerHaveDefaultPaymentMethod(key: String, customerId: String, verbose: Bool): Bool {
def customer: Possible[SrdRef[Customer]] = getCustomer(key, customerId, verbose);
if customer and customer.defaultPaymentMethod != String("null")
if customer and customer.defaultPaymentMethod != ""
return true;
return false;
}

func addCustomerDefaultPaymentMethod(key: String, customerId: String, paymentMethodId: String, verbose: Bool): Bool {
def parameters: String = "invoice_settings[default_payment_method]=";
parameters = parameters + paymentID;
def b: Bool = true;
def customerId: String;
def request: Net.Request(
String.format("https://api.stripe.com/v1/customers/%s", customerId.buf),
func addCustomerDefaultPaymentMethod(
key: String, customerId: String, paymentMethodId: String, verbose: Bool
): SrdRef[Error] {
def parameters: String = String("invoice_settings[default_payment_method]=") + paymentMethodId;
def res: Possible[String] = postRecord(
key,
String("Bearer"),
String("application/x-www-form-urlencoded")
String.format("https://api.stripe.com/v1/customers/%s", customerId.buf),
parameters,
verbose
);
request.verbose = verbose;
request.post(parameters.buf);
if request.responseHttpStatus == 401 {
return false;
} else if request.responseHttpStatus == 400 {
return false;
} else if request.responseHttpStatus == 0 {
return false;
} else if request.responseHttpStatus != 200 {
return false;
}
return true;
if not res return res.error
else return SrdRef[Error]();
}
}
15 changes: 11 additions & 4 deletions Apis/subscription.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
return Possible[Array[SrdRef[Subscription]]].failure(Errors.unexpected());
}
def subscriptionsJson: Json = request.responseBody;
if subscriptionsJson("object") == String("search_result") {
if subscriptionsJson("object")~cast[String] == "search_result" {
def i: Int;
for i = 0, i < subscriptionsJson("data").getLength(), i += 1 {
subscriptions.add(convertToSubscription(subscriptionsJson("data")(i)));
}
Expand All @@ -39,9 +40,9 @@
return Possible[Array[SrdRef[Subscription]]].success(subscriptions);
}

func getSubscription(key: String, checkoutId: String, verbose: Bool): Possible[SrdRef[Subscription]] {
func getSubscription(key: String, subscriptionId: String, verbose: Bool): Possible[SrdRef[Subscription]] {
return getRecord[Subscription, convertToSubscription](
key, "https://api.stripe.com/v1/subscriptions", checkoutId, verbose
key, "https://api.stripe.com/v1/subscriptions", subscriptionId, verbose
);
}

Expand Down Expand Up @@ -70,7 +71,7 @@
def metadata: Map[String, String];
def i: Int;
for i = 0, i < subscriptionJson("metadata").getLength(), ++i {
metadata[subscriptionJson("metadata").getKey(i)] = subscriptionJson("metadata")(i);
metadata(subscriptionJson("metadata").getKey(i)) = subscriptionJson("metadata")(i);
}
return SrdRef[Subscription]().{
alloc()~init(
Expand Down Expand Up @@ -101,4 +102,10 @@
verbose
);
}

func cancelSubscription(key: String, subscriptionId: String, verbose: Bool): SrdRef[Error] {
return deleteRecord[Subscription, convertToSubscription](
key, "https://api.stripe.com/v1/subscriptions", subscriptionId, verbose
);
}
}
11 changes: 8 additions & 3 deletions Stripe.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import "Models/PaymentMethod";
import "Utils/getRecord";
import "Utils/getRecords";
import "Utils/postRecord";
import "Utils/deleteRecord";
import "Apis/customer";
import "Apis/checkout_session";
import "Apis/balance_transaction";
Expand Down Expand Up @@ -69,7 +70,7 @@ import "Errors";
return Stripe.doesCustomerHaveDefaultPaymentMethod(this.key, customerId, this.verbose);
}

handler this.addCustomerDefaultPaymentMethod(customerId: String, paymentMethodId: String): Bool {
handler this.addCustomerDefaultPaymentMethod(customerId: String, paymentMethodId: String): SrdRef[Error] {
return Stripe.addCustomerDefaultPaymentMethod(this.key, customerId, paymentMethodId, this.verbose);
}

Expand Down Expand Up @@ -135,7 +136,7 @@ import "Errors";
return Stripe.createBillingPortalSession(this.key, parameters, this.verbose);
}

handler this.createBillingPortalSession(customerId: String, returnUrl: CharsPtr): Possible[String] {
handler this.createBillingPortalSession(customerId: CharsPtr, returnUrl: CharsPtr): Possible[String] {
return Stripe.createBillingPortalSession(this.key, customerId, returnUrl, this.verbose);
}

Expand Down Expand Up @@ -164,7 +165,11 @@ import "Errors";
handler this.updateSubscription(subscriptionId: String, parameters: String): Possible[String] {
return Stripe.updateSubscription(this.key, subscriptionId, parameters, this.verbose);
}


handler this.cancelSubscription(subscriptionId: String): SrdRef[Error] {
return Stripe.cancelSubscription(this.key, subscriptionId, this.verbose);
}

// PaymentMethod APIs

handler this.getPaymentMethods(customerId: String): Possible[Array[SrdRef[PaymentMethod]]] {
Expand Down
19 changes: 19 additions & 0 deletions Utils/deleteRecord.alusus
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@merge module Stripe {
func deleteRecord [RecordType: type, convertor: function] (
key: String, url: CharsPtr, id: CharsPtr, verbose: Bool
): SrdRef[Error] {
def request: Net.Request(String.format("%s/%s", url, id), key, String("Bearer"));
request.verbose = verbose;
request.delete();
if request.responseHttpStatus == 401 {
return Errors.unauthenticated();
} else if request.responseHttpStatus == 404 {
return Errors.notFound();
} else if request.responseHttpStatus == 0 {
return Errors.connection();
} else if request.responseHttpStatus != 200 {
return Errors.unexpected();
}
return SrdRef[Error]();
}
}
24 changes: 20 additions & 4 deletions readme.ar.md
Original file line number Diff line number Diff line change
Expand Up @@ -696,13 +696,13 @@ handler this.doesCustomerHaveDefaultPaymentMethod(customerId: String): Bool;
#### أضف_طريقة_دفع_مبدئية_للزبون (addCustomerDefaultPaymentMethod)

```
عملية هذا.أضف_طريقة_دفع_مبدئية_للزبون(معرف_الزبون: نـص، معرف_طريقة_الدفع: نـص): ثـنائي؛
عملية هذا.أضف_طريقة_دفع_مبدئية_للزبون(معرف_الزبون: نـص، معرف_طريقة_الدفع: نـص): سـندنا[خـطأ]؛
```

<div dir=ltr>

```
handler this.addCustomerDefaultPaymentMethod(customerId: String, paymentMethodId: String): Bool;
handler this.addCustomerDefaultPaymentMethod(customerId: String, paymentMethodId: String): SrdRef[Error];
```

</div>
Expand Down Expand Up @@ -799,7 +799,7 @@ handler this.createBillingPortalSession(parameters: String): Possible[String]

```
عملية هذا.أنشئ_جلسة_تحكم_بالفوترة(
معرف_الزبون: نص،
معرف_الزبون: مـؤشر_محارف،
رابط_الرجوع: مـؤشر_محارف
): لـا_مضمون[نـص]
```
Expand All @@ -808,7 +808,7 @@ handler this.createBillingPortalSession(parameters: String): Possible[String]

```
handler this.createBillingPortalSession(
customerId:String,
customerId:CharsPtr,
returnUrl: CharsPtr
): Possible[String]
```
Expand Down Expand Up @@ -1048,6 +1048,22 @@ handler this.updateSubscription(subscriptionId: String, parameters: String): Pos

`معطيات` (`parameters`) معطيات الاشتراك المطلوبة بالصيغة التالية: "customer=customerID&line_items=planID".

#### ألغ_اشتراكا (cancelSubscription)

```
عملية هذا.ألغ_اشتراكا(معرف_الاشتراك: نـص): سـندنا[خـطأ]؛
```

<div dir=ltr>

```
handler this.cancelSubscription(subscriptionId: String): SrdRef[Error];
```

</div>

تلغي الاشتراك ذا المعرف المعطى.

#### هات_طرق_الدفع (getPaymentMethods)

```
Expand Down
12 changes: 10 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ Returns 1 if the customer with the given ID has a default payment method.
#### addCustomerDefaultPaymentMethod

```
handler this.addCustomerDefaultPaymentMethod(customerId: String, paymentMethodId: String): Bool;
handler this.addCustomerDefaultPaymentMethod(customerId: String, paymentMethodId: String): SrdRef[Error];
```

Assigns the payment method with the given ID as the customer's default payment method. Returns 1 on
Expand Down Expand Up @@ -545,6 +545,14 @@ Updates the subscription having the given ID.
`parameters`: The parameters to assign to this subscription. It should have the following format:
`customer=customerID&line_items=planID`.

#### cancelSubscription

```
handler this.cancelSubscription(subscriptionId: String): SrdRef[Error];
```

Cancels the subscription having the given ID.

#### createBillingPortalSession

```
Expand All @@ -559,7 +567,7 @@ Returns customer account url.

```
handler this.createBillingPortalSession(
customerId:String,
customerId: CharsPtr,
returnUrl: CharsPtr
): Possible[String]
```
Expand Down
1 change: 1 addition & 0 deletions سـترايب.أسس
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
عرف هات_اشتراكا: لقب getSubscription؛
عرف أنشئ_اشتراكا: لقب createSubscription؛
عرف حدث_اشتراكا: لقب updateSubscription؛
عرف ألغ_اشتراكا: لقب cancelSubscription؛
عرف هات_طرق_الدفع: لقب getPaymentMethods؛
عرف هات_طريقة_دفع: لقب getPaymentMethod؛
}
Expand Down

0 comments on commit 38c806e

Please sign in to comment.