AopF2F.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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';
  31. //后备:https://cli.im/api/qrcode/code?text=".$aliResponse->getQrCode()."&mhid=5EfGCwztyckhMHcmI9ZcOKs
  32. $payment->save();
  33. return Response::json(['status' => 'success', 'data' => $payment->sn, 'message' => '创建订单成功!']);
  34. }
  35. private function createGateway()
  36. {
  37. $gateway = Omnipay::create('Alipay_AopF2F');
  38. $gateway->setSignType('RSA2'); //RSA/RSA2
  39. $gateway->setAppId(parent::$systemConfig['f2fpay_app_id']);
  40. $gateway->setPrivateKey(parent::$systemConfig['f2fpay_private_key']);
  41. $gateway->setAlipayPublicKey(parent::$systemConfig['f2fpay_public_key']);
  42. $gateway->setNotifyUrl((parent::$systemConfig['website_callback_url']? : parent::$systemConfig['website_url']).'/callback/notify?method=f2fpay');
  43. return $gateway;
  44. }
  45. public function notify($request)
  46. {
  47. $gateway = self::createGateway();
  48. $request = $gateway->completePurchase();
  49. $request->setParams($_POST);
  50. try{
  51. /** @var AopCompletePurchaseResponse $response */
  52. $response = $request->send();
  53. if($response->isPaid()){
  54. self::postPayment($response->data('out_trade_no'), '支付宝当面付');
  55. exit('success');
  56. }else{
  57. exit('fail');
  58. }
  59. }catch(Exception $e){
  60. Log::error('支付宝当面付 '.$e);
  61. exit('fail');
  62. }
  63. }
  64. public function getReturnHTML($request)
  65. {
  66. // TODO: Implement getReturnHTML() method.
  67. }
  68. public function getPurchaseHTML()
  69. {
  70. // TODO: Implement getReturnHTML() method.
  71. }
  72. }