Skip to content

Commit

Permalink
Merge pull request #4 from cborac/master
Browse files Browse the repository at this point in the history
Refactoring and few fixes
  • Loading branch information
0x178F authored Jan 9, 2024
2 parents bb2d76e + 8d8a3d4 commit 9351d41
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ shopier.setOrderShipping({
#### For 15₺:

```javascript
const paymentPage = shopier.payment(15);
const paymentPage = shopier.generatePaymentHTML(15);
```

> This will return the purchase form as html.
Expand Down
5 changes: 5 additions & 0 deletions src/enums/currencyTypes.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum CurrencyType {
TL,
USD,
EUR
}
31 changes: 25 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
ICallback
} from './interfaces';
import { PlatformType, ProductType } from './enums';
import { deprecate } from 'util';
import { CurrencyType } from './enums/currencyTypes.enum';

export class Shopier {
private paymentUrl: string =
Expand All @@ -16,7 +18,7 @@ export class Shopier {
private buyer: IBuyer = {} as IBuyer;
private orderBilling: IBillingAddress = {} as IBillingAddress;
private orderShipping: IShippingAddress = {} as IShippingAddress;
private currency: string = 'TRY';
private currency: CurrencyType = CurrencyType.TL;
private moduleVersion: string = '1.0.4';

constructor(apiKey: string, apiSecret: string) {
Expand Down Expand Up @@ -91,7 +93,18 @@ export class Shopier {
.join('');
}

/**
* @deprecated Use `generatePaymentHTML(amount: number)` instead
*/
payment(amount: number): string {
deprecate(
this.payment,
'payment(amount: number) is deprecated. Use generatePaymentHTML(amount: number) instead.'
);
return this.generatePaymentHTML(amount);
}

generatePaymentHTML(amount: number): string {
const obj = this.generateIForm(amount);
return `<!doctype html>
<html lang="en">
Expand All @@ -113,8 +126,11 @@ export class Shopier {
</html>`;
}

setCurrency(currency: string) {
this.currency = currency;
setCurrency(currency: keyof typeof CurrencyType): this;
setCurrency(currency: CurrencyType): this;
setCurrency(currency: CurrencyType | keyof typeof CurrencyType) {
this.currency =
typeof currency === 'number' ? currency : CurrencyType[currency];
return this;
}

Expand All @@ -127,9 +143,10 @@ export class Shopier {
return current_lan;
}

callback(body: any, apiSecret: string): ICallback | boolean {
const data = body.random_nr + body.platform_order_id;
const hmac = createHmac('sha256', apiSecret);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback(body: any): ICallback | false {
const data = `${body.random_nr}${body.platform_order_id}`;
const hmac = createHmac('sha256', this.apiSecret);
hmac.update(data);
const expected = hmac.digest('base64');
if (body.signature === expected) {
Expand All @@ -147,3 +164,5 @@ export class Shopier {
}
}
}

export * from './enums';
2 changes: 1 addition & 1 deletion src/interfaces/form.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface IForm extends IBuyer, IShippingAddress, IBillingAddress {
API_key: string;
website_index: number;
total_order_value: number;
currency: string;
currency: number;
platform: number;
is_in_frame: number;
current_language: number;
Expand Down

0 comments on commit 9351d41

Please sign in to comment.