有没有一种方法可以将元数据从Subscription API传递到Charge API?

时间:2019-01-21 06:04:02

标签: php stripe-payments

我有两个功能,分别是收费和订阅,收费成功时 碰巧我在下面插入一些元数据:

enter image description here

与订阅功能相同:

enter image description here

然后我有一个单独的文件,因为它仅调用费用列表API来获取所有成功的费用以检查其中的所有元数据,所以我不会在此处发布该文件,但是我注意到,一旦我通过API创建了订阅,仅在其中提供元数据1和元数据2。

当我查看由订阅产生的费用时,其元数据没有附加任何内容。如何将订阅中的元数据附加到与其对应的费用上?

enter image description here

<?php
  require_once('stripe-php-master/init.php');
  require 'apiKeys.php';

$data = $_REQUEST['data'];

if(isset($data['stripeToken'])){
    create_customer($data);
}else{
    printResponse($data,"No Token");
}

//Create Customer
function create_customer($data){

    try{
        \Stripe\Stripe::setApiKey(SK_TEST_KEY);
        $customer = \Stripe\Customer::create(array(
            "description" => 'Widget Create Customer',
            "source" => $data['stripeToken'],
            "email" => $data['stripeEmail']
        ));

        $data['customerID'] = $customer->id;
        //create_charge($data);
        create_subscription($data);

    }catch(Exception $e){
        echo $e->getMessage();
        exit;
    }

}//create_customer

//Create charge
function create_charge($data){

    try{
        \Stripe\Stripe::setApiKey(SK_TEST_KEY);

        $settings = [
            'currency' => 'aud',
            'amount'=>5500,
            'description' => 'Widget Payment for ' .$data['widget_name'],
            'customer' => $data['customerID'],
            'metadata'=>["metadata1" => $data['metadata1'],"metadata2" => $data['metadata2']]
        ];

        // Get the payment token ID submitted by the form:
        $charge = \Stripe\Charge::create($settings);

        printResponse($data, $charge->status);

    }catch(Exception $e){
        printResponse($data,$e->getMessage());
        exit;
    }
}//create_charge

//Create subscription
function create_subscription($data){

    try{

        \Stripe\Stripe::setApiKey(SK_TEST_KEY);

        $settings = [
            'customer' => $data['customerID'],
            'metadata'=>["metadata1" => $data['metadata1'],"metadata2" => $data['metadata2']],
            'items' => [
                [
                  'plan' => $data['widget_class']
                ]
            ]

        ];

        $subscription = \Stripe\Subscription::create($settings);   
        printResponse($data,$subscription->status);

    }catch(Exception $e){
        printResponse($data,$e->getMessage());
        exit;
    }
}

function printResponse($data,$status){
     //CREATE Response JSON
    print_r(json_encode([
        'customer_name'=> $data['customer_name'],
        'metadata1' => $data['metadata1'],
        'metadata2'=> $data['metadata2'],
        'payment_status'=>$status
    ]));

}
?>

我只想要某人从订阅事件中订阅费用时,将元数据1和元数据2附加到它。

0 个答案:

没有答案