比特币 coinbase

tech2022-10-02  106

比特币 coinbase

Have you ever thought about selling your services in exchange for Bitcoins? It’s not so strange – today, many big players are actually doing it. From OkCupid to KhanAcademy, even WordPress is accepting Bitcoin. Also, some countries are thinking about it as a currency. Today, we will see how to accept Bitcoin payments on your website/application in an easy way, with the Coinbase API (and its SDK).

您是否考虑过出售服务以换取比特币? 并不奇怪-今天,许多大型企业正在这样做。 从OkCupid到KhanAcademy,甚至WordPress都接受比特币。 另外, 一些国家正在考虑将其作为货币 。 今天,我们将了解如何使用Coinbase API(及其SDK)以一种简便的方式在您的网站/应用程序上接受比特币付款。

Coinbase SDK (Coinbase SDK)

Coinbase has some interesting tools and SDKs available for everyone, with some pricing conditions that are convenient and affordable.

Coinbase为每个人提供了一些有趣的工具和SDK,并提供了一些方便且价格合理的定价条件 。

Here’s what you have to know about pricing:

关于定价,您需要了解以下内容:

Receiving payments with Coinbase is free;

使用Coinbase进行付款是免费的; You will have to pay a fee (1%) only if you want to transfer money on your bank account, but only if your sales exceed $1000000 (yes, one milion dollars);

仅当您要在银行帐户上转帐时,才需要支付费用(1%),但前提是您的销售额超过1000000美元(是,一百万美元); The smallest payment you can do in the Bitcoin network is 0.001 BTC. Using a Coinbase wallet, however, that limit goes down to 1 Satoshi (0.00000001 BTC);

您可以在比特币网络中进行的最小付款是0.001 BTC。 但是,使用Coinbase钱包时,该限制下降到1 Satoshi(0.00000001 BTC);

Another important (and interesting) thing is that you can choose to activate the “Instant Exchange” service, which converts a Bitcoin payment amount immediately into a currency of your choice without additional steps, automatically. Really cool if you want to avoid the currency instability and transfer your money as soon as you can.

另一个重要(且有趣)的事情是,您可以选择激活“即时交换”服务,该服务将自动将比特币支付金额立即转换为您选择的货币,而无需执行其他步骤。 如果您想避免货币不稳定并尽快转账,那真是太酷了。

That said… how do we implement this integration?

就是说……我们如何实现这种整合?

整合类型 (Integration Types)

Just like many other online payment services, Coinbase offers two main ways of integration into your web app. The first one is faster and easier. The second one is a little more difficult, but also goes into more depth and is adequate for a bigger project.

就像许多其他在线支付服务一样,Coinbase提供了两种主要方式集成到您的Web应用程序中。 第一个更快,更轻松。 第二个要困难一点,但也要深入一些,足以应付更大的项目。

The first integration type consists of using one of the Merchant Tools that Coinbase makes available. You can use buttons, pages and frames. If you are using a CMS or an e-commerce (WordPress, WooCommerce, Magento…) there are many plugins for your favorite one.

第一种集成类型包括使用Coinbase提供的一种Merchant工具 。 您可以使用按钮 , 页面和框架 。 如果您使用的是CMS或电子商务(WordPress,WooCommerce,Magento等),则有许多您最喜欢的插件。

The second, of which we will see an example today, is a complete integration of the service without going to the Coinbase website to generate some standard button code. In fact, we will use the specific PHP SDK to do it.

第二个是我们今天将看到的一个示例,它是服务的完全集成,而无需访问Coinbase网站来生成一些标准按钮代码。 实际上,我们将使用特定PHP SDK来实现。

What can we do with this SDK? The dedicated page on Coinbase is clear:

这个SDK有什么用? Coinbase上的专用页面很清楚:

sell or buy bitcoins (or exchange with your currency);

出售或购买比特币(或与您的货币进行兑换); send or request bitcoins via email or bitcoin address;

通过电子邮件或比特币地址发送或请求比特币; accept bitcoin payments as a merchant;

接受比特币付款作为商人; store your bitcoins in one or more wallets;

将您的比特币存储在一个或多个钱包中; have access to bitcoin raw network data (blocks, transactions and so on…);

有权访问比特币原始网络数据(区块,交易等); handle micro-payments and recurring payments;

处理小额付款和定期付款;

A little bit of everything.

每样东西都来一点。

At this moment, there are three SDKs available: for Ruby, Java and PHP. There are also many unofficial libraries for other languages (Python, .NET, Node…). As I told you before, the SDK that we will use is the PHP SDK, which you can find on GitHub.

目前,有三个可用的SDK:Ruby,Java和PHP。 还有许多其他语言(Python,.NET,Node…)的非官方库。 如前所述,我们将使用的SDK是PHP SDK,您可以在GitHub上找到它 。

Note: before you go to the next step, I will presume that you are able to create an account on Coinbase, or you already have one.

注意:在进行下一步之前,我假设您可以在Coinbase上创建一个帐户,或者您已经拥有一个。

PHP SDK (The PHP SDK)

安装 (Installation)

Let’s start with the package installation for our project. If you take a look at the GitHub page of the SDK you will find nothing about doing it with Composer. However, simple searching will easily yield the packagist page of the coinbase/coinbase package.

让我们从项目的软件包安装开始。 如果您查看SDK的GitHub页面,则不会发现使用Composer进行此操作。 然而,简单的搜索很容易产生packagist页面中的coinbase/coinbase包。

You can install it with

您可以使用安装

{ "require": { "coinbase/coinbase": "dev-master" } }

in your composer.json file and a

在您的composer.json文件和一个

composer update

认证方式 (Authentication)

Before starting with the code let’s talk about authentication. Coinbase has two ways to authenticate you as a developer in order to access the API methods. You can choose a simple API Key access, with an API Secret to have more security. Or, if you want, you can use OAuth 2. The difference is not only about complexity, but also about the situation you have to deal with.

在开始编写代码之前,让我们谈谈身份验证。 Coinbase有两种方法可以验证您作为开发人员的身份,以便访问API方法。 您可以选择简单的API密钥访问权限,并选择“ API机密”以提高安全性。 或者,如果需要,您可以使用OAuth2。区别不仅在于复杂性,还在于您必须处理的情况。

The Coinbase documentation is quite clear: if you want to use your account and make changes to it, you can use the API Key system. If you want to let the user use his account through your app (a client you build, for example), the best thing is to use OAuth.

Coinbase文档非常清楚:如果您想使用您的帐户并对其进行更改,则可以使用API​​密钥系统。 如果要让用户通过您的应用程序(例如,您构建的客户端)使用其帐户,最好的方法是使用OAuth。

API Key + Secret

API密钥+秘密

Creating an API Key is really simple, once you have an account on Coinbase. All you have to do is to go here and click on “+ New API Key”.

在Coinbase上拥有一个帐户后,创建API密钥真的非常简单。 所有您需要做的就是去这里 ,点击“ + New API Key”。

For your first time, you will probably have to confirm your account with Authy.

您可能第一次需要通过Authy确认您的帐户。

The screen you’ll see will be one like this:

您将看到的屏幕如下所示:

You will have to specify the account you want to use and what permissions you want to assign to that specific key, in order to gain access. Also, you can choose one or more IPs to use as a whitelist. If you don’t specify anything, there will be no whitelist.

为了获得访问权限,您将必须指定要使用的帐户以及要分配给该特定密钥的权限。 另外,您可以选择一个或多个IP用作白名单。 如果您不指定任何内容,则不会有白名单。

After that, confirm the procedure by clicking on “Create” and click on “Enable” when you want to activate the API Key.

之后,单击“创建”以确认该过程,然后在要激活API密钥时单击“启用”。

OAuth 2.0

OAuth 2.0

If you want to use OAuth 2.0 you will have to follow a similar procedure. This time, however, you will not create an API Key but an OAuth application. To do that, go to https://coinbase.com/oauth/applications. From there, click on “+ Create an Application”. You will see a screen like this one:

如果要使用OAuth 2.0,则必须遵循类似的步骤。 但是,这一次,您将不会创建API密钥,而是会创建OAuth应用程序。 为此,请转到https://coinbase.com/oauth/applications 。 从那里,单击“ +创建应用程序”。 您将看到类似这样的屏幕:

Insert your application name, choose an icon if you want and specify a list of URLs for future redirect operations.

插入您的应用程序名称,如果需要,选择一个图标,并指定URL列表以用于将来的重定向操作。

