我的订阅控制器中有一个公共函数,可以从 chargify 中删除订阅的挂起状态。
从他们的文档中,使用 pecl/http1 方法的代码应该是这样的:
$request = new HttpRequest();
$request->setUrl('https://subdomain.chargify.com/subscriptions/$subscriptionId/delayed_cancel.json');
$request->setMethod(HTTP_METH_DELETE);
$request->setHeaders(array(
'authorization' => 'Basic YXBpLWtleTp4'
));
$request->setBody('{}');
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
因此我把这段代码放在我的函数中
public function changeYearlySubscriptionBillingDate(Request $request)
{
$chargify = new \Crucial\Service\Chargify([
'hostname' => env('CHARGIFY_HOSTNAME'),
'api_key' => env('CHARGIFY_KEY'),
'shared_key' => env('CHARGIFY_SHARED_KEY')
]);
$subscription = $chargify->subscription();
$user = $request->user();
$subscriptionId = $user->subscription->subscription_id;
$nextBilling = Carbon::now()->addYear();
$hostname = env('CHARGIFY_HOSTNAME');
$config = [
'headers' => [
'authorization' => 'Basic YXBpLWtleTp4',
'content-type' => 'application/json'
]
];
$client = new Client($config);
$res = $client->put("https://$hostname/subscriptions/$subscriptionId/.json",
["json" => [
[ "subscription" =>
[ "next_billing_at" => $nextBilling ]
]
]]);
echo $res->getBody();
}
因为我是 Laravel 的新手,而且这个文档一般是针对 PHP 的,所以我不确定如何在 Laravel 框架中转换这段代码。
参考:
错误信息:
Client error: `PUT https://(the hostname)/subscriptions/(the id)/.json` `resulted in a `404 Not Found` response:`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>404 File Not Found</title>
<style>
b (truncated...)
参考2:官方文档
https://reference.chargify.com/v1/subscriptions/update-subscription-calendar-billing-day-change