Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper routes to create charge with token #61

Open
phieronymus opened this issue Sep 10, 2020 · 2 comments
Open

Add helper routes to create charge with token #61

phieronymus opened this issue Sep 10, 2020 · 2 comments
Assignees
Labels
Enhancement New feature or request
Milestone

Comments

@phieronymus
Copy link
Collaborator

We are currently repeating the charge creation process boilerplate:

  • Create a token
  • Create a charge
  • Cleanup token in case the charge fails

The idea here is to create some helper function to handle the whole process for it instead of having everyone repeating the same creation flow.

We may as well add a function for the other kind of payment if needed at some point.

@phieronymus phieronymus added the Enhancement New feature or request label Sep 10, 2020
@phieronymus
Copy link
Collaborator Author

phieronymus commented Sep 16, 2020

Here is the miniapp code to do it 🙂 (thanks Julian)

/**
 * Charge
 */
univapay
    .createTransactionToken(OnlineGateway.ALIPAY_ONLINE, username, metadata(authorizer, items), {
        jwt,
        secret
    })
    .then(({ id: transactionTokenId, storeId }) =>
        univapay
            .createCharge(
                transactionTokenId,
                amount(items),
                {
                    ...metadata(authorizer, items),
                    ...(transaction?.title && {
                        internal_payments_transaction_name: transaction.title
                    })
                },
                { jwt, secret }
            )
            .then(({ id, storeId }) => {...})
            .catch(err =>
                // otherwise there will be some latency before you can create another one for the app token
                univapay
                    .deleteTransactionToken(storeId, transactionTokenId, { jwt, secret })
                    .then(() => Promise.reject(err))
            )


/**
 * Online
 */
univapay
    .createTransactionToken(OnlineGateway.ALIPAY_ONLINE, username, metadata(authorizer, items), {
        jwt,
        secret
    })
    .then(({ id: transactionTokenId, storeId }) =>
        univapay
            .createCharge(
                transactionTokenId,
                amount(items),
                {
                    ...metadata(authorizer, items),
                    ...(transaction?.title && {
                        internal_payments_transaction_name: transaction.title
                    })
                },
                { jwt, secret }
            )
            .then(({ id, storeId }) =>
                univapay.getIssuerToken(storeId, id, { jwt, secret }).then(
                    tap(() =>
                        lbb.addUnivaId(
                            items[0].detail_id,
                            items[0].store_id,
                            id,
                            null,
                            username,
                            token,
                            {
                                host,
                                app: APP
                            }
                        )
                    )
                )
            )
            .then(({ issuerToken, callMethod }) => ({
                statusCode: 200,
                body: { order, items, charge: { issuerToken, callMethod } }
            }))
            .catch(err =>
                // otherwise there will be some latency before you can create another one for the app token
                univapay
                    .deleteTransactionToken(storeId, transactionTokenId, { jwt, secret })
                    .then(() => Promise.reject(err))
            )

@perrin4869
Copy link
Collaborator

@perrin4869 perrin4869 added this to the 1.0.0 milestone Jan 12, 2021
@perrin4869 perrin4869 self-assigned this Jan 12, 2021
@perrin4869 perrin4869 modified the milestones: 1.0.0, 1.1.0 Feb 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants