Skip to content
xBit Android payments library written in Kotlin
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea
app
gradle/wrapper
sdk
.gitignore
LICENSE
Readme.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

Readme.md

xBit-Android-SDK

Platforms License

xBit Android payments library

Requirements

  • Android SDK 21+
  • Android Studio 2.3.2+

Installation

Dependency Managers

JitPack is a novel package repository for JVM and Android projects. It builds Git projects on demand and provides you with ready-to-use artifacts

Add this lines to build.gradle (Project level):

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Add this lines to build.gradle (App level):

dependencies {
    implementation 'com.github.xcompanies:xBit-Android-SDK:1.0.3'
}

Or you can use this library together with:

Maven
<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>
<dependency>
  <groupId>com.github.xcompanies</groupId>
  <artifactId>xBit-Android-SDK</artifactId>
  <version>1.0.3</version>
</dependency>
Sbt
resolvers += "jitpack" at "https://jitpack.io"
libraryDependencies += "com.github.xcompanies" % "xBit-Android-SDK" % "1.0.3"

Manually

JAR file

Add the compiled AAR (or JAR) file (the library must be already built):

  • Click File > New > New Module.
  • Click Import .JAR/.AAR Package then click Next.
  • Enter the location of the compiled AAR or JAR file then click Finish.
Source code

Import the library module to your project (the library source becomes part of your project):

  • Click File > New > Import Module.
  • Enter the location of the library module directory then click Finish.

The library module is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead add the compiled AAR file as described above.

Usage

To start accepting payments in xBit you need to create xChange account, configure a Merchant and get your API Key and API Secret. To initiate a payment, you need to import library and call it's method this way to get Activity with payment View. After that you should present it:

Java
import com.xbitcrypto.sdk.PaymentFormCreator;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PaymentFormCreator.INSTANCE.runPaymentForm(
            this,
            "API KEY", //todo put your api key here
            "API SECRET", //todo put your api secret here
            1.0, // Amount of xBits to pay
            "order ID", // Id of the order to pay for
            "http://google.com"); // Url of the service that will handle your payment
    }
}
Kotlin
import com.xbitcrypto.sdk.PaymentFormCreator

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        PaymentFormCreator.runPaymentForm(
            this,
            "API KEY", //todo put your api key here
            "API SECRET", //todo put your api secret here
            1.0, // Amount of xBits to pay
            "order ID", // Id of the order to pay for
            "http://google.com") // Url of the service that will handle your payment
    }
}

Example project is available at /app folder of the repository. To test it, clone repository and open it in Android Studio

Callback

xChange offers a callback service that will notify your system about successfull payments and used to mark an order as paid.

The callback service is asynchronous and as such will not interfer with or prolong the processing time of the API request generating a callback - eg. the time your customer will have to wait for payment confirmation.

In the event that your system is not able to receive or correctly process the callback, the callback service will try to deliver its message up to 10 times, with gradually increasing delays between each try.

Callback params:

Param Description
api_key your Merchant API Key
amount amount in xBit
order_id given Order ID
signature calculated signature for callback validation

Signature

For protocols HTTP and HTTPS it is specified that xChange signs the callback. If the client verifies the signature according to the specified signature method, any data sent with the callback can be used safely. To verify that the values, which you have received in the result URL, are valid and have not been tampered in the process, refer to this examples.

To verify that the values, which you have received in the result URL, are valid and have not been tampered in the process, refer to this examples:

PHP

<?php
class XBitGateway
{
    private $apiKey;
    private $apiSecret;
    private $logger;

    public function processCallback()
    {
        $this->logger->putLog("Callback");
        foreach (["api_key", "amount", "order_id", "signature"] as $name) {
            if (empty($_POST[$name])) {
                $this->logger->putLog("Callback fail. \n" . var_export($_SERVER, 1) . "\n" . var_export($_REQUEST, 1));
                wp_die( 'XBIT Request Check Failure', 'XBIT payment', array( 'response' => 500 ) );
            }
        }

        if (!$this->checkSignature()) {
            wp_die( 'XBIT Request Check Failure', 'XBIT payment', array( 'response' => 500 ) );
        }

        $order = wc_get_order($_POST['order_id']);

        if (($total = get_post_meta($_POST['order_id'], "_xbit_total", true)) == $_POST["amount"]) {
            $this->logger->putLog("Everything is OK");
            $order->payment_complete();
        } else {
            $this->logger->putLog("Order Total is $total but amount is $_POST[amount]");
        }
    }

    private function checkSignature()
    {
        $recalculatedSignature = hash("sha256", $this->apiKey . $_POST["order_id"] . sprintf("%.02f", round($_POST["amount"], 2)) . $this->apiSecret);
        if ($recalculatedSignature !== $_POST["signature"]) {
            $this->logger->putLog("Invalid signature\nRecalculated Signature is $recalculatedSignature\nSent signature is $_POST[signature]\n" . var_export($_REQUEST, 1));
            return false;
        }
        return true;
    }
}
?>
Python
from hashlib import sha256

def process_callback(request):
    data = request.POST.dict()
    api_key = 'YourMerchantAPIKey'
    order_id = data.get('order_id')
    amount = data.get('amount')
    api_secret = 'YourMerchantAPISecret'
    sign = '{api_key}{order_id}{amount}{api_secret}'.format(
        api_key=api_key,
        order_id=order_id,
        amount=amount,
        api_secret=api_secret
    )
    signature = sha256(sign.encode('ascii')).hexdigest()

    # compare calculated signature with received from request
    if signature == data.get(signature):
        # callback is authenticated
    else:
        # callback is not authenticated
    ...

Currency rates

You can get xBit rates in JSON format sending GET request to Currency Rates endpoint:
https://myxpay.net/api/v1/xbit_currency_rates/

Response example:

{
    "status": 1,
    "rates":{
        "EUR": "0.296766823",
        "USD": "0.332283877",
        "GBP": "0.256415435",
        "BTC": "0.000080135",
        "ETH": "0.002346883"
    }
}

Contributing

Issues and pull requests are welcome!

Author

Max Mamuta

License

xBit_Android_SDK is released under the MIT license. See LICENSE for details.

You can’t perform that action at this time.