AopF2F.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Http\Models\Payment;
  4. use Auth;
  5. use Exception;
  6. use Log;
  7. use Omnipay\Alipay\Responses\AopCompletePurchaseResponse;
  8. use Omnipay\Alipay\Responses\AopTradePreCreateResponse;
  9. use Omnipay\Omnipay;
  10. use Response;
  11. class AopF2F extends AbstractPayment
  12. {
  13. public function purchase($request)
  14. {
  15. $payment = new Payment();
  16. $payment->sn = self::generateGuid();
  17. $payment->user_id = Auth::user()->id;
  18. $payment->oid = $request->input('oid');
  19. $payment->amount = $request->input('amount');
  20. $payment->save();
  21. $gateway = $this->createGateway();
  22. $request = $gateway->purchase();
  23. $request->setBizContent([
  24. 'subject' => parent::$systemConfig['subject_name']? : parent::$systemConfig['website_name'],
  25. 'out_trade_no' => $payment->sn,
  26. 'total_amount' => $payment->amount
  27. ]);
  28. /** @var AopTradePreCreateResponse $response */
  29. $aliResponse = $request->send();
  30. $payment->qr_code = 'http://qr.topscan.com/api.php?text='.$aliResponse->getQrCode().'&bg=ffffff&fg=000000&pt=1c73bd&m=10&w=400&el=1&inpt=1eabfc&logo=https://t.alipayobjects.com/tfscom/T1Z5XfXdxmXXXXXXXX.png';//后备:https://cli.im/api/qrcode/code?text=".$aliResponse->getQrCode()."&mhid=5EfGCwztyckhMHcmI9ZcOKs
  31. $payment->save();
  32. return Response::json(['status' => 'success', 'data' => $payment->sn, 'message' => '创建订单成功!']);
  33. }
  34. private function createGateway()
  35. {
  36. $gateway = Omnipay::create('Alipay_AopF2F');
  37. $gateway->setSignType('RSA2'); //RSA/RSA2
  38. $gateway->setAppId(parent::$systemConfig['f2fpay_app_id']);
  39. $gateway->setPrivateKey(parent::$systemConfig['f2fpay_private_key']); // 可以是路径,也可以是密钥内容
  40. $gateway->setAlipayPublicKey(parent::$systemConfig['f2fpay_public_key']); // 可以是路径,也可以是密钥内容
  41. $notifyUrl = (parent::$systemConfig['website_callback_url']? : parent::$systemConfig['website_url']).'/payment/notify';
  42. $gateway->setNotifyUrl($notifyUrl);
  43. return $gateway;
  44. }
  45. public function notify($request)
  46. {
  47. $gateway = self::createGateway();
  48. $aliRequest = $gateway->completePurchase();
  49. $aliRequest->setParams($_POST);
  50. try{
  51. /** @var AopCompletePurchaseResponse $response */
  52. $aliResponse = $aliRequest->send();
  53. $pid = $aliResponse->data('out_trade_no');
  54. if($aliResponse->isPaid()){
  55. self::postPayment($pid, '支付宝当面付');
  56. exit('success');
  57. }
  58. }catch(Exception $e){
  59. Log::error('支付宝当面付 '.$e);
  60. exit('fail');
  61. }
  62. }
  63. public function getReturnHTML($request)
  64. {
  65. // TODO: Implement getReturnHTML() method.
  66. }
  67. public function getPurchaseHTML()
  68. {
  69. // TODO: Implement getReturnHTML() method.
  70. }
  71. }