Showing posts with label bitpay. Show all posts
Showing posts with label bitpay. Show all posts

Wednesday, April 30, 2014

BitPay Api Integration

Simple PHP Class Wrapper using BitPay Api Integration.

Source Code
<?php

class BitPay
{
    var $config = array();
    
    function __construct() {
        $this->config = array(
                'apiKey'            => '', /* bitpay.com Api key*/
                'verifyPos'         => 'true',
                'notificationEmail' => '',
                'notificationURL'   => '',
                'redirectURL'       => '',
                'currency'          => 'BTC',
                'physical'          => 'true',
                'fullNotifications' => 'true',
                'transactionSpeed'  => 'low'
         );
    }
    
    function execute ($url, $apiKey, $post = false){
        $curl   = curl_init($url);
        $length = 0;
        
        if ($post) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
            $length = strlen($post);
        }

        $uname = base64_encode($apiKey);
        $header = array(
            'Content-Type: application/json',
            "Content-Length: $length",
            "Authorization: Basic $uname",
        );

        curl_setopt($curl, CURLOPT_PORT, 443);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1); 
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); 
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
        curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);

        $responseString = curl_exec($curl);

        if ($responseString == false) {
            $response = curl_error($curl);
        } else {
            $response = json_decode($responseString, true);
        }
        
        curl_close($curl);
        return $response;
    }
    
    function CreateInvoice($orderId, $price, $posData, $options = array()) {
        
        $options = array_merge($this->config, $options); 

        $options['posData'] = '{"posData": "' . $posData . '"';
        if ($this->config['verifyPos']) 
            $options['posData'].= ', "hash": "' . crypt($posData, $this->config['apiKey']) . '"';
        $options['posData'].= '}';

        $options['orderID'] = $orderId;
        $options['price'] = $price;

        $postOptions = array(   'orderID', 
                                'itemDesc', 
                                'itemCode', 
                                'notificationEmail', 
                                'notificationURL', 
                                'redirectURL',
                                'posData', 
                                'price', 
                                'currency', 
                                'physical', 
                                'fullNotifications', 
                                'transactionSpeed', 
                                'buyerName',
                                'buyerAddress1', 
                                'buyerAddress2', 
                                'buyerCity', 
                                'buyerState', 
                                'buyerZip', 
                                'buyerEmail', 
                                'buyerPhone'
                            );
        foreach ($postOptions as $o){
            if (array_key_exists($o, $options)){
                $post[$o] = $options[$o];
            }
        }
            
        $post       = json_encode($post);
        $response   = $this->execute('https://bitpay.com/api/invoice/', $this->config['apiKey'], $post);

        return $response;
    }
    
    function VerifyNotification($apiKey = false) {
        
        if (!$apiKey)
            $apiKey = $this->config['apiKey'];

        $post = file_get_contents("php://input");
        if (!$post)
            return 'No post data';

        $json = json_decode($post, true);

        if (is_string($json))
            return $json; // error

        if (!array_key_exists('posData', $json))
            return 'no posData';

        $posData = json_decode($json['posData'], true);
        if ($this->config['verifyPos'] and $posData['hash'] != crypt($posData['posData'], $apiKey))
            return 'authentication failed (bad hash)';
        $json['posData'] = $posData['posData'];

        return $json;
    }
    
    function GetInvoice($invoiceId, $apiKey = false) {
        
        if (!$apiKey)
            $apiKey = $this->config['apiKey'];

        $response = $this->execute('https://bitpay.com/api/invoice/' . $invoiceId, $apiKey);
        if (is_string($response))
            return $response; // error
        $response['posData'] = json_decode($response['posData'], true);
        return $response;
    }
            
}


?>


Genesis Framework for WordPress