du958642589 2018-07-15 07:49
浏览 43
已采纳

Laravel自定义商店方法无法正常工作

I am intergrating paypal payment gateway on my application, before user first leave my site for paypal, I saved all the necessary information, and set payment_status to be 0, when user returns, if payment is sucessful, it returns to a new page where it gets the paymentId, and payerID, plus sets the paymentstatus to 1.

the thing is that if i set the database value for paymentID and PaymentId to null, it doesnt save.

if i dont set it to anything i.e leave it blank, it would save, but retruns an error:

Integrity constraint violation: 1048 Column 'PayerID' cannot be null (SQL: update `paypal_officers` set `paymentId` = , `PayerID` = , `updated_at` = 2018-07-15 07:29:21 where `id` = 1)

i cant seem to figure out what the problem is.

Here is the method that does the saving.

public function getPaymentStatus(Input $input)
{
    /** Get the payment ID before session clear **/
    $payment_id = Session::get('paypal_payment_id');

    $insert_the_user_id = PaypalOfficer::findorFail(\session()->get('receivers_main_info_id'));
    $insert_the_user_id->update([
        'paymentId' => Input::get('paymentId'),
        'PayerID' => Input::get('PayerID'),
        'payment_status' => 1,
    ]);
    $insert_the_user_id->save();

    /** clear the session payment ID **/
    Session::forget('paypal_payment_id');

    if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
        \Session::put('error', 'Payment failed');
        return Redirect::to('account/send-money/paypal/send')->with('payer_id', $payment_id);;
    }

    $payment = Payment::get($payment_id, $this->_api_context);
    $execution = new PaymentExecution();
    $execution->setPayerId(Input::get('PayerID'));
    /**Execute the payment **/
    $result = $payment->execute($execution, $this->_api_context);

    if ($result->getState() == 'approved') {
        \Session::put('success', 'Payment success');
        return Redirect::to('account/send-money/paypal/stagged/paypal')->with('payer_id', $result);
    }

    \Session::put('error', 'Payment failed');
    return Redirect::to('account/send-money/paypal/send');
}

My Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class PaypalOfficer extends Model
{
    protected $fillable = [
        'payment_status',
        'paymentId',
        'PayerID',
        'user_id',
        'amount',
        'destination_account_number',
        'destination_account_name',
        'destination_bank_name',
        'destination_phone_number',
        'destination_country',
        'destination_state',
        'currency_symbol',
        'receivers_name',
    ];
    //
    public function user(){
        return $this->belongsTo(User::class);
    }
}

Please what must i be missing in the code , anybody please!

  • 写回答

1条回答 默认 最新

  • douou2026 2018-07-15 08:25
    关注

    Error occurs when Input::get('PayerID') is empty or null. You must be firstly check all required field exists in input, then try to update PaypalOfficer. Also in eloquent model use ->update method that case ->save does not need.
    Fix this part

    public function getPaymentStatus(Input $input)
    {
        // firstly check PayerID and paymentId input exist
        if (empty(Input::get('PayerID')) || empty(Input::get('paymentId')) || empty(Input::get('token'))) {
            \Session::put('error', 'Payment failed');
            return Redirect::to('account/send-money/paypal/send')->with('payer_id', $payment_id):
        }
    
        /** Get the payment ID before session clear **/
        $payment_id = Session::get('paypal_payment_id');
    
        $insert_the_user_id = PaypalOfficer::findorFail(\session()->get('receivers_main_info_id'));
        $insert_the_user_id->update([
            'paymentId' => Input::get('paymentId'),
            'PayerID' => Input::get('PayerID'),
            'payment_status' => 1,
        ]);
    
        /** clear the session payment ID **/
        Session::forget('paypal_payment_id');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能