无法在mysql数据库中存储条带webhook数据

wz1wpwve  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(343)

我在我的电脑里设置了webhook stripe 当我从stripe dashboard测试webhook时,我从stripe得到了成功的结果。我可以在预览中看到React。
但是,当我在浏览器上做这件事并点击相同的webhook url时,我的React却一无所获。
这是我用来让它工作的代码。

include('config.php');

function searchArrayValueByKey(array $array, $search) 
{
    foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key => $value) 
    {
        if ($search === $key)
        return $value;
    }
    return false;
}

require_once('vendor/autoload.php');

\Stripe\Stripe::setApiKey("Stripe Key");

// This function is leveraged to search through the JSON for specific values

$input = @file_get_contents("php://input");         
// this gets the POST data provided when the webhook pings this page
$event_json = json_decode($input, TRUE);    
// decode the JSON, and TRUE turns it into a multidimensional array

if(isset($type))
{
    $type = searchArrayValueByKey($event_json, 'customer.subscription.updated'); 
}
else
{
    $type = "";
}
// Check the type of event sent from Stripe.com - example "charge.succeeded"

switch ($type) 
{                                   
    // depending on the event fired by Stripe, do something different. 
    case "account.updated":
        // do something
        break;
    case "balance.available":
        // do something
        break;
    case "charge.captured":
        // do something
        break;
    case "charge.refunded":
        // do something
        break;
    case "charge.succeeded":
        // do something
        break;
    case "charge.updated":
        // do something
        break;
    case "charge.failed":
        // do something
        break;
    case "charge.dispute.created":
        // do something
        break;
    case "charge.dispute.updated":
        // do something
        break;
    case "charge.dispute.closed":
        // do something
        break;
    case "coupon.created":
        // do something
        break;
    case "coupon.deleted":
        // do something
        break;
    case "customer.created":
        // do something
        break;
    case "customer.updated":
        // do something
        break;
    case "customer.deleted":
        // do something
        break;
    case "customer.card.created":
        // do something
        break;
    case "customer.card.updated":
        // do something
        break;
    case "customer.card.deleted":
        // do something
        break;
    case "customer.discount.created":
        // do something
        break;
    case "customer.discount.updated":
        // do something
        break;
    case "customer.discount.deleted":
        // do something
        break;
    case "customer.subscription.created":
        // do something
        break;
    case "customer.subscription.updated":

        $id = searchArrayValueByKey($event_json, 'id'); 
        $customer_id = searchArrayValueByKey($event_json, 'customer'); 
        $created = searchArrayValueByKey($event_json, 'created'); 
        $subscription = searchArrayValueByKey($event_json, 'subscription'); 
        $period_start = searchArrayValueByKey($event_json, 'period_start'); 
        $period_end = searchArrayValueByKey($event_json, 'period_end'); 

        $insert_into = mysqli_query($conn,"insert into ext_payment_data set
                                        subscription = '".$subscription."',
                                        add_date = '".$created."',
                                        subscr_id = '".$subscription."',
                                        subscribe_from_date = '".$period_start."',
                                        subscribe_to_date = '".$period_end."',
                                        response = '".json_encode($event_json)."'
            ");     

        break;
    case "customer.subscription.deleted":
        // do something
        break;
    case "customer.subscription.trial_will_end":
        // do something
        break;
    case "invoice.created":
        // do something
        break;
    case "invoice.updated":
        // do something
        break;
    case "invoice.payment_succeeded":
        // do something
        break;
    case "invoice.payment_failed":
        // do something
        break;
    case "invoiceitem.created":
        // do something
        break;
    case "invoiceitem.updated":
        // do something
        break;
    case "invoiceitem.deleted":
        // do something
        break;
    case "plan.created":
        // do something
        break;
    case "plan.updated":
        // do something
        break;
    case "plan.deleted":
        // do something
        break;
    case "transfer.created":
        // do something
        break;
    case "transfer.updated":
        // do something
        break;
    case "transfer.paid":
        // do something
        break;
    case "transfer.failed":
        // do something
        break;
    default:
        echo "Error";

}

$myfile = fopen("auto_response.txt", "a+") or die("Unable to open file!");
file_put_contents('auto_response.txt', print_r($event_json,true),FILE_APPEND | LOCK_EX);
fclose($myfile);

==================
在这里写下答案 auto_response.txt 文件中,我可以将数据存储在文件中,但在数据库中存储数据时却一无所获。
所以有没有人有任何解决方案,因为我无法在mysql数据库中存储stripe响应。

jhdbpxl9

jhdbpxl91#

在查询中,可以执行以下操作:

insert into ext_payment_data set

这个 set 运算符用于更新,而不是插入。
查询需要稍作更改:

"insert into ext_payment_data (
    subscription, 
    add_date, 
    subscr_id, 
    subscribe_from_date, 
    subscribe_to_date, 
    response
 ) values (
    '".$subscription."', 
    '".$created."', 
    '".$subscription."', 
    '".$period_start."', 
    '".$period_end."', 
    '".json_encode($event_json)."'
 )
"

注意,根据处理用户输入的方式,使用准备好的语句可以更安全地防止sql注入。

相关问题