|
| 1 | +import { PAYMENT_METHOD_OPTIONS } from "../../common/constants.mjs"; |
| 2 | +import ifthenpay from "../../ifthenpay.app.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "ifthenpay-create-payment-reference", |
| 6 | + name: "Create Payment Reference", |
| 7 | + description: "Generates a Multibanco or MB WAY payment reference with a specified amount, entity code, and deadline. [See the documentation](https://ifthenpay.com/docs/en/)", |
| 8 | + version: "0.0.1", |
| 9 | + type: "action", |
| 10 | + props: { |
| 11 | + ifthenpay, |
| 12 | + paymentMethod: { |
| 13 | + type: "string", |
| 14 | + label: "Payment Method", |
| 15 | + description: "The payment method to use for Ifthenpay (e.g., MB WAY, Multibanco)", |
| 16 | + options: PAYMENT_METHOD_OPTIONS, |
| 17 | + reloadProps: true, |
| 18 | + }, |
| 19 | + mbKey: { |
| 20 | + propDefinition: [ |
| 21 | + ifthenpay, |
| 22 | + "mbKey", |
| 23 | + ], |
| 24 | + hidden: true, |
| 25 | + }, |
| 26 | + mbWayKey: { |
| 27 | + propDefinition: [ |
| 28 | + ifthenpay, |
| 29 | + "mbWayKey", |
| 30 | + ], |
| 31 | + hidden: true, |
| 32 | + }, |
| 33 | + orderId: { |
| 34 | + type: "string", |
| 35 | + label: "Order Id", |
| 36 | + description: "Payment identifier defined by the client (e.g., invoice number, order number, etc.)", |
| 37 | + }, |
| 38 | + amount: { |
| 39 | + type: "string", |
| 40 | + label: "Amount", |
| 41 | + description: "The amount to be paid with decimal separator \".\"", |
| 42 | + }, |
| 43 | + mobileNumber: { |
| 44 | + type: "string", |
| 45 | + label: "Mobile Number", |
| 46 | + description: "Place the country code before the mobile number without any spaces (use '#'' to separate the country code from the mobile number - p.e. 351#912345678)", |
| 47 | + hidden: true, |
| 48 | + }, |
| 49 | + description: { |
| 50 | + type: "string", |
| 51 | + label: "Description", |
| 52 | + description: "Description of the payment, with a maximum length of 200 characters", |
| 53 | + optional: true, |
| 54 | + }, |
| 55 | + url: { |
| 56 | + type: "string", |
| 57 | + label: "URL", |
| 58 | + description: "URL address, with a maximum length of 200 characters", |
| 59 | + optional: true, |
| 60 | + hidden: true, |
| 61 | + }, |
| 62 | + clientCode: { |
| 63 | + type: "string", |
| 64 | + label: "Client Code", |
| 65 | + description: "Client's code, with a maximum length of 200 characters", |
| 66 | + optional: true, |
| 67 | + hidden: true, |
| 68 | + }, |
| 69 | + clientName: { |
| 70 | + type: "string", |
| 71 | + label: "Client Name", |
| 72 | + description: "Client's name, with a maximum length of 200 characters", |
| 73 | + optional: true, |
| 74 | + hidden: true, |
| 75 | + }, |
| 76 | + email: { |
| 77 | + type: "string", |
| 78 | + label: "Email", |
| 79 | + description: "The Client's name.", |
| 80 | + optional: true, |
| 81 | + }, |
| 82 | + clientUsername: { |
| 83 | + type: "string", |
| 84 | + label: "Client Username", |
| 85 | + description: "Client's username, with a maximum length of 200 characters", |
| 86 | + optional: true, |
| 87 | + hidden: true, |
| 88 | + }, |
| 89 | + clientPhone: { |
| 90 | + type: "string", |
| 91 | + label: "Client Phone", |
| 92 | + description: "Client's cell phone or phone number, with a maximum length of 200 characters", |
| 93 | + optional: true, |
| 94 | + hidden: true, |
| 95 | + }, |
| 96 | + expiryDays: { |
| 97 | + type: "integer", |
| 98 | + label: "Expiry Days", |
| 99 | + description: "How many days the payment reference is valid for. [See the documentation](https://ifthenpay.com/docs/en/api/multibanco/#tag/multibanco/POST/init) for further details.", |
| 100 | + optional: true, |
| 101 | + hidden: true, |
| 102 | + }, |
| 103 | + }, |
| 104 | + async additionalProps(props) { |
| 105 | + const isMb = this.paymentMethod === "Multibanco"; |
| 106 | + props.mbKey.hidden = !isMb; |
| 107 | + props.mbWayKey.hidden = isMb; |
| 108 | + props.mobileNumber.hidden = isMb; |
| 109 | + props.url.hidden = isMb; |
| 110 | + props.clientCode.hidden = isMb; |
| 111 | + props.clientName.hidden = isMb; |
| 112 | + props.email.label = "Client Email"; |
| 113 | + props.email.description = "Client's email address, with a maximum length of 200 characters"; |
| 114 | + props.clientUsername.hidden = isMb; |
| 115 | + props.clientPhone.hidden = isMb; |
| 116 | + props.expiryDays.hidden = isMb; |
| 117 | + return {}; |
| 118 | + }, |
| 119 | + async run({ $ }) { |
| 120 | + const response = await this.ifthenpay.generatePaymentReference({ |
| 121 | + $, |
| 122 | + paymentMethod: this.paymentMethod, |
| 123 | + data: { |
| 124 | + mbKey: this.mbKey, |
| 125 | + mbWayKey: this.mbWayKey, |
| 126 | + orderId: this.orderId, |
| 127 | + amount: parseFloat(this.amount), |
| 128 | + mobileNumber: this.mobileNumber, |
| 129 | + email: this.email, |
| 130 | + description: this.description, |
| 131 | + url: this.url, |
| 132 | + clientCode: this.clientCode, |
| 133 | + clientName: this.clientName, |
| 134 | + clientUsername: this.clientUsername, |
| 135 | + clientPhone: this.clientPhone, |
| 136 | + expiryDays: this.expiryDays, |
| 137 | + expirationDate: this.expirationDate, |
| 138 | + clientEmail: this.email, |
| 139 | + }, |
| 140 | + }); |
| 141 | + |
| 142 | + $.export("$summary", `Successfully created payment reference with Order ID: ${response.OrderId}`); |
| 143 | + return response; |
| 144 | + }, |
| 145 | +}; |
0 commit comments