brianhenryie / bh-php-bitcoinpostageinfo
BitcoinPostage.info API
dev-master
2023-12-05 06:59 UTC
Requires
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-05 08:39:05 UTC
README
BitcoinPostage.info PHP API
一个针对BitcoinPostage.info的薄PHP包装器 – 一个销售USPS、UPS和FedEx邮票并接受比特币和门罗币作为支付方式的网站/API。
composer require brianhenryie/bh-php-bitcoinpostageinfo
查看
使用
使用PSR HTTP实现创建API类的实例。Guzzle是最受欢迎的。
$httpFactory = new \GuzzleHttp\Psr7\HttpFactory(); $client = new \GuzzleHttp\Client(); $bitcoinPostageInfo = new \BrianHenryIE\BitcoinPostageInfo\BitcoinPostageInfoAPI( requestFactory: $httpFactory, streamFactory: $httpFactory, client: $client, );
所有API调用都需要认证
$credentials = new \BrianHenryIE\BitcoinPostageInfo\Model\Request\Credentials( key: $_ENV[ 'BITCOINPOSTAGEINFO_API_KEY' ], secret: $_ENV[ 'BITCOINPOSTAGEINFO_API_SECRET' ], );
向您的账户充值
$chargeCreditsRequest = new \BrianHenryIE\BitcoinPostageInfo\Model\Request\ChargeCreditsRequest( credentials: $credentials, amount: '10.00' ); /** @var \BrianHenryIE\BitcoinPostageInfo\Model\Response\ChargeCredits $chargeCreditsResponse */ $chargeCreditsResponse = $api->chargeCredits($chargeCreditsRequest); $bitcoinAddressHref = 'bitcoin:' . $chargeCreditsResponse->address . '?amount=' . $chargeCreditsResponse->amount; $bitcoinQrCode = 'https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=' . $bitcoinAddressHref . '&choe=UTF-8'; printf( '<a href="%s" target="_blank"><img src="%s"/><br/>Pay BitcoinPostage.info for $%s credits.</a>', $bitcoinAddressHref, $bitcoinQrCode, $chargeCreditsResponse->credits );
检查您的账户余额
$credits = $api->getCredits($credentials); echo $credits->credits;
您需要您的出发和目的地地址以及包裹尺寸才能获取运费并购买标签
$fromAddress = new \BrianHenryIE\BitcoinPostageInfo\Model\Address( name: 'Brian Henry', street: '800 N St.', street2: null, city: 'Sacramento', state: 'CA', zip: '95814', country: 'US', phone: null, ); $toAddress = new \BrianHenryIE\BitcoinPostageInfo\Model\Address( name: 'Brian Henry', street: '1 Palace St', street2: 'Apartment 3', city: 'Dublin', state: 'Dublin', zip: 'D02 XR57', country: 'IE', phone: null, ); $dimensions = new \BrianHenryIE\BitcoinPostageInfo\Model\Dimensions( weightLbs: 0, weightOz: 1.5, heightInches: 1.0, widthInches: 2.0, depthInches: 3.0, );
查询运费
$ratesRequest = new \BrianHenryIE\BitcoinPostageInfo\Model\Request\GetRatesRequest( credentials: $credentials, fromAddress: $fromAddress, toAddress: $toAddress, service: new \BrianHenryIE\BitcoinPostageInfo\Model\Service( packageType: UspsPackage::FLAT_RATE_ENVELOPE, ), dimensions: $dimensions, ); /** @var \BrianHenryIE\BitcoinPostageInfo\Model\Response\Rate[] $ratesResponse */ $ratesResponse = $api->getRates($ratesRequest); $cheapest_rate = array_reduce( $ratesResponse, function (\BrianHenryIE\BitcoinPostageInfo\Model\Response\Rate $carry, \BrianHenryIE\BitcoinPostageInfo\Model\Response\Rate $rate) { return is_null($carry) || $rate->rate < $carry->rate ? $rate : $carry; }, null ); echo "{$cheapest_rate->currency} {$cheapest_rate->rate}"; echo $cheapest_rate->service;
国际运输需要海关信息
$customs = new Customs( type: CustomsContentsType::GIFT, signer: 'Brian Henry', customs: [ new \BrianHenryIE\BitcoinPostageInfo\Model\CustomsItem( quantity: 1, description: 'T-shirt', totalValue: '10.00', totalWeightOz: 20.5, countryCodeOfOrigin: 'US', hsTariffNumber: '610910', ) ], );
购买标签
$createPurchaseRequest = new \BrianHenryIE\BitcoinPostageInfo\Model\Request\CreatePurchaseRequest( credentials: $credentials, fromAddress: $fromAddress, toAddress: $toAddress, service: new \BrianHenryIE\BitcoinPostageInfo\Model\Service( packageType: UspsPackage::FLAT_RATE_ENVELOPE, service: 'usps_intl_first_class_package', ), dimensions: $dimensions, customs: $customs, testMode: true, ); /** @var \BrianHenryIE\BitcoinPostageInfo\Model\Response\Purchase $purchaseResponse */ $purchaseResponse = $api->createPurchase($createPurchaseRequest); /** @var \BrianHenryIE\BitcoinPostageInfo\Model\Response\PurchaseItem $purchasedLabel */ $purchasedLabel = $purchaseResponse->items[0]; echo $purchasedLabel->price; echo $purchasedLabel->filename; echo $purchasedLabel->trackingNo;
备注
- 货币金额是字符串,其他数字是浮点数,除了
weightLbs
是整数 - 一个用于生成QR码的优秀的PHP库是 chillerlan/php-qrcode(用于
::chargeCredits()
支付地址) - Guzzle是一个非常好的PSR HTTP库,但如果您正在为WordPress开发,请考虑使用 Art4/WP-Requests-PSR18-Adapter
- USPS将免费向您发送平邮信封和盒子。在线订购或在您当地的邮局取货。
贡献
PhpDoc 不完整。我不确定如何正确记录PHP 8.0构造函数属性提升属性。
使用什么编码标准更好?
尚未为UPS / FedEx进行测试。
致谢
BitcoinPostage.info 的支持非常迅速地回复了电子邮件,并根据要求更新了文档。