CoinPayments (CP)
This Lib make integration with https://www.coinpayments.net in easy way.

Initial setup

Need to setup public and private key:
    1.
    ​Register​
    2.
    Go to this page and generate new key.
Press on button "Edit Permissions" and add API Key Permissions:
Check all options what you need
Then on bot /setup command:
1
// Get your keys in https://www.coinpayments.net/index.php?cmd=acct_api_keys
2
Libs.CoinPayments.setPrivateKey("YOUR KEY");
3
Libs.CoinPayments.setPublicKey('YOUR KEY');
4
​
5
// for Receiving Payments
6
// Get your BB Api Key from Bots.Business App in Profile
7
Libs.CoinPayments.setBBApiKey('YOUR API KEY');
Copied!

Call API methods

All CoinPayments API method available here.
For example for method Get Basic Account Information we need 2 commands: /info and /onInfo
/info command:
1
Libs.CoinPayments.apiCall({
2
fields: { cmd: "get_basic_info"},
3
onSuccess: '/onInfo'
4
});
Copied!
In fields you can pass all fields from CoinPayments api. Just read help.
/onInfo command:
1
Bot.sendMessage(inspect(options));
2
Bot.sendMessage("CoinPayments owner email:" + options.body.result.email);
Copied!

Combine libs!

CoinPayments API do not have some methods. For example get balance by address, validate address, get transactions for address and etc.
Use Block.io Lib with CP Lib together!
Block.io is free if you do not use wallets there.
​

Receiving Payments

See demo bot. Available in the Store.
It is possible to receive payment for a temporary or permanent wallet.
Temporary wallet benefits:
    Fixed amount
    Can bind the payment to the ordered product
    Status and checkout page
    QR code for payment
    one address for one payment
Permanent wallet benefits:
    Any amount
    One address for several payments
    User can transfer funds at any time

Setup: set IPN Secret

The first step is to go to the My Settings page > Merchant Settings and set a IPN Secret.
Your IPN Secret is a string of your choosing. Recommended to be a random string of letters, numbers, and special characters.
CoinPayments will not send any IPNs unless you have an IPN Secret set.
See more​
Once more! You need input any text (random text) as IPN secret in Merchant Settings page

Temporary wallet

We use command "create_transaction" with IPN.
Yes, you can write it via Libs.CoinPayments.apiCallmethod too. But there is an easier way.

​

Command /pay

1
let amount = 0.0001; // amount in BTC
2
​
3
options = {
4
fields: {
5
amount: amount, // amount in BTC
6
currency: "BTC", // currency1 = currency2 = BTC
7
// currency1: "BTC", // The original currency of the transaction
8
// currency2: "LTC" //The currency the buyer will be sending
9
// buyer_email: "[email protected]",
10
// you can use another fields also
11
// except custom and ipn_url (it used by Lib)
12
// See https://www.coinpayments.net/apidoc-create-transaction
13
},
14
// generated wallet, QR code, payment page
15
// will be available in this command
16
onSuccess: '/onCreatePayment',
17
18
// on successful payment this command
19
// will be executed
20
onPaymentCompleted: "/onPaymentCompleted",
21
22
// it is not necessary
23
// onIPN: "/onIPN"
24
25
// if you want customize error messages
26
// onError: "/onError"
27
}
28
​
29
Libs.CoinPayments.createTransaction(options);
Copied!

Automatically with Library:

CoinPayments: IPN Retries / Duplicate IPNs​

It is preferable to use method onPaymentCompleted and not method onIPN.
Since the method onPaymentCompleted completely covers the IPN and solves the problem with IPN Retries / Duplicate IPNs​

Command /onCreatePayment

1
// You can inspect all options:
2
// Bot.sendMessage(inspect(options));
3
​
4
let result = options.result;
5
​
6
let msg = "*Need pay:*\n `" + result.amount + "`" +
7
"\n\n*to address:*\n" +
8
"`" + result.address + "`" +
9
"\n\n [Checkout](" + result.checkout_url +
10
") | [Status](" + result.status_url +
11
")" // you can uncomment this for manual status checking
12
// + "\n\nCheck status manually: /check" + options.payment_index;
13
​
14
Bot.sendMessage(msg);
15
Api.sendPhoto({ photo: result.qrcode_url });
16
​
Copied!

Command /onPaymentCompleted

This command will be executed on successful payment
Need install ResourcesLib
1
// you can inspect all options
2
// Bot.sendMessage(inspect(options));
3
​
4
if(!options){
5
// for security we need to check that this command runned only by lib
6
// user can not run command with options
7
return
8
}
9
​
10
Bot.sendMessage("Payment completed");
11
​
12
let amount = options.amount1;
13
​
14
let res = Libs.ResourcesLib.userRes("balance");
15
res.add(amount)
16
​
17
Bot.sendMessage("added to balance, BTC: " + amount);
Copied!

Finished!

Now you can receive payments
​

Information about extra payments and refunds.

It is possible to define buyer email in fields for options (see code for command /pay )
Please note: CoinPayments do not send you extra payments. CoinPayments send just email to buyer (or you) about refund for that extra.
Also no any information about such extra in CP Panel or IPN. Be careful
​

Additional Information

You can check payment status
1
Libs.CoinPayments.getTxInfo({
2
payment_index: payment_index, // see /onCreatePayment command.
3
// Need pass this payment_index to this command
4
onSuccess: '/on_txn_id'
5
})
Copied!

command: /on_txn_id:

1
// You can inspect all options:
2
// Bot.sendMessage(inspect(options));
3
​
4
Bot.sendMessage(options.result.status_text);
5
​
6
// Do not finish payment here.
7
// Use /onPaymentCompleted for this
Copied!

command /onIPN

You can get info from IPN. Really it is not needed in simple. Just use onPaymentCompleted option on createTransaction.
1
// You can inspect all fields:
2
// Bot.sendMessage(inspect(options))
3
​
4
// IPN is not needed
5
// Use - onPaymentCompleted
6
​
7
Bot.sendMessage("IPN: Payment status: " + options.status_text );
8
​
Copied!

command onError

1
// You can inspect all fields:
2
Bot.sendMessage(inspect(options))
Copied!

​

Permanent wallet

We use command "get_callback_address" with IPN.
Yes, you can write it via Libs.CoinPayments.apiCallmethod too. But there is an easier way.
​

Command /createWallet

1
Libs.CoinPayments.createPermanentWallet({
2
currency: "BTC",
3
//label: "myLabel",
4
onSuccess: "/onWalletCreate",
5
6
// onIPN - not necessary
7
//onIPN: "/onPermanentWalletIPN",
8
9
onIncome: "/onIncome"
10
11
// if you want customize error messages
12
// onError: "/onError"
13
});
Copied!

Automatically with Library:

CoinPayments: IPN Retries / Duplicate IPNs​

It is preferable to use method onIncome and not method onIPN.
Since the method onIncome completely covers the IPN and solves the problem with IPN Retries / Duplicate IPNs​

Command /onWalletCreate

1
//Bot.sendMessage(inspect(options));
2
​
3
let wallet = options.result.address;
4
Bot.sendMessage("Your permanent wallet address is:\n`" + wallet + "`")
5
​
6
// You can save wallet
7
//User.setProperty("wallet", wallet, "string");
Copied!

Command /onIncome

1
// anybody can run /onIncome command!
2
​
3
if(!options){
4
// for security we need to check that this command runned only by lib
5
// user can not run command with options
6
return
7
}
8
​
9
let wallet = options.address;
10
let currency = options.currency;
11
let amount = options.amount;
12
​
13
let fiat_amount = options.fiat_amount;
14
let fiat_currency = options.fiat_coin;
15
​
16
let fee = options.fee;
17
​
18
let txn_id = options.txn_id
19
​
20
// see another fields by
21
// Bot.sendMessage(inspect(options));
22
​
23
Bot.sendMessage(
24
"*Income to wallet:*" +
25
"\n`"+ wallet + "`" +
26
"\n\n*Amount*:\n" +
27
amount + " " + currency + " (" + fiat_amount + " " + fiat_currency + ")" +
28
"\n*Fee*: " + fee +
29
"\n\nTXN: `" + txn_id + "`"
30
);
31
​
Copied!
​

command onError

1
// You can inspect all fields:
2
Bot.sendMessage(inspect(options))
Copied!
​
​

Troubleshooting & Debuging

    Do not use same CoinPayment account for receiving and transfering funds.
    Go to page. This list must have history with completed income transaction(s)
    Try to resend IPN - see Debuging
    Verify that you have set IPN secret​

IPN History

You can view IPN History by link https://www.coinpayments.net/acct-ipn-history​
Also you can resend IPN by checkin "Resend" checkbox and button "Re-send checked IPN(s)"

Test methods

Temporary wallet:

Also it is possible make test onPaymentCompleted event. It is good if you do not want make test payment.
1
options = {
2
onPaymentCompleted: "/onPaymentCompleted 0.75"
3
}
4
​
5
Libs.CoinPayments.callTestPaymentCompleted(options);
Copied!

​

Permanent wallet:

Also it is possible make test callTestPermanentWalletIncome event. It is good if you do not want make test payment.
1
options = {
2
// onIPN: "/onPermanentWalletIPN", // if you need IPN also
3
onIncome: "/onIncome",
4
5
// not necessary options
6
// you can pass amount
7
//amount: 0.5
8
// txn_id: YOUR_TXN_ID
9
}
10
​
11
Libs.CoinPayments.callTestPermanentWalletIncome(options);
Copied!
​

Security

It is strongly recommended to pay attention to safety when using this library.
Do not use default names for secure commands such as /onIncome, /onPaymentCompleted
Anybody can run any command by names. So need to check that security command runned by CoinPayment Lib only!
1
if(!options){
2
// for security we need to check that this command runned only by lib
3
// user can not run command with options
4
return
5
}
6
​
7
// your secure code
8
...
Copied!
Do not use any non official libs now.
    Any lib can run command with options.
    Any libs can read properties (and read your API Keys from other lib)
We have not way to protect this now. Just not use NON official libs with CP lib. Well, that now there are no such libraries
​
Grant only the really necessary permissions for the Api key. If yor bot do not need "create_withdrawal" or etc methods - turn theys off.
Check all options what you need
Read more about security here​

Do not process any before payment!

Any API commands can have errors: API errors, timeout errors, network errors, and other errors.
You need to withdraw, make deposit income only on success!
Because if you get an error, such a conclusion, replenishment of the deposit, etc. - will always be!
Here the balance will always be replenished, even if half of the Internet falls
​
Last modified 5mo ago