所以我需要用户上传文件。这些文件可以由客户(不是用户)购买。一旦付款已经由stripe处理,买家应该会收到一封电子邮件,其中包含下载文件的链接。我需要的是这些文件的下载链接,只有上传文件的用户和购买文件的客户才能使用。
这是我的控制器,用于处理由用户填写的表单以上传文件。NonUser是前一个表单中文件的详细信息。
$note = new Note;
$note->title = $nonUser->title;
$note->description = $nonUser->description;
$note->mark = $nonUser->mark;
$note->page_count = $nonUser->page_count;
$note->subject_id = $nonUser->subject_id;
$note->year = $nonUser->year;
$note->modules = $nonUser->modules;
$note->price = $nonUser->modules*25;
$note->user_id = $user->id;
$note->exam_id=$user->exam_id;
$note->save();
Storage::put(
'notes/' . $note->id . '.pdf',
file_get_contents($request->file('notes')->getRealPath())
);
我的条纹处理。
public function charge()
{
$nid = Session::get('nid');
$note = Note::where('id','=',$nid)->first();
$price = $note->price;
'Stripe'Stripe::setApiKey("sk_test_key");
$token = $_POST['stripeToken'];
try {
$charge = 'Stripe'Charge::create(array(
"amount" => $price*100, // amount in cents, again
"currency" => "aud",
"source" => $token,
"description" => "Example charge"
));
} catch('Stripe'Error'Card $e) {
flashWarning('An error occured');
return back();
}
flash('payment succesful! Check your email for a download link!');
return back();
}
我会使用以下步骤为买家提供服务:
-
一旦支付成功,在DB中存储
orderID
,fileID
(应该匹配所有文件的主键,存储在不同的表中)一个随机散列作为download_ticket
,一个DateTime作为ticket_expiration
,以及票证被使用的次数作为download_count
-
电子邮件买家下载链接,指向一个php脚本。脚本应该期待下载票据。例子:
example.com/download.php ?票= m54hm4j390534gi2frew0094
在脚本download.php
中,您将执行以下操作:
- 从查询字符串中抓取票:
$ticket = $_GET['ticket']
- 获取DB中的记录:
SELECT * from tbl WHERE ticket=m54hm4j390534gi2frew0094
如果没有匹配,用 - 如果
ticket_expiration
已通过,则删除记录,错误403 forbidden
并中止。 - 如果
download_count
超过限制,删除记录,错误429 too many requests
,终止。 - 使用
fileID
列查找已购买的文件
http_response_code(404)
错误404 not found
并中止。如果所有检查都有效,您可以将文件发送给用户。不将用户重定向到文件的真实位置。相反,可以这样做:
$path = '...'; // real path on disk
header('Content-Type: application/pdf');
header('Content-Length: ' . filesize($path));
$pipe = fopen($path, 'rb');
fpassthru($pipe); //sends file to user
fclose($pipe);
//TODO: increment `download_count` in the purchase record
您可以通过以下方式实现
步骤1:生成链接
当你做
Storage::put(
'notes/' . $note->id . '.pdf',
file_get_contents($request->file('notes')->getRealPath())
);
用
生成链接$data['link'] = URL::to('/notes/'.$note->id.'pdf'); //Modify path according to your need
$message = 'Your Custom HTML';
步骤2:发送电子邮件
触发给上传用户的邮件
Mail::send(email.file, function ($message) {
$message->from('youremail@example.com', 'File Received');
$message->to(Auth::user()->email); // Email id of one who uploads it
});
触发邮件谁购买了文件
$userlist = User::where('file_id' , $fileid)->get(); //modify the condition according to the tables you have
foreach($userlist as $users)
{
Mail::send(email.file, function ($message) {
$message->from('youremail@example.com', 'File Received');
$message->to($user['email']); // Looped Email id
});
}
附加步骤:(确保文件是安全的)
不要直接指向文件,你可以这样做
- 生成如下链接
yourapp.com/pdf/12/143
其中12是用户id, 143是pdf文件的id
在您的控制器下,您可以检查id为12的用户是否被授权下载id为143的文件,如果是,则生成pdf视图或下载给用户。
希望这对你有帮助
付款成功处理后,将文件上传的PK(来自数据库)存储在一个新表中(我们称之为购买的下载),并附带一个唯一令牌,可用于在该表中查找文件的PK。这将是你发送给他们下载文件的电子邮件的令牌。
创建一个新的控制器,接受这个令牌,并在购买的下载表中查找唯一的令牌,然后你可以使用X-Sendfile
头之类的东西,让你的web服务器在令牌验证的情况下从你的文件系统向客户端提供文件。如果您愿意,还可以设置此令牌的过期时间。