AopF2F.php 2.6 KB

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