Skip to content
xBit iOS payments library written in Swift
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.
.bundle
.github
Sources
Tests
scripts
xBit_iOS_SDK.playground
xBit_iOS_SDK.xcodeproj
xBit_iOS_SDK.xcworkspace
xBit_iOS_SDK_Test
.gitignore
.jazzy.yml
.ruby-version
.swift-version
.travis.yml
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Cartfile
Cartfile.private
Cartfile.resolved
Dangerfile
Gemfile
Gemfile.lock
LICENSE
Package.resolved
Package.swift
Readme.md
xBit_iOS_SDK.podspec

Readme.md

xBit_iOS_SDK

Platforms License

Swift Package Manager Carthage compatible CocoaPods compatible

xBit iOS payments library

Requirements

  • iOS 9.0+
  • Xcode 10.0+

Installation

Dependency Managers

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate xBit_iOS_SDK into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

pod 'xBit_iOS_SDK', '~> 0.0.3'

Then, run the following command:

$ pod install
Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate xBit_iOS_SDK into your Xcode project using Carthage, specify it in your Cartfile:

github "xcompanies/xBit-iOS-SDK" ~> 0.0.3
Swift Package Manager

To use xBit_iOS_SDK as a Swift Package Manager package just add the following in your Package.swift file.

// swift-tools-version:4.2

import PackageDescription

let package = Package(
    name: "HelloxBit-iOS-SDK",
    dependencies: [
        .package(url: "https://github.com/xcompanies/xBit-iOS-SDK.git", .upToNextMajor(from: "0.0.3"))
    ],
    targets: [
        .target(name: "HelloxBit-iOS-SDK", dependencies: ["xBit-iOS-SDK"])
    ]
)

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate xBit_iOS_SDK into your project manually.

Git Submodules

  • Open up Terminal, cd into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
  • Add xBit_iOS_SDK as a git submodule by running the following command:
$ git submodule add https://github.com/xcompanies/xBit-iOS-SDK.git
$ git submodule update --init --recursive
  • Open the new xBit_iOS_SDK folder, and drag the xBit_iOS_SDK.xcodeproj into the Project Navigator of your application's Xcode project.

    It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.

  • Select the xBit_iOS_SDK.xcodeproj in the Project Navigator and verify the deployment target matches that of your application target.

  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.

  • In the tab bar at the top of that window, open the "General" panel.

  • Click on the + button under the "Embedded Binaries" section.

  • You will see two different xBit_iOS_SDK.xcodeproj folders each with two different versions of the xBit_iOS_SDK.framework nested inside a Products folder.

    It does not matter which Products folder you choose from.

  • Select the xBit_iOS_SDK.framework.

  • And that's it!

The xBit_iOS_SDK.framework is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.

Embedded Binaries

  • Download the latest release from https://github.com/xcompanies/xBit_iOS_SDK/releases
  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
  • In the tab bar at the top of that window, open the "General" panel.
  • Click on the + button under the "Embedded Binaries" section.
  • Add the downloaded xBit_iOS_SDK.framework.
  • And that's it!

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 a payment ViewController. After that you should present it:

import xBit_iOS_SDK

let paymentVC = XPayment.paymentViewControllerWith(
    apiKey: "YOUR API KEY",
    apiSecret: "YOUR API SECRET",
    amount: "5.0", // Amount of xBits to pay
    orderId: "123", // Id of the order to pay for
    callbackBaseUrlString: "https://google.com/") // Url of the service that will handle your payment
    
    present(paymentVC, animated: true, completion: nil)

Example project is available at /xBit_iOS_SDK_Test folder of the repository. To test it, clone repository and open xBit_iOS_SDK.xcworkspace file

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

Vihlayew Alex

License

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

You can’t perform that action at this time.