You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.1 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: travis
  5. * Date: 2019/12/12
  6. * Time: 6:20
  7. */
  8. namespace api\logic;
  9. use backend\modules\shop\models\ars\Config;
  10. use Yii;
  11. use backend\modules\shop\models\ars\Order;
  12. class WXPaymentLogic extends \yii\base\BaseObject
  13. {
  14. const PAY_TYPE_WEB = 1;
  15. const PAY_TYPE_MINI_PROGRAM = 2;
  16. public $appId;
  17. public $mchId;
  18. public $key;
  19. public $certPath;
  20. public $keyPath;
  21. public $notifyUrl;
  22. public $tradeType;
  23. public $payType;
  24. public $app;
  25. public $order;
  26. public function wxPayment()
  27. {
  28. $this->payType = self::PAY_TYPE_WEB;
  29. $data = $this->applyPaymentData();
  30. $this->unify($data);
  31. }
  32. /**
  33. * @return array
  34. */
  35. private function applyPaymentData()
  36. {
  37. $orderId = Yii::$app->request->getBodyParam('orderId');/*int 商品id*/
  38. if (empty($orderId)) {
  39. throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
  40. }
  41. $order = Order::find()
  42. ->where(['id' => $orderId, 'user_id' => Yii::$app->user->getId()])
  43. ->one();
  44. if (empty($order)) {
  45. throw new NotFoundHttpException('未找到该订单');
  46. }
  47. $data = [
  48. 'body' => '订单支付',
  49. 'out_trade_no' => $order->order_sn,
  50. 'total_fee' => round($order->pay_fee * 100),
  51. 'openid' => Yii::$app->user->identity->wx_openid ?? '',
  52. ];
  53. $this->tradeType = 'JSAPI';
  54. $this->order = $order;
  55. return $data;
  56. }
  57. private function getPaymentApp()
  58. {
  59. $this->_config();
  60. $config = [
  61. 'app_id' => $this->appId,
  62. 'mch_id' => $this->mchId,
  63. 'key' => $this->key,
  64. 'cert_path' => $this->certPath,
  65. 'key_path' => $this->keyPath,
  66. 'notify_url' => $this->notifyUrl,
  67. 'trade_type' => $this->tradeType
  68. ];
  69. $this->app = Factory::payment($config);
  70. }
  71. private function _config()
  72. {
  73. $path = Yii::getAlias('@backend');
  74. $config = Config::find()->one();
  75. switch ($this->payType) {
  76. case self::PAY_TYPE_WEB:
  77. $this->appId = trim($config->wx_appId);
  78. $this->mchId = trim($config->wx_mchId);
  79. $this->key = trim($config->wx_key);
  80. $this->certPath = trim($path . $config->wx_certPath);
  81. $this->keyPath = trim($path . $config->wx_keyPath);
  82. break;
  83. case self::PAY_TYPE_MINI_PROGRAM:
  84. $this->appId = trim($config->mini_program_appId);
  85. $this->mchId = trim($config->mini_program_mchId);
  86. $this->key = trim($config->mini_program_key);
  87. $this->certPath = trim($path . $config->mini_program_certPath);
  88. $this->keyPath = trim($path . $config->mini_program_keyPath);
  89. break;
  90. }
  91. $this->notifyUrl = $config->api_domain_name . $this->order->notify_url;
  92. }
  93. private function unify($data)
  94. {
  95. $this->getPaymentApp();
  96. return $this->app->order->unify($data);
  97. }
  98. }