Note: every URL you are going to insert must use SSL (https://...). Everything else will be ignored.

注意:您要插入的每个URL必须使用SSL( https://... )。 其他所有内容都将被忽略。

Give your OK and you are done! You will see a confirmation message with your new ClientID and Client Secret. Things are not over, however: you can read other notes about authentication from the dedicated page.

给您确定,您就完成了! 您将看到一条确认消息,其中包含新的ClientID和Client Secret。 但是,事情还没有结束:您可以从专用页面上阅读有关身份验证的其他说明。

权限 (Permissions)

If you work with the API you will have to deal with permissions, for better security. Here you can see a complete list:

如果使用API​​,则必须处理权限,以提高安全性。 在这里您可以看到完整的列表:

all: complete access to your account;

全部:完全访问您的帐户; merchant: create payment buttons, forms, access to basic informations about the merchant, edit your info and create new addresses;

商家:创建付款按钮,表格,访问有关商家的基本信息,编辑您的信息并创建新地址; balance: access to your actual balance;

余额:获取您的实际余额; buttons: create payment buttons;

按钮:创建付款按钮; buy: buy bitcoins;

购买:购买比特币; contacts: get a list of your contacts;

联系人:获取您的联系人列表; orders: get a list of your received orders;

订单:获取收到的订单清单; sell: sell bitcoins;

出售:出售比特币; transactions: get a history of your transactions;

交易:获取交易记录; send: send a certain amount of bitcoins from your account;

发送:从您的帐户发送一定数量的比特币; request: request bitcoins from your account;

请求:从您的帐户中请求比特币; transfers: get the history of buys and sells;

转移:获取买卖历史; recurring_payments: get a list of recurring payments;

recurring_payments:获取定期付款清单; oauth_apps: see, create and edit OAuth applications;

oauth_apps:查看,创建和编辑OAuth应用程序; reports: get and create new reports;

报告:获取并创建新报告;

基本的SDK使用 (Basic SDK Use)

Now that we have our SDK included in our project, our access set up, and we know what we need, it is time to start.

现在我们已经在项目中包含了我们的SDK,已经设置了访问权限,并且知道我们需要什么,现在该开始了。

First of all, let’s take a look at the implementation of the authentication procedure.

首先,让我们看一下认证过程的实现。

Access via API Key and Secret

通过API密钥和机密访问

Nothing complex, just a single instruction.

没什么复杂的,只有一条指令。

$coinbase = Coinbase::withApiKey($coinbaseAPIKey, $coinbaseAPISecret);

Just use the API Key and the API Secret as parameters for the withApiKey() method. The resulting $coinbase object is the one that we’ll use for future examples.

只需使用API​​ Key和API Secret作为withApiKey()方法的参数withApiKey() 。 生成的$coinbase对象是我们将在以后的示例中使用的对象。

Access via OAuth

通过OAuth访问

After the OAuth application creation the next thing to do is to create a $coinbaseOauth object, specifying the Client ID and the Client Secret that we got previously. Just like this example:

创建OAuth应用程序之后,下一步是创建一个$coinbaseOauth对象,指定我们之前获得的客户端ID和客户端密钥。 就像这个例子:

$coinbaseOauth = new Coinbase_OAuth($_CLIENT_ID, $_CLIENT_SECRET, $_REDIRECT_URL); header("Location: " . $coinbaseOauth->createAuthorizeUrl("all"));

After the user completes the authorization process he will be redirected to the URL specified before, during the setup. A code parameter will also be added to get a valid token.

用户完成授权过程后,将在设置过程中将其重定向到之前指定的URL。 还将添加一个code参数以获得有效的令牌。

$tokens = $coinbaseOauth->getTokens($_GET['code']);

Then, the last step will be the creation of the $coinbase object using the token we now have.

然后,最后一步将是使用我们现在拥有的令牌创建$coinbase对象。

$coinbase = Coinbase::withOauth($coinbaseOauth, $tokens);

You can use this object in the same way you can use the one you got from the API Key authentication, the API is the same.

您可以使用与使用从API密钥身份验证获得的对象相同的方法来使用该对象,API是相同的。

Accessing your Data

存取资料

Accessing to your data is quite easy. All you have to do is to use the $coinbase variable set before.

访问您的数据非常容易。 您要做的就是使用之前设置的$coinbase变量。

Let’s see how to check your balance:

让我们看看如何检查余额:

echo $coinbase->getBalance() . " BTC";

Here you can see how to access user data:

在这里,您可以查看如何访问用户数据:

$user = $coinbase->getUser() echo $user->name; echo $user->email; echo $user->time_zone; echo $user->native_currency;

Using the $coinbase object you can also gain access to some merchant properties.

使用$coinbase对象,您还可以访问某些商人属性。

$user->merchant->company_name; $user->merchant->logo;

Note: the logo property returns the logo URL.

注意: logo属性返回徽标URL。

Also, if you want, you can get a list of your contacts using getContacts().

另外,如果需要,您可以使用getContacts()获得联系人列表。

$response = $coinbase->getContacts("user"); foreach($response->contacts as $contact) { echo $contact; // 'guyaddress@provider.com' }

Currency Data

货币数据

With this PHP SDK you can also gain access to some data about currencies.

使用此PHP SDK,您还可以访问有关货币的一些数据。

$currencies = $coinbase->getCurrencies(); echo $currencies[0]->name;

The getCurrencies() method returns a list of all the currencies actually active on the system, with their ISO codes.

getCurrencies()方法返回系统上实际激活的所有货币及其ISO代码的列表。

Here’s how to get some information about the exchange rates:

以下是获取有关汇率的一些信息的方法:

$rates = $coinbase->getExchangeRate(); echo $rates->btc_to_usd; // is the same as... echo $coinbase->getExchangeRate('btc', 'usd');

The getExchangeRate() can be used both with and without parameters (with two different results, as you can see).

getExchangeRate()可以与参数一起使用,也可以不与参数一起使用(如您所见,具有两个不同的结果)。

You can also get some information about the buy and sell prices with getBuyPrice() and getSellPrice():

您还可以使用getBuyPrice()和getSellPrice()获得有关买卖价格的一些信息:

echo $coinbase->getBuyPrice('1'); // '125.31' echo $coinbase->getSellPrice('1'); // '122.41'

Note: the returned price includes the 1% Coinbase fee and the $0.15 bank one.

注意:返回的价格包括1%的Coinbase费用和$ 0.15的银行手续费。

Create a Payment Button

创建一个付款按钮

The payment button creation method is really useful, if you consider the implementation difficulty and the final result. All you have to do is to call the createButton() method with a specific set of parameters.

如果您考虑实现难度和最终结果,那么付款按钮创建方法真的很有用。 您所要做的就是使用一组特定的参数调用createButton()方法。

Here’s the signature:

这是签名:

createButton($name, $price, $currency, $custom=null, $options=array())

… and here’s an example.

……这是一个例子。

$paymentButton = $coinbase->createButton( "Order #1", "19.99", "EUR", "TRACKING_CODE_1", array( "description" => "1 item at 19.99" ) );

Quite easy, huh?

很容易,对吧?

The first $name parameter is the “title” for the payment you want to create. The next one, $price, is the amount of the payment you want. The third one is the currency you want to use, and $custom is a specific transaction code that will be sent back to you after the payment procedure.

第一个$name参数是您要创建的付款的“标题”。 下一个$price是您想要的付款金额。 第三个是您要使用的货币, $custom是特定的交易代码,将在付款过程后发送回给您。

Finally, the $options array will let you customize your button in every way: you will be able to customize the button appearance or the return URL after a successful payment, or a failed one. If you want to know more about that, I suggest you to take a look at the dedicated page on the official documentation.

最后, $options数组将使您以各种方式自定义按钮:成功付款后或失败后,您将能够自定义按钮外观或返回URL。 如果您想了解更多信息,建议您看一下官方文档中的专用页面 。

Once you have the button it’s easy to get its embed code.

有了按钮后,就很容易获得其嵌入代码。

echo $response->button->code; // '93865b9cae83706ae59220c013bc0afd' echo $response->embedHtml; // '<div class=\"coinbase-button\" data-code=\"93865b9cae83706ae59220c013bc0afd\"></div><script src=\"https://coinbase.com/assets/button.js\" type=\"text/javascript\"></script>'

Then, you will have nothing to do but echo the $response->embedHtml.

然后,您只需要回显$response->embedHtml 。

结语 (Wrapping up)

This concludes part 1 in which we covered the basic usage and installation of the Coinbase API SDK. In part 2, we’ll cover sending and receiving money, as well as build our sample application. Stay tuned!

到第1部分为止,我们涵盖了Coinbase API SDK的基本用法和安装。 在第2部分中,我们将介绍如何收发资金,以及构建示例应用程序。 敬请关注!

翻译自: https://www.sitepoint.com/bitcoin-php-coinbases-api-basic-usage/

比特币 coinbase

最新回复(0)