Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi, I'm trying to setup crypto payments using coinbase commerce but I'm having a hard time returning the hosted URL, The charge.php file seems correct but the button on my checkout.html page is not fetching the information from charge.php.

The button should fetch the CURLOPT_URL and API-KEY from the PHP file along with the arrays for description, customerUID, price etc. If I fetch from the data.json file I receive the Object information but I need to fetch the information from charge.php instead, How do I do this using Async/Await?

Any help will be much appreciated :) Thanks.

Here is my checkout.html...

HTML
<button id="btn">Pay with Crypto?</button>

<p id="pay"></p>


<script>
const btn = document.getElementById('btn');
const pay = document.getElementById('pay');

btn.onclick = async() => {
const response = await fetch('coinbasePHPtest/charge.php');
const charge= await response.json();
console.log(charge);
pay.innerHTML = `<a href="${charge}">Pay Now!</a>`
}
</script>


And here is my charge.php file...

PHP
<?php
$userdetails = $_SESSION['usr_name'];
$repairprice = $_POST['price'];
$customerUID = $_POST['uid'];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.commerce.coinbase.com/charges/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$post = array(
    "name" => "TeZe",
    "description" => "Repair Service",
    "local_price" => array(
        'amount' => "$repairprice",
        'currency' => 'GBP'
    ),
    "pricing_type" => "fixed_price",
    "metadata" => array(
        'customer_id' => "$customerUID",
        'customer_name' => "$userdetails"
    )
);

$post = json_encode($post);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-Cc-Api-Key: df-xxxxxxx-1';
$headers[] = 'X-Cc-Version: 2018-03-22';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

$response = json_decode($result, TRUE);
return $response->data->hosted_url;
?>


Here is the old data.json file..
{
"name":"TeZe",
"description":"Selected Repair Service",
"pricing_type":"fixed_price"
}


What I have tried:

This script worked in returning an object {description:Repair service, name:TeZe, pricing_type:fixed_price}

<script>
const btn = document.getElementById('btn');
const pay = document.getElementById('pay');

btn.onclick = async() => {
const res = await fetch('coinbasePHPtest/data.json');
const charge= await res.json();
console.log(charge);
pay.innerHTML = `<a href="${charge}">Pay Now!</a>`
}
</script>
Posted
Updated 23-Oct-21 7:12am
v5

1 solution

Did you tried debug (breakpoint | var_dump), response from charge.php?
What did you get as response from curl, try to fetch html status code, erros, to get as more information. Log - var_dump, print_r the response from coinbase, log all step results, than check each by one, if the response is as it expected.
 
Share this answer
 
Comments
[no name] 28-Oct-21 11:11am    
All I have is console.log() I dont know how to use var_dump with what I'm doing specifically as the curl file is seperate to my checkout page...do I put the var_dup within the curl file or the checkout file? Also when I tried to look how this works it shows I have to use array within the dump script but how am I supposed to type the arrays if its not even fetching the arrays?
[no name] 28-Oct-21 11:19am    
The problem is that the checkout page script is supposed to fetch the CURLOPT_URL but the script is trying to fetch the JSON Object instead, I need it to fetch the coinbase link and the json data at the same time somehow

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900