<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


function getCoinbaseToken() {
	return '3927fe05-373f-494f-956f-20db3ab96322';
}

function coinbaseApiCreateCharge($planName, $price, $user_id, $plan, $siteUrl) {
    $url = 'https://api.commerce.coinbase.com/charges';
	 
	$ch = curl_init($url);
	 
	$jsonData = array( 
		'name' => 'YGG',
		'description' => $planName,
		'local_price' => array(
							'amount' => $price,
							'currency' => 'EUR'
						),
		'pricing_type' => 'fixed_price',
		'metadata' => array('user_id' => $user_id, 'plan' => $plan),
		'redirect_url' => $siteUrl.'donation/bonus/pending_confirmation',
		'cancel_url' => $siteUrl.'donation/bonus'
	);

	$jsonDataEncoded = json_encode($jsonData);
	 
	$header = array(
		'Content-Type: application/json',
	    'X-CC-Api-Key: '.getCoinbaseToken(),
	    'X-CC-Version: 2018-03-22'
	);
	
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$result = curl_exec($ch);

	return json_decode($result);
